37 #if 1 || defined(__STDC__) || defined(__ANSI_CPP__)
38 #define __ListItr(List) List##_Iterator
39 #define ListItr(List) __ListItr(List)
40 #define __ListUpdater(List) List##_Updater
41 #define ListUpdater(List) __ListUpdater(List)
43 #define __ListItr(List) List_Iterator
44 #define ListItr(List) __ListItr(List)
45 #define __ListUpdater(List) List_Updater
46 #define ListUpdater(List) __ListUpdater(List)
49 #define declareList(List,T) \
52 List(long size = 0); \
56 T item(long index) const; \
57 T& item_ref(long index) const; \
59 void prepend(const T&); \
60 void append(const T&); \
61 void insert(long index, const T&); \
62 void remove(long index); \
71 inline long List::count() const { return count_; } \
73 inline T List::item(long index) const { \
74 if (index < 0 || index >= count_) { \
75 ListImpl_range_error(index); \
77 long i = index < free_ ? index : index + size_ - count_; \
80 inline T& List::item_ref(long index) const { \
81 if (index < 0 || index >= count_) { \
82 ListImpl_range_error(index); \
84 long i = index < free_ ? index : index + size_ - count_; \
88 inline void List::append(const T& item) { insert(count_, item); } \
89 inline void List::prepend(const T& item) { insert(0, item); } \
91 class ListItr(List) { \
93 ListItr(List)(const List&); \
104 inline bool ListItr(List)::more() const { return cur_ < list_->count(); } \
105 inline T ListItr(List)::cur() const { return list_->item(cur_); } \
106 inline T& ListItr(List)::cur_ref() const { \
107 return list_->item_ref(cur_); \
109 inline void ListItr(List)::next() { ++cur_; } \
111 class ListUpdater(List) { \
113 ListUpdater(List)(List&); \
117 T& cur_ref() const; \
125 inline bool ListUpdater(List)::more() const { \
126 return cur_ < list_->count(); \
128 inline T ListUpdater(List)::cur() const { return list_->item(cur_); } \
129 inline T& ListUpdater(List)::cur_ref() const { \
130 return list_->item_ref(cur_); \
132 inline void ListUpdater(List)::remove_cur() { list_->remove(cur_); } \
133 inline void ListUpdater(List)::next() { ++cur_; }
147 #define declarePtrList(PtrList,T) \
150 PtrList(long size = 0); \
152 long count() const; \
153 T* item(long index) const; \
157 void insert(long index, T*); \
158 void remove(long index); \
161 __AnyPtrList impl_; \
164 inline PtrList::PtrList(long size) : impl_(size) { } \
165 inline long PtrList::count() const { return impl_.count(); } \
166 inline T* PtrList::item(long index) const { return (T*)impl_.item(index); } \
167 inline void PtrList::append(T* item) { insert(impl_.count(), item); } \
168 inline void PtrList::prepend(T* item) { insert(0, item); } \
169 inline void PtrList::remove(long index) { impl_.remove(index); } \
170 inline void PtrList::remove_all() { impl_.remove_all(); } \
172 class ListItr(PtrList) { \
174 ListItr(PtrList)(const PtrList&); \
180 const PtrList* list_; \
184 inline bool ListItr(PtrList)::more() const { \
185 return cur_ < list_->count(); \
187 inline T* ListItr(PtrList)::cur() const { return list_->item(cur_); } \
188 inline void ListItr(PtrList)::next() { ++cur_; } \
190 class ListUpdater(PtrList) { \
192 ListUpdater(PtrList)(PtrList&); \
203 inline bool ListUpdater(PtrList)::more() const { \
204 return cur_ < list_->count(); \
206 inline T* ListUpdater(PtrList)::cur() const { return list_->item(cur_); } \
207 inline void ListUpdater(PtrList)::remove_cur() { list_->remove(cur_); } \
208 inline void ListUpdater(PtrList)::next() { ++cur_; }
214 #define implementList(List,T) \
215 List::List(long size) { \
217 size_ = ListImpl_best_new_count(size, sizeof(T)); \
218 items_ = new T[size_]; \
231 void List::insert(long index, const T& item) { \
232 if (count_ == size_) { \
233 long size = ListImpl_best_new_count(size_ + 1, sizeof(T), 2); \
234 T* items = new T[size]; \
237 for (i = 0; i < free_; ++i) { \
238 items[i] = items_[i]; \
240 for (i = 0; i < count_ - free_; ++i) { \
241 items[free_ + size - count_ + i] = \
242 items_[free_ + size_ - count_ + i]; \
249 if (index >= 0 && index <= count_) { \
250 if (index < free_) { \
251 for (long i = free_ - index - 1; i >= 0; --i) { \
252 items_[index + size_ - count_ + i] = items_[index + i]; \
254 } else if (index > free_) { \
255 for (long i = 0; i < index - free_; ++i) { \
256 items_[free_ + i] = items_[free_ + size_ - count_ + i]; \
261 items_[index] = item; \
265 void List::remove(long index) { \
266 if (index >= 0 && index <= count_) { \
267 if (index < free_) { \
268 for (long i = free_ - index - 2; i >= 0; --i) { \
269 items_[size_ - count_ + index + 1 + i] = \
270 items_[index + 1 + i]; \
272 } else if (index > free_) { \
273 for (long i = 0; i < index - free_; ++i) { \
274 items_[free_ + i] = items_[free_ + size_ - count_ + i]; \
282 void List::remove_all() { \
287 ListItr(List)::ListItr(List)(const List& list) { \
292 ListUpdater(List)::ListUpdater(List)(List& list) { \
297 #define implementPtrList(PtrList,T) \
298 void PtrList::insert(long index, T* item) { \
299 const __AnyPtr p = item; \
300 impl_.insert(index, p); \
302 ListItr(PtrList)::ListItr(PtrList)(const PtrList& list) { \
307 ListUpdater(PtrList)::ListUpdater(PtrList)(PtrList& list) { \
long ListImpl_best_new_count(long count, unsigned int size, unsigned int m=1)
#define declareList(List, T)
void ListImpl_range_error(long index)