adevs
|
00001 /*************** 00002 Copyright (C) 2000-2006 by James Nutaro 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this library; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 Bugs, comments, and questions can be sent to nutaro@gmail.com 00019 ***************/ 00020 #ifndef _adevs_bag_h 00021 #define _adevs_bag_h 00022 #include <cstdlib> 00023 00024 namespace adevs 00025 { 00026 00034 template <class T> class Bag 00035 { 00036 public: 00038 class iterator 00039 { 00040 public: 00041 iterator(unsigned int start = 0, T* b = NULL): 00042 i(start),b(b){} 00043 iterator(const iterator& src): 00044 i(src.i),b(src.b){} 00045 const iterator& operator=(const iterator& src) 00046 { 00047 i = src.i; 00048 b = src.b; 00049 return *this; 00050 } 00051 bool operator==(const iterator& src) const { return i==src.i; } 00052 bool operator!=(const iterator& src) const { return i!=src.i; } 00053 T& operator*() { return b[i]; } 00054 const T& operator*() const { return b[i]; } 00055 iterator& operator++() { i++; return *this; } 00056 iterator& operator--() { i--; return *this; } 00057 iterator& operator++(int) { ++i; return *this; } 00058 iterator& operator--(int) { --i; return *this; } 00059 private: 00060 friend class Bag<T>; 00061 unsigned int i; 00062 T* b; 00063 }; 00064 typedef iterator const_iterator; 00066 Bag(unsigned int cap = 8): 00067 cap_(cap),size_(0),b(new T[cap]){} 00069 Bag(const Bag<T>& src): 00070 cap_(src.cap_), 00071 size_(src.size_) 00072 { 00073 b = new T[src.cap_]; 00074 for (unsigned int i = 0; i < size_; i++) 00075 b[i] = src.b[i]; 00076 } 00078 const Bag<T>& operator=(const Bag<T>& src) 00079 { 00080 cap_ = src.cap_; 00081 size_ = src.size_; 00082 delete [] b; 00083 b = new T[src.cap_]; 00084 for (unsigned int i = 0; i < size_; i++) 00085 b[i] = src.b[i]; 00086 return *this; 00087 } 00089 unsigned count(const T& a) const 00090 { 00091 unsigned result = 0; 00092 for (unsigned i = 0; i < size_; i++) 00093 if (b[i] == a) result++; 00094 return result; 00095 } 00097 unsigned size() const { return size_; } 00099 bool empty() const { return size_ == 0; } 00101 iterator begin() const { return iterator(0,b); } 00103 iterator end() const { return iterator(size_,b); } 00105 void erase(const T& k) 00106 { 00107 iterator p = find(k); 00108 if (p != end()) erase(p); 00109 } 00111 void erase(iterator p) 00112 { 00113 size_--; 00114 b[p.i] = b[size_]; 00115 } 00117 void clear() { size_ = 0; } 00119 iterator find(const T& k) const 00120 { 00121 for (unsigned i = 0; i < size_; i++) 00122 if (b[i] == k) return iterator(i,b); 00123 return end(); 00124 } 00126 void insert(const T& t) 00127 { 00128 if (cap_ == size_) enlarge(2*cap_); 00129 b[size_] = t; 00130 size_++; 00131 } 00132 ~Bag() { delete [] b; } 00133 private: 00134 unsigned cap_, size_; 00135 T* b; 00137 void enlarge(unsigned adjustment) 00138 { 00139 cap_ = cap_ + adjustment; 00140 T* rb = new T[cap_]; 00141 for (unsigned i = 0; i < size_; i++) 00142 rb[i] = b[i]; 00143 delete [] b; 00144 b = rb; 00145 } 00146 }; 00147 00148 } // end of namespace 00149 00150 #endif