00001 // 00002 // FileFragments/Queue.hpp 00003 // 00004 // Copyright (c) Shareaza Development Team, 2002-2005. 00005 // This file is part of SHAREAZA (www.shareaza.com) 00006 // 00007 // Shareaza is free software; you can redistribute it 00008 // and/or modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 of 00010 // the License, or (at your option) any later version. 00011 // 00012 // Shareaza is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with Shareaza; if not, write to the Free Software 00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 // 00021 00022 #ifndef FILEFRAGMENTS_QUEUE_HPP_INCLUDED 00023 #define FILEFRAGMENTS_QUEUE_HPP_INCLUDED 00024 00025 namespace detail 00026 { 00027 00028 class Queue 00029 { 00030 // Interface 00031 public: 00032 // Typedefs 00033 typedef Fragment< EmptyType, u64 > FragmentType; 00034 typedef std::list< FragmentType > ContainerType; 00035 typedef FragmentType::SizeType FSizeType; 00036 typedef FragmentType::PayloadType PayloadType; 00037 typedef FragmentType::Traits Traits; 00038 typedef ContainerType::value_type ValueType; 00039 typedef ContainerType::pointer Pointer; 00040 typedef ContainerType::const_pointer ConstPointer; 00041 typedef ContainerType::reference Reference; 00042 typedef ContainerType::const_reference ConstReference; 00043 typedef ContainerType::iterator Iterator; 00044 typedef ContainerType::const_iterator ConstIterator; 00045 typedef ContainerType::reverse_iterator ReverseIterator; 00046 typedef ContainerType::const_reverse_iterator ConstReverseIterator; 00047 typedef ContainerType::size_type SizeType; 00048 typedef ContainerType::difference_type DifferenceType; 00049 typedef ValueType value_type; 00050 typedef Pointer pointer; 00051 typedef ConstPointer const_pointer; 00052 typedef Reference reference; 00053 typedef ConstReference const_reference; 00054 typedef Iterator iterator; 00055 typedef ConstIterator const_iterator; 00056 typedef ReverseIterator reverse_iterator; 00057 typedef ConstReverseIterator const_reverse_iterator; 00058 typedef SizeType size_type; 00059 typedef DifferenceType difference_type; 00060 // Constructor 00061 // Use implicit version 00062 // Copy constructor 00063 // Use implicit version 00064 // Destructor 00065 // Use implicit version // We don't inherit from this class 00066 // Assignment 00067 // Use implicit version 00068 00069 // Iterators 00070 Iterator begin() { return s_.begin(); } 00071 ConstIterator begin() const { return s_.begin(); } 00072 Iterator end() { return s_.end(); } 00073 ConstIterator end() const { return s_.end(); } 00074 ReverseIterator rbegin() { return s_.rbegin(); } 00075 ConstReverseIterator rbegin() const { return s_.rbegin(); } 00076 ReverseIterator rend() { return s_.rend(); } 00077 ConstReverseIterator rend() const { return s_.rend(); } 00078 00079 // Accessors 00080 SizeType size() const { return s_.size(); } 00081 // @empty Returns true, if list does not contain any fragments. 00082 bool empty() const { return s_.size() == 0; } 00083 // Operations 00084 void clear() 00085 { 00086 s_.clear(); 00087 } 00088 void pushBack(const FragmentType& insertFragment) 00089 { 00090 s_.push_back( insertFragment ); 00091 } 00092 void push_back(const FragmentType& insertFragment) 00093 { 00094 pushBack( insertFragment ); 00095 } 00096 template< typename InputIterator > 00097 void insert(InputIterator first, InputIterator last) 00098 { 00099 for( ; first != last; ) s_.insert( *first++ ); 00100 } 00101 void erase(const FragmentType& eraseFragment) 00102 { 00103 for( Iterator it = begin(); it != end(); ) 00104 { 00105 if( eraseFragment.begin() < it->end() 00106 && eraseFragment.end() > it->begin() ) 00107 { 00108 if( eraseFragment.begin() <= it->begin() ) 00109 { 00110 if( eraseFragment.end() >= it->end() ) 00111 { 00112 it = s_.erase( it ); 00113 } 00114 else 00115 { 00116 *it = FragmentType( eraseFragment.end(), it->end() ); 00117 ++it; 00118 } 00119 } 00120 else 00121 { 00122 if( eraseFragment.end() >= it->end() ) 00123 { 00124 *it = FragmentType( it->begin(), 00125 eraseFragment.begin() ); 00126 ++it; 00127 } 00128 else 00129 { 00130 s_.push_back( 00131 FragmentType( eraseFragment.end(), it->end() ) ); 00132 *it = FragmentType( 00133 it->begin(), eraseFragment.begin() ); 00134 ++it; 00135 } 00136 } 00137 } 00138 else 00139 { 00140 ++it; 00141 } 00142 } 00143 } 00144 template< typename InputIterator > 00145 void erase(InputIterator first, InputIterator last) 00146 { 00147 for( ; first != last; ) erase( *first++ ); 00148 } 00149 Iterator erase(Iterator where) 00150 { 00151 return s_.erase( where ); 00152 } 00153 void popFront() { s_.pop_front(); } 00154 void pop_front() { popFront(); } 00155 // @swap Swaps two lists. 00156 // @complexity ~O( 1 ) 00157 void swap(Queue& rhs) // throw () 00158 { 00159 s_.swap( rhs.s_ ); 00160 } 00161 // Implementation 00162 private: 00163 ContainerType s_; 00164 }; 00165 00166 inline Queue extractRange(Queue& src, const Queue::FragmentType& match) 00167 { 00168 Queue result; 00169 for( Queue::Iterator it = src.begin(); it != src.end(); ) 00170 { 00171 if( it->begin() < match.end() && it->end() > match.begin() ) 00172 { 00173 result.pushBack( *it ); 00174 it = src.erase( it ); 00175 } 00176 else 00177 { 00178 ++it; 00179 } 00180 } 00181 return result; 00182 } 00183 00184 } // namespace types 00185 00186 #endif // #ifndef FILEFRAGMENTS_QUEUE_HPP_INCLUDED