1915a7b8cSOliver Tappe /*
2915a7b8cSOliver Tappe Open Tracker License
3915a7b8cSOliver Tappe
4915a7b8cSOliver Tappe Terms and Conditions
5915a7b8cSOliver Tappe
6915a7b8cSOliver Tappe Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7915a7b8cSOliver Tappe
8915a7b8cSOliver Tappe Permission is hereby granted, free of charge, to any person obtaining a copy of
9915a7b8cSOliver Tappe this software and associated documentation files (the "Software"), to deal in
10915a7b8cSOliver Tappe the Software without restriction, including without limitation the rights to
11915a7b8cSOliver Tappe use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12915a7b8cSOliver Tappe of the Software, and to permit persons to whom the Software is furnished to do
13915a7b8cSOliver Tappe so, subject to the following conditions:
14915a7b8cSOliver Tappe
15915a7b8cSOliver Tappe The above copyright notice and this permission notice applies to all licensees
16915a7b8cSOliver Tappe and shall be included in all copies or substantial portions of the Software.
17915a7b8cSOliver Tappe
18915a7b8cSOliver Tappe THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19915a7b8cSOliver Tappe IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20915a7b8cSOliver Tappe FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21915a7b8cSOliver Tappe BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22915a7b8cSOliver Tappe AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23915a7b8cSOliver Tappe WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24915a7b8cSOliver Tappe
25915a7b8cSOliver Tappe Except as contained in this notice, the name of Be Incorporated shall not be
26915a7b8cSOliver Tappe used in advertising or otherwise to promote the sale, use or other dealings in
27915a7b8cSOliver Tappe this Software without prior written authorization from Be Incorporated.
28915a7b8cSOliver Tappe
29915a7b8cSOliver Tappe Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30915a7b8cSOliver Tappe of Be Incorporated in the United States and other countries. Other brand product
31915a7b8cSOliver Tappe names are registered trademarks or trademarks of their respective holders.
32915a7b8cSOliver Tappe All rights reserved.
33915a7b8cSOliver Tappe */
34915a7b8cSOliver Tappe #ifndef _OBJECT_LIST_H
35915a7b8cSOliver Tappe #define _OBJECT_LIST_H
36915a7b8cSOliver Tappe
37915a7b8cSOliver Tappe
38915a7b8cSOliver Tappe #include <new>
39915a7b8cSOliver Tappe
40915a7b8cSOliver Tappe #include <List.h>
41915a7b8cSOliver Tappe #include <SupportDefs.h>
42915a7b8cSOliver Tappe
43915a7b8cSOliver Tappe
44915a7b8cSOliver Tappe //
45915a7b8cSOliver Tappe // ObjectList is a wrapper around BList that adds type safety,
46915a7b8cSOliver Tappe // optional object ownership, search, insert operations, etc.
47915a7b8cSOliver Tappe //
48915a7b8cSOliver Tappe
49915a7b8cSOliver Tappe template<class T> class BObjectList;
50915a7b8cSOliver Tappe
51915a7b8cSOliver Tappe
52915a7b8cSOliver Tappe template<class T>
53915a7b8cSOliver Tappe struct UnaryPredicate {
operatorUnaryPredicate54915a7b8cSOliver Tappe virtual int operator()(const T *) const
55915a7b8cSOliver Tappe // virtual could be avoided here if FindBinaryInsertionIndex,
56915a7b8cSOliver Tappe // etc. were member template functions
57*29e8fa59SJohn Scipione {
58*29e8fa59SJohn Scipione return 0;
59*29e8fa59SJohn Scipione }
60915a7b8cSOliver Tappe
61915a7b8cSOliver Tappe private:
62915a7b8cSOliver Tappe static int _unary_predicate_glue(const void *item, void *context);
63915a7b8cSOliver Tappe
64915a7b8cSOliver Tappe friend class BObjectList<T>;
65915a7b8cSOliver Tappe };
66915a7b8cSOliver Tappe
67915a7b8cSOliver Tappe
68915a7b8cSOliver Tappe template<class T>
69915a7b8cSOliver Tappe int
_unary_predicate_glue(const void * item,void * context)70915a7b8cSOliver Tappe UnaryPredicate<T>::_unary_predicate_glue(const void *item, void *context)
71915a7b8cSOliver Tappe {
72915a7b8cSOliver Tappe return ((UnaryPredicate<T> *)context)->operator()((const T *)item);
73915a7b8cSOliver Tappe }
74915a7b8cSOliver Tappe
75915a7b8cSOliver Tappe
76915a7b8cSOliver Tappe class _PointerList_ : public BList {
77915a7b8cSOliver Tappe public:
78915a7b8cSOliver Tappe _PointerList_(const _PointerList_ &list);
79915a7b8cSOliver Tappe _PointerList_(int32 itemsPerBlock = 20, bool owning = false);
80915a7b8cSOliver Tappe ~_PointerList_();
81915a7b8cSOliver Tappe
82915a7b8cSOliver Tappe typedef void *(* GenericEachFunction)(void *, void *);
83915a7b8cSOliver Tappe typedef int (* GenericCompareFunction)(const void *, const void *);
84915a7b8cSOliver Tappe typedef int (* GenericCompareFunctionWithState)(const void *, const void *,
85915a7b8cSOliver Tappe void *);
86915a7b8cSOliver Tappe typedef int (* UnaryPredicateGlue)(const void *, void *);
87915a7b8cSOliver Tappe
88915a7b8cSOliver Tappe void *EachElement(GenericEachFunction, void *);
89915a7b8cSOliver Tappe void SortItems(GenericCompareFunction);
90915a7b8cSOliver Tappe void SortItems(GenericCompareFunctionWithState, void *state);
91915a7b8cSOliver Tappe void HSortItems(GenericCompareFunction);
92915a7b8cSOliver Tappe void HSortItems(GenericCompareFunctionWithState, void *state);
93915a7b8cSOliver Tappe
94915a7b8cSOliver Tappe void *BinarySearch(const void *, GenericCompareFunction) const;
95915a7b8cSOliver Tappe void *BinarySearch(const void *, GenericCompareFunctionWithState,
96915a7b8cSOliver Tappe void *state) const;
97915a7b8cSOliver Tappe
98915a7b8cSOliver Tappe int32 BinarySearchIndex(const void *, GenericCompareFunction) const;
99915a7b8cSOliver Tappe int32 BinarySearchIndex(const void *, GenericCompareFunctionWithState,
100915a7b8cSOliver Tappe void *state) const;
101915a7b8cSOliver Tappe int32 BinarySearchIndexByPredicate(const void *, UnaryPredicateGlue) const;
102915a7b8cSOliver Tappe
103915a7b8cSOliver Tappe bool Owning() const;
104915a7b8cSOliver Tappe bool ReplaceItem(int32, void *);
105f7953fa7SClemens Zeidler bool MoveItem(int32 from, int32 to);
106915a7b8cSOliver Tappe
107915a7b8cSOliver Tappe protected:
108915a7b8cSOliver Tappe bool owning;
109915a7b8cSOliver Tappe
110915a7b8cSOliver Tappe };
111915a7b8cSOliver Tappe
112915a7b8cSOliver Tappe
113915a7b8cSOliver Tappe template<class T>
114915a7b8cSOliver Tappe class BObjectList : private _PointerList_ {
115915a7b8cSOliver Tappe public:
116915a7b8cSOliver Tappe // iteration and sorting
117915a7b8cSOliver Tappe typedef T* (*EachFunction)(T*, void*);
118915a7b8cSOliver Tappe typedef const T* (*ConstEachFunction)(const T*, void*);
119915a7b8cSOliver Tappe typedef int (*CompareFunction)(const T*, const T*);
120915a7b8cSOliver Tappe typedef int (*CompareFunctionWithState)(const T*, const T*,
121915a7b8cSOliver Tappe void* state);
122915a7b8cSOliver Tappe
123915a7b8cSOliver Tappe BObjectList(int32 itemsPerBlock = 20,
124915a7b8cSOliver Tappe bool owning = false);
125915a7b8cSOliver Tappe BObjectList(const BObjectList& list);
126915a7b8cSOliver Tappe // clones list; if list is owning, makes
127915a7b8cSOliver Tappe // copies of all the items
128915a7b8cSOliver Tappe
129915a7b8cSOliver Tappe virtual ~BObjectList();
130915a7b8cSOliver Tappe
131915a7b8cSOliver Tappe BObjectList& operator=(const BObjectList& list);
132915a7b8cSOliver Tappe // clones list; if list is owning, makes
133915a7b8cSOliver Tappe // copies of all the items
134915a7b8cSOliver Tappe
135915a7b8cSOliver Tappe // adding and removing
136915a7b8cSOliver Tappe bool AddItem(T*);
137915a7b8cSOliver Tappe bool AddItem(T*, int32);
138915a7b8cSOliver Tappe bool AddList(BObjectList*);
139915a7b8cSOliver Tappe bool AddList(BObjectList*, int32);
140915a7b8cSOliver Tappe
141915a7b8cSOliver Tappe bool RemoveItem(T*, bool deleteIfOwning = true);
142915a7b8cSOliver Tappe // if owning, deletes the removed item
143915a7b8cSOliver Tappe T* RemoveItemAt(int32);
144915a7b8cSOliver Tappe // returns the removed item
145915a7b8cSOliver Tappe
146915a7b8cSOliver Tappe void MakeEmpty(bool deleteIfOwning = true);
147915a7b8cSOliver Tappe
148915a7b8cSOliver Tappe // item access
149915a7b8cSOliver Tappe T* ItemAt(int32) const;
150915a7b8cSOliver Tappe
151915a7b8cSOliver Tappe bool ReplaceItem(int32 index, T*);
152915a7b8cSOliver Tappe // if list is owning, deletes the item
153915a7b8cSOliver Tappe // at <index> first
154915a7b8cSOliver Tappe T* SwapWithItem(int32 index, T* newItem);
155915a7b8cSOliver Tappe // same as ReplaceItem, except does not
156915a7b8cSOliver Tappe // delete old item at <index>, returns it
157915a7b8cSOliver Tappe // instead
158f7953fa7SClemens Zeidler bool MoveItem(int32 from, int32 to);
159915a7b8cSOliver Tappe
160915a7b8cSOliver Tappe T* FirstItem() const;
161915a7b8cSOliver Tappe T* LastItem() const;
162915a7b8cSOliver Tappe
163915a7b8cSOliver Tappe // misc. getters
164915a7b8cSOliver Tappe int32 IndexOf(const T*) const;
165915a7b8cSOliver Tappe bool HasItem(const T*) const;
166915a7b8cSOliver Tappe bool IsEmpty() const;
167915a7b8cSOliver Tappe int32 CountItems() const;
168915a7b8cSOliver Tappe
169915a7b8cSOliver Tappe T* EachElement(EachFunction, void*);
170915a7b8cSOliver Tappe const T* EachElement(ConstEachFunction, void*) const;
171915a7b8cSOliver Tappe
172915a7b8cSOliver Tappe void SortItems(CompareFunction);
173915a7b8cSOliver Tappe void SortItems(CompareFunctionWithState,
174915a7b8cSOliver Tappe void* state);
175915a7b8cSOliver Tappe void HSortItems(CompareFunction);
176915a7b8cSOliver Tappe void HSortItems(CompareFunctionWithState,
177915a7b8cSOliver Tappe void* state);
178915a7b8cSOliver Tappe
179915a7b8cSOliver Tappe // linear search, returns first item that
180915a7b8cSOliver Tappe // matches predicate
181915a7b8cSOliver Tappe const T* FindIf(const UnaryPredicate<T>&) const;
182915a7b8cSOliver Tappe T* FindIf(const UnaryPredicate<T>&);
183915a7b8cSOliver Tappe
184915a7b8cSOliver Tappe // list must be sorted with CompareFunction for
185915a7b8cSOliver Tappe // these to work
186915a7b8cSOliver Tappe T* BinarySearch(const T&, CompareFunction) const;
187915a7b8cSOliver Tappe T* BinarySearch(const T&, CompareFunctionWithState,
188915a7b8cSOliver Tappe void* state) const;
189915a7b8cSOliver Tappe
190915a7b8cSOliver Tappe template<typename Key>
191915a7b8cSOliver Tappe T* BinarySearchByKey(const Key& key,
192915a7b8cSOliver Tappe int (*compare)(const Key*, const T*)) const;
193915a7b8cSOliver Tappe
194915a7b8cSOliver Tappe template<typename Key>
195915a7b8cSOliver Tappe T* BinarySearchByKey(const Key& key,
196915a7b8cSOliver Tappe int (*compare)(const Key*, const T*, void*),
197915a7b8cSOliver Tappe void* state) const;
198915a7b8cSOliver Tappe
199915a7b8cSOliver Tappe int32 BinarySearchIndex(const T&item,
200915a7b8cSOliver Tappe CompareFunction compare) const;
201915a7b8cSOliver Tappe int32 BinarySearchIndex(const T&item,
202915a7b8cSOliver Tappe CompareFunctionWithState compare,
203915a7b8cSOliver Tappe void* state) const;
204915a7b8cSOliver Tappe
205915a7b8cSOliver Tappe template<typename Key>
206915a7b8cSOliver Tappe int32 BinarySearchIndexByKey(const Key& key,
207915a7b8cSOliver Tappe int (*compare)(const Key*, const T*)) const;
208915a7b8cSOliver Tappe
209915a7b8cSOliver Tappe // Binary insertion - list must be sorted with
210915a7b8cSOliver Tappe // CompareFunction for these to work
211915a7b8cSOliver Tappe
212915a7b8cSOliver Tappe // simple insert
213915a7b8cSOliver Tappe bool BinaryInsert(T*, CompareFunction);
214915a7b8cSOliver Tappe bool BinaryInsert(T*, CompareFunctionWithState,
215915a7b8cSOliver Tappe void* state);
216915a7b8cSOliver Tappe bool BinaryInsert(T*, const UnaryPredicate<T>&);
217915a7b8cSOliver Tappe
218915a7b8cSOliver Tappe // unique insert, returns false if item already
219915a7b8cSOliver Tappe // in list
220915a7b8cSOliver Tappe bool BinaryInsertUnique(T*, CompareFunction);
221915a7b8cSOliver Tappe bool BinaryInsertUnique(T*, CompareFunctionWithState,
222915a7b8cSOliver Tappe void* state);
223915a7b8cSOliver Tappe bool BinaryInsertUnique(T*,
224915a7b8cSOliver Tappe const UnaryPredicate<T>&);
225915a7b8cSOliver Tappe
226915a7b8cSOliver Tappe // insert a copy of the item, returns new
227915a7b8cSOliver Tappe // inserted item
228915a7b8cSOliver Tappe T* BinaryInsertCopy(const T& copyThis,
229915a7b8cSOliver Tappe CompareFunction);
230915a7b8cSOliver Tappe T* BinaryInsertCopy(const T& copyThis,
231915a7b8cSOliver Tappe CompareFunctionWithState, void* state);
232915a7b8cSOliver Tappe
233915a7b8cSOliver Tappe // insert a copy of the item if not in list
234915a7b8cSOliver Tappe // already returns new inserted item or existing
235915a7b8cSOliver Tappe // item in case of a conflict
236915a7b8cSOliver Tappe T* BinaryInsertCopyUnique(const T& copyThis,
237915a7b8cSOliver Tappe CompareFunction);
238915a7b8cSOliver Tappe T* BinaryInsertCopyUnique(const T& copyThis,
239915a7b8cSOliver Tappe CompareFunctionWithState, void* state);
240915a7b8cSOliver Tappe
241915a7b8cSOliver Tappe int32 FindBinaryInsertionIndex(
242915a7b8cSOliver Tappe const UnaryPredicate<T>&,
243915a7b8cSOliver Tappe bool* alreadyInList = 0) const;
244915a7b8cSOliver Tappe // returns either the index into which a new
245915a7b8cSOliver Tappe // item should be inserted or index of an
246915a7b8cSOliver Tappe // existing item that matches the predicate
247915a7b8cSOliver Tappe
248915a7b8cSOliver Tappe class Private;
249915a7b8cSOliver Tappe private:
250915a7b8cSOliver Tappe friend class Private;
251915a7b8cSOliver Tappe
252915a7b8cSOliver Tappe void _SetItem(int32, T*);
253915a7b8cSOliver Tappe };
254915a7b8cSOliver Tappe
255915a7b8cSOliver Tappe
256915a7b8cSOliver Tappe template<class Item, class Result, class Param1>
257915a7b8cSOliver Tappe Result
WhileEachListItem(BObjectList<Item> * list,Result (Item::* func)(Param1),Param1 p1)258915a7b8cSOliver Tappe WhileEachListItem(BObjectList<Item>* list, Result (Item::*func)(Param1),
259915a7b8cSOliver Tappe Param1 p1)
260915a7b8cSOliver Tappe {
261915a7b8cSOliver Tappe Result result = 0;
262915a7b8cSOliver Tappe int32 count = list->CountItems();
263915a7b8cSOliver Tappe
264915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
265915a7b8cSOliver Tappe if ((result = (list->ItemAt(index)->*func)(p1)) != 0)
266915a7b8cSOliver Tappe break;
267915a7b8cSOliver Tappe }
268915a7b8cSOliver Tappe
269915a7b8cSOliver Tappe return result;
270915a7b8cSOliver Tappe }
271915a7b8cSOliver Tappe
272915a7b8cSOliver Tappe
273915a7b8cSOliver Tappe template<class Item, class Result, class Param1>
274915a7b8cSOliver Tappe Result
WhileEachListItem(BObjectList<Item> * list,Result (* func)(Item *,Param1),Param1 p1)275915a7b8cSOliver Tappe WhileEachListItem(BObjectList<Item>* list, Result (*func)(Item*, Param1),
276915a7b8cSOliver Tappe Param1 p1)
277915a7b8cSOliver Tappe {
278915a7b8cSOliver Tappe Result result = 0;
279915a7b8cSOliver Tappe int32 count = list->CountItems();
280915a7b8cSOliver Tappe
281915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
282915a7b8cSOliver Tappe if ((result = (*func)(list->ItemAt(index), p1)) != 0)
283915a7b8cSOliver Tappe break;
284915a7b8cSOliver Tappe }
285915a7b8cSOliver Tappe
286915a7b8cSOliver Tappe return result;
287915a7b8cSOliver Tappe }
288915a7b8cSOliver Tappe
289915a7b8cSOliver Tappe
290915a7b8cSOliver Tappe template<class Item, class Result, class Param1, class Param2>
291915a7b8cSOliver Tappe Result
WhileEachListItem(BObjectList<Item> * list,Result (Item::* func)(Param1,Param2),Param1 p1,Param2 p2)292915a7b8cSOliver Tappe WhileEachListItem(BObjectList<Item>* list, Result (Item::*func)(Param1, Param2),
293915a7b8cSOliver Tappe Param1 p1, Param2 p2)
294915a7b8cSOliver Tappe {
295915a7b8cSOliver Tappe Result result = 0;
296915a7b8cSOliver Tappe int32 count = list->CountItems();
297915a7b8cSOliver Tappe
298915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
299915a7b8cSOliver Tappe if ((result = (list->ItemAt(index)->*func)(p1, p2)) != 0)
300915a7b8cSOliver Tappe break;
301915a7b8cSOliver Tappe }
302915a7b8cSOliver Tappe
303915a7b8cSOliver Tappe return result;
304915a7b8cSOliver Tappe }
305915a7b8cSOliver Tappe
306915a7b8cSOliver Tappe
307915a7b8cSOliver Tappe template<class Item, class Result, class Param1, class Param2>
308915a7b8cSOliver Tappe Result
WhileEachListItem(BObjectList<Item> * list,Result (* func)(Item *,Param1,Param2),Param1 p1,Param2 p2)309915a7b8cSOliver Tappe WhileEachListItem(BObjectList<Item>* list,
310915a7b8cSOliver Tappe Result (*func)(Item*, Param1, Param2), Param1 p1, Param2 p2)
311915a7b8cSOliver Tappe {
312915a7b8cSOliver Tappe Result result = 0;
313915a7b8cSOliver Tappe int32 count = list->CountItems();
314915a7b8cSOliver Tappe
315915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
316915a7b8cSOliver Tappe if ((result = (*func)(list->ItemAt(index), p1, p2)) != 0)
317915a7b8cSOliver Tappe break;
318915a7b8cSOliver Tappe }
319915a7b8cSOliver Tappe
320915a7b8cSOliver Tappe return result;
321915a7b8cSOliver Tappe }
322915a7b8cSOliver Tappe
323915a7b8cSOliver Tappe
324915a7b8cSOliver Tappe template<class Item, class Result, class Param1, class Param2, class Param3,
325915a7b8cSOliver Tappe class Param4>
326915a7b8cSOliver Tappe Result
WhileEachListItem(BObjectList<Item> * list,Result (* func)(Item *,Param1,Param2,Param3,Param4),Param1 p1,Param2 p2,Param3 p3,Param4 p4)327915a7b8cSOliver Tappe WhileEachListItem(BObjectList<Item>* list,
328915a7b8cSOliver Tappe Result (*func)(Item*, Param1, Param2, Param3, Param4), Param1 p1, Param2 p2,
329915a7b8cSOliver Tappe Param3 p3, Param4 p4)
330915a7b8cSOliver Tappe {
331915a7b8cSOliver Tappe Result result = 0;
332915a7b8cSOliver Tappe int32 count = list->CountItems();
333915a7b8cSOliver Tappe
334915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
335915a7b8cSOliver Tappe if ((result = (*func)(list->ItemAt(index), p1, p2, p3, p4)) != 0)
336915a7b8cSOliver Tappe break;
337915a7b8cSOliver Tappe }
338915a7b8cSOliver Tappe
339915a7b8cSOliver Tappe return result;
340915a7b8cSOliver Tappe }
341915a7b8cSOliver Tappe
342915a7b8cSOliver Tappe
343915a7b8cSOliver Tappe template<class Item, class Result>
344915a7b8cSOliver Tappe void
EachListItemIgnoreResult(BObjectList<Item> * list,Result (Item::* func)())345915a7b8cSOliver Tappe EachListItemIgnoreResult(BObjectList<Item>* list, Result (Item::*func)())
346915a7b8cSOliver Tappe {
347915a7b8cSOliver Tappe int32 count = list->CountItems();
348915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++)
349915a7b8cSOliver Tappe (list->ItemAt(index)->*func)();
350915a7b8cSOliver Tappe }
351915a7b8cSOliver Tappe
352915a7b8cSOliver Tappe
353915a7b8cSOliver Tappe template<class Item, class Param1>
354915a7b8cSOliver Tappe void
EachListItem(BObjectList<Item> * list,void (* func)(Item *,Param1),Param1 p1)355915a7b8cSOliver Tappe EachListItem(BObjectList<Item>* list, void (*func)(Item*, Param1), Param1 p1)
356915a7b8cSOliver Tappe {
357915a7b8cSOliver Tappe int32 count = list->CountItems();
358915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++)
359915a7b8cSOliver Tappe (func)(list->ItemAt(index), p1);
360915a7b8cSOliver Tappe }
361915a7b8cSOliver Tappe
362915a7b8cSOliver Tappe
363915a7b8cSOliver Tappe template<class Item, class Param1, class Param2>
364915a7b8cSOliver Tappe void
EachListItem(BObjectList<Item> * list,void (Item::* func)(Param1,Param2),Param1 p1,Param2 p2)365915a7b8cSOliver Tappe EachListItem(BObjectList<Item>* list, void (Item::*func)(Param1, Param2),
366915a7b8cSOliver Tappe Param1 p1, Param2 p2)
367915a7b8cSOliver Tappe {
368915a7b8cSOliver Tappe int32 count = list->CountItems();
369915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++)
370915a7b8cSOliver Tappe (list->ItemAt(index)->*func)(p1, p2);
371915a7b8cSOliver Tappe }
372915a7b8cSOliver Tappe
373915a7b8cSOliver Tappe
374915a7b8cSOliver Tappe template<class Item, class Param1, class Param2>
375915a7b8cSOliver Tappe void
EachListItem(BObjectList<Item> * list,void (* func)(Item *,Param1,Param2),Param1 p1,Param2 p2)376915a7b8cSOliver Tappe EachListItem(BObjectList<Item>* list, void (*func)(Item*,Param1, Param2),
377915a7b8cSOliver Tappe Param1 p1, Param2 p2)
378915a7b8cSOliver Tappe {
379915a7b8cSOliver Tappe int32 count = list->CountItems();
380915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++)
381915a7b8cSOliver Tappe (func)(list->ItemAt(index), p1, p2);
382915a7b8cSOliver Tappe }
383915a7b8cSOliver Tappe
384915a7b8cSOliver Tappe
385915a7b8cSOliver Tappe template<class Item, class Param1, class Param2, class Param3>
386915a7b8cSOliver Tappe void
EachListItem(BObjectList<Item> * list,void (* func)(Item *,Param1,Param2,Param3),Param1 p1,Param2 p2,Param3 p3)387915a7b8cSOliver Tappe EachListItem(BObjectList<Item>* list,
388915a7b8cSOliver Tappe void (*func)(Item*,Param1, Param2, Param3), Param1 p1, Param2 p2, Param3 p3)
389915a7b8cSOliver Tappe {
390915a7b8cSOliver Tappe int32 count = list->CountItems();
391915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++)
392915a7b8cSOliver Tappe (func)(list->ItemAt(index), p1, p2, p3);
393915a7b8cSOliver Tappe }
394915a7b8cSOliver Tappe
395915a7b8cSOliver Tappe
396915a7b8cSOliver Tappe template<class Item, class Param1, class Param2, class Param3, class Param4>
397915a7b8cSOliver Tappe void
EachListItem(BObjectList<Item> * list,void (* func)(Item *,Param1,Param2,Param3,Param4),Param1 p1,Param2 p2,Param3 p3,Param4 p4)398915a7b8cSOliver Tappe EachListItem(BObjectList<Item>* list,
399915a7b8cSOliver Tappe void (*func)(Item*,Param1, Param2, Param3, Param4), Param1 p1, Param2 p2,
400915a7b8cSOliver Tappe Param3 p3, Param4 p4)
401915a7b8cSOliver Tappe {
402915a7b8cSOliver Tappe int32 count = list->CountItems();
403915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++)
404915a7b8cSOliver Tappe (func)(list->ItemAt(index), p1, p2, p3, p4);
405915a7b8cSOliver Tappe }
406915a7b8cSOliver Tappe
407915a7b8cSOliver Tappe
408915a7b8cSOliver Tappe // inline code
409915a7b8cSOliver Tappe
410915a7b8cSOliver Tappe
411915a7b8cSOliver Tappe inline bool
Owning()412915a7b8cSOliver Tappe _PointerList_::Owning() const
413915a7b8cSOliver Tappe {
414915a7b8cSOliver Tappe return owning;
415915a7b8cSOliver Tappe }
416915a7b8cSOliver Tappe
417915a7b8cSOliver Tappe
418915a7b8cSOliver Tappe template<class T>
BObjectList(int32 itemsPerBlock,bool owning)419915a7b8cSOliver Tappe BObjectList<T>::BObjectList(int32 itemsPerBlock, bool owning)
420915a7b8cSOliver Tappe :
421915a7b8cSOliver Tappe _PointerList_(itemsPerBlock, owning)
422915a7b8cSOliver Tappe {
423915a7b8cSOliver Tappe }
424915a7b8cSOliver Tappe
425915a7b8cSOliver Tappe
426915a7b8cSOliver Tappe template<class T>
BObjectList(const BObjectList<T> & list)427915a7b8cSOliver Tappe BObjectList<T>::BObjectList(const BObjectList<T>& list)
428915a7b8cSOliver Tappe :
429915a7b8cSOliver Tappe _PointerList_(list)
430915a7b8cSOliver Tappe {
431915a7b8cSOliver Tappe owning = list.owning;
432915a7b8cSOliver Tappe if (owning) {
433915a7b8cSOliver Tappe // make our own copies in an owning list
434915a7b8cSOliver Tappe int32 count = list.CountItems();
435915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
436915a7b8cSOliver Tappe T* item = list.ItemAt(index);
437915a7b8cSOliver Tappe if (item)
438915a7b8cSOliver Tappe item = new (std::nothrow) T(*item);
439915a7b8cSOliver Tappe _SetItem(index, item);
440915a7b8cSOliver Tappe }
441915a7b8cSOliver Tappe }
442915a7b8cSOliver Tappe }
443915a7b8cSOliver Tappe
444915a7b8cSOliver Tappe
445915a7b8cSOliver Tappe template<class T>
~BObjectList()446915a7b8cSOliver Tappe BObjectList<T>::~BObjectList()
447915a7b8cSOliver Tappe {
448915a7b8cSOliver Tappe if (Owning()) {
449915a7b8cSOliver Tappe // have to nuke elements first
450915a7b8cSOliver Tappe MakeEmpty();
451915a7b8cSOliver Tappe }
452915a7b8cSOliver Tappe }
453915a7b8cSOliver Tappe
454915a7b8cSOliver Tappe
455915a7b8cSOliver Tappe template<class T>
456915a7b8cSOliver Tappe BObjectList<T>&
457915a7b8cSOliver Tappe BObjectList<T>::operator=(const BObjectList<T>& list)
458915a7b8cSOliver Tappe {
459915a7b8cSOliver Tappe owning = list.owning;
460915a7b8cSOliver Tappe BObjectList<T> &result = (BObjectList<T>&)_PointerList_::operator=(list);
461915a7b8cSOliver Tappe if (owning) {
462915a7b8cSOliver Tappe // make our own copies in an owning list
463915a7b8cSOliver Tappe int32 count = list.CountItems();
464915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
465915a7b8cSOliver Tappe T* item = list.ItemAt(index);
466915a7b8cSOliver Tappe if (item)
467915a7b8cSOliver Tappe item = new (std::nothrow) T(*item);
468915a7b8cSOliver Tappe _SetItem(index, item);
469915a7b8cSOliver Tappe }
470915a7b8cSOliver Tappe }
471915a7b8cSOliver Tappe return result;
472915a7b8cSOliver Tappe }
473915a7b8cSOliver Tappe
474915a7b8cSOliver Tappe
475915a7b8cSOliver Tappe template<class T>
476915a7b8cSOliver Tappe bool
AddItem(T * item)477915a7b8cSOliver Tappe BObjectList<T>::AddItem(T* item)
478915a7b8cSOliver Tappe {
479915a7b8cSOliver Tappe // need to cast to void* to make T work for const pointers
480915a7b8cSOliver Tappe return _PointerList_::AddItem((void*)item);
481915a7b8cSOliver Tappe }
482915a7b8cSOliver Tappe
483915a7b8cSOliver Tappe
484915a7b8cSOliver Tappe template<class T>
485915a7b8cSOliver Tappe bool
AddItem(T * item,int32 index)486*29e8fa59SJohn Scipione BObjectList<T>::AddItem(T* item, int32 index)
487915a7b8cSOliver Tappe {
488*29e8fa59SJohn Scipione return _PointerList_::AddItem((void*)item, index);
489915a7b8cSOliver Tappe }
490915a7b8cSOliver Tappe
491915a7b8cSOliver Tappe
492915a7b8cSOliver Tappe template<class T>
493915a7b8cSOliver Tappe bool
AddList(BObjectList<T> * list)494*29e8fa59SJohn Scipione BObjectList<T>::AddList(BObjectList<T>* list)
495915a7b8cSOliver Tappe {
496*29e8fa59SJohn Scipione return _PointerList_::AddList(list);
497915a7b8cSOliver Tappe }
498915a7b8cSOliver Tappe
499915a7b8cSOliver Tappe
500915a7b8cSOliver Tappe template<class T>
501915a7b8cSOliver Tappe bool
AddList(BObjectList<T> * list,int32 index)502*29e8fa59SJohn Scipione BObjectList<T>::AddList(BObjectList<T>* list, int32 index)
503915a7b8cSOliver Tappe {
504*29e8fa59SJohn Scipione return _PointerList_::AddList(list, index);
505915a7b8cSOliver Tappe }
506915a7b8cSOliver Tappe
507915a7b8cSOliver Tappe
508915a7b8cSOliver Tappe template<class T>
509915a7b8cSOliver Tappe bool
RemoveItem(T * item,bool deleteIfOwning)510915a7b8cSOliver Tappe BObjectList<T>::RemoveItem(T* item, bool deleteIfOwning)
511915a7b8cSOliver Tappe {
512915a7b8cSOliver Tappe bool result = _PointerList_::RemoveItem((void*)item);
513915a7b8cSOliver Tappe
514915a7b8cSOliver Tappe if (result && Owning() && deleteIfOwning)
515915a7b8cSOliver Tappe delete item;
516915a7b8cSOliver Tappe
517915a7b8cSOliver Tappe return result;
518915a7b8cSOliver Tappe }
519915a7b8cSOliver Tappe
520915a7b8cSOliver Tappe
521915a7b8cSOliver Tappe template<class T>
522915a7b8cSOliver Tappe T*
RemoveItemAt(int32 index)523915a7b8cSOliver Tappe BObjectList<T>::RemoveItemAt(int32 index)
524915a7b8cSOliver Tappe {
525915a7b8cSOliver Tappe return (T*)_PointerList_::RemoveItem(index);
526915a7b8cSOliver Tappe }
527915a7b8cSOliver Tappe
528915a7b8cSOliver Tappe
529915a7b8cSOliver Tappe template<class T>
530915a7b8cSOliver Tappe inline T*
ItemAt(int32 index)531915a7b8cSOliver Tappe BObjectList<T>::ItemAt(int32 index) const
532915a7b8cSOliver Tappe {
533915a7b8cSOliver Tappe return (T*)_PointerList_::ItemAt(index);
534915a7b8cSOliver Tappe }
535915a7b8cSOliver Tappe
536915a7b8cSOliver Tappe
537915a7b8cSOliver Tappe template<class T>
538915a7b8cSOliver Tappe bool
ReplaceItem(int32 index,T * item)539915a7b8cSOliver Tappe BObjectList<T>::ReplaceItem(int32 index, T* item)
540915a7b8cSOliver Tappe {
541915a7b8cSOliver Tappe if (owning)
542915a7b8cSOliver Tappe delete ItemAt(index);
543*29e8fa59SJohn Scipione
544915a7b8cSOliver Tappe return _PointerList_::ReplaceItem(index, (void*)item);
545915a7b8cSOliver Tappe }
546915a7b8cSOliver Tappe
547915a7b8cSOliver Tappe
548915a7b8cSOliver Tappe template<class T>
549915a7b8cSOliver Tappe T*
SwapWithItem(int32 index,T * item)550*29e8fa59SJohn Scipione BObjectList<T>::SwapWithItem(int32 index, T* item)
551915a7b8cSOliver Tappe {
552915a7b8cSOliver Tappe T* result = ItemAt(index);
553*29e8fa59SJohn Scipione _PointerList_::ReplaceItem(index, (void*)item);
554*29e8fa59SJohn Scipione
555915a7b8cSOliver Tappe return result;
556915a7b8cSOliver Tappe }
557915a7b8cSOliver Tappe
558915a7b8cSOliver Tappe
559915a7b8cSOliver Tappe template<class T>
560f7953fa7SClemens Zeidler bool
MoveItem(int32 from,int32 to)561f7953fa7SClemens Zeidler BObjectList<T>::MoveItem(int32 from, int32 to)
562f7953fa7SClemens Zeidler {
563f7953fa7SClemens Zeidler return _PointerList_::MoveItem(from, to);
564f7953fa7SClemens Zeidler }
565f7953fa7SClemens Zeidler
566f7953fa7SClemens Zeidler
567f7953fa7SClemens Zeidler template<class T>
568915a7b8cSOliver Tappe void
_SetItem(int32 index,T * newItem)569915a7b8cSOliver Tappe BObjectList<T>::_SetItem(int32 index, T* newItem)
570915a7b8cSOliver Tappe {
571915a7b8cSOliver Tappe _PointerList_::ReplaceItem(index, (void*)newItem);
572915a7b8cSOliver Tappe }
573915a7b8cSOliver Tappe
574915a7b8cSOliver Tappe
575915a7b8cSOliver Tappe template<class T>
576915a7b8cSOliver Tappe int32
IndexOf(const T * item)577915a7b8cSOliver Tappe BObjectList<T>::IndexOf(const T* item) const
578915a7b8cSOliver Tappe {
579915a7b8cSOliver Tappe return _PointerList_::IndexOf((void*)item);
580915a7b8cSOliver Tappe }
581915a7b8cSOliver Tappe
582915a7b8cSOliver Tappe
583915a7b8cSOliver Tappe template<class T>
584915a7b8cSOliver Tappe T*
FirstItem()585915a7b8cSOliver Tappe BObjectList<T>::FirstItem() const
586915a7b8cSOliver Tappe {
587915a7b8cSOliver Tappe return (T*)_PointerList_::FirstItem();
588915a7b8cSOliver Tappe }
589915a7b8cSOliver Tappe
590915a7b8cSOliver Tappe
591915a7b8cSOliver Tappe template<class T>
592915a7b8cSOliver Tappe T*
LastItem()593915a7b8cSOliver Tappe BObjectList<T>::LastItem() const
594915a7b8cSOliver Tappe {
595915a7b8cSOliver Tappe return (T*)_PointerList_::LastItem();
596915a7b8cSOliver Tappe }
597915a7b8cSOliver Tappe
598915a7b8cSOliver Tappe
599915a7b8cSOliver Tappe template<class T>
600915a7b8cSOliver Tappe bool
HasItem(const T * item)601915a7b8cSOliver Tappe BObjectList<T>::HasItem(const T* item) const
602915a7b8cSOliver Tappe {
603915a7b8cSOliver Tappe return _PointerList_::HasItem((void*)item);
604915a7b8cSOliver Tappe }
605915a7b8cSOliver Tappe
606915a7b8cSOliver Tappe
607915a7b8cSOliver Tappe template<class T>
608915a7b8cSOliver Tappe bool
IsEmpty()609915a7b8cSOliver Tappe BObjectList<T>::IsEmpty() const
610915a7b8cSOliver Tappe {
611915a7b8cSOliver Tappe return _PointerList_::IsEmpty();
612915a7b8cSOliver Tappe }
613915a7b8cSOliver Tappe
614915a7b8cSOliver Tappe
615915a7b8cSOliver Tappe template<class T>
616915a7b8cSOliver Tappe int32
CountItems()617915a7b8cSOliver Tappe BObjectList<T>::CountItems() const
618915a7b8cSOliver Tappe {
619915a7b8cSOliver Tappe return _PointerList_::CountItems();
620915a7b8cSOliver Tappe }
621915a7b8cSOliver Tappe
622915a7b8cSOliver Tappe
623915a7b8cSOliver Tappe template<class T>
624915a7b8cSOliver Tappe void
MakeEmpty(bool deleteIfOwning)625915a7b8cSOliver Tappe BObjectList<T>::MakeEmpty(bool deleteIfOwning)
626915a7b8cSOliver Tappe {
627915a7b8cSOliver Tappe if (owning && deleteIfOwning) {
628915a7b8cSOliver Tappe int32 count = CountItems();
629915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++)
630915a7b8cSOliver Tappe delete ItemAt(index);
631915a7b8cSOliver Tappe }
632915a7b8cSOliver Tappe _PointerList_::MakeEmpty();
633915a7b8cSOliver Tappe }
634915a7b8cSOliver Tappe
635915a7b8cSOliver Tappe
636915a7b8cSOliver Tappe template<class T>
637915a7b8cSOliver Tappe T*
EachElement(EachFunction func,void * params)638915a7b8cSOliver Tappe BObjectList<T>::EachElement(EachFunction func, void* params)
639915a7b8cSOliver Tappe {
640915a7b8cSOliver Tappe return (T*)_PointerList_::EachElement((GenericEachFunction)func, params);
641915a7b8cSOliver Tappe }
642915a7b8cSOliver Tappe
643915a7b8cSOliver Tappe
644915a7b8cSOliver Tappe template<class T>
645915a7b8cSOliver Tappe const T*
EachElement(ConstEachFunction func,void * params)646915a7b8cSOliver Tappe BObjectList<T>::EachElement(ConstEachFunction func, void* params) const
647915a7b8cSOliver Tappe {
648915a7b8cSOliver Tappe return (const T*)
649915a7b8cSOliver Tappe const_cast<BObjectList<T>*>(this)->_PointerList_::EachElement(
650915a7b8cSOliver Tappe (GenericEachFunction)func, params);
651915a7b8cSOliver Tappe }
652915a7b8cSOliver Tappe
653915a7b8cSOliver Tappe template<class T>
654915a7b8cSOliver Tappe const T*
FindIf(const UnaryPredicate<T> & predicate)655915a7b8cSOliver Tappe BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate) const
656915a7b8cSOliver Tappe {
657915a7b8cSOliver Tappe int32 count = CountItems();
658915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
659915a7b8cSOliver Tappe if (predicate.operator()(ItemAt(index)) == 0)
660915a7b8cSOliver Tappe return ItemAt(index);
661915a7b8cSOliver Tappe }
662915a7b8cSOliver Tappe return 0;
663915a7b8cSOliver Tappe }
664915a7b8cSOliver Tappe
665915a7b8cSOliver Tappe template<class T>
666915a7b8cSOliver Tappe T*
FindIf(const UnaryPredicate<T> & predicate)667915a7b8cSOliver Tappe BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate)
668915a7b8cSOliver Tappe {
669915a7b8cSOliver Tappe int32 count = CountItems();
670915a7b8cSOliver Tappe for (int32 index = 0; index < count; index++) {
671915a7b8cSOliver Tappe if (predicate.operator()(ItemAt(index)) == 0)
672915a7b8cSOliver Tappe return ItemAt(index);
673915a7b8cSOliver Tappe }
674915a7b8cSOliver Tappe return 0;
675915a7b8cSOliver Tappe }
676915a7b8cSOliver Tappe
677915a7b8cSOliver Tappe
678915a7b8cSOliver Tappe template<class T>
679915a7b8cSOliver Tappe void
SortItems(CompareFunction function)680915a7b8cSOliver Tappe BObjectList<T>::SortItems(CompareFunction function)
681915a7b8cSOliver Tappe {
682915a7b8cSOliver Tappe _PointerList_::SortItems((GenericCompareFunction)function);
683915a7b8cSOliver Tappe }
684915a7b8cSOliver Tappe
685915a7b8cSOliver Tappe
686915a7b8cSOliver Tappe template<class T>
687915a7b8cSOliver Tappe void
SortItems(CompareFunctionWithState function,void * state)688915a7b8cSOliver Tappe BObjectList<T>::SortItems(CompareFunctionWithState function, void* state)
689915a7b8cSOliver Tappe {
690915a7b8cSOliver Tappe _PointerList_::SortItems((GenericCompareFunctionWithState)function, state);
691915a7b8cSOliver Tappe }
692915a7b8cSOliver Tappe
693915a7b8cSOliver Tappe
694915a7b8cSOliver Tappe template<class T>
695915a7b8cSOliver Tappe void
HSortItems(CompareFunction function)696915a7b8cSOliver Tappe BObjectList<T>::HSortItems(CompareFunction function)
697915a7b8cSOliver Tappe {
698915a7b8cSOliver Tappe _PointerList_::HSortItems((GenericCompareFunction)function);
699915a7b8cSOliver Tappe }
700915a7b8cSOliver Tappe
701915a7b8cSOliver Tappe
702915a7b8cSOliver Tappe template<class T>
703915a7b8cSOliver Tappe void
HSortItems(CompareFunctionWithState function,void * state)704915a7b8cSOliver Tappe BObjectList<T>::HSortItems(CompareFunctionWithState function, void* state)
705915a7b8cSOliver Tappe {
706915a7b8cSOliver Tappe _PointerList_::HSortItems((GenericCompareFunctionWithState)function, state);
707915a7b8cSOliver Tappe }
708915a7b8cSOliver Tappe
709915a7b8cSOliver Tappe
710915a7b8cSOliver Tappe template<class T>
711915a7b8cSOliver Tappe T*
BinarySearch(const T & key,CompareFunction func)712915a7b8cSOliver Tappe BObjectList<T>::BinarySearch(const T& key, CompareFunction func) const
713915a7b8cSOliver Tappe {
714915a7b8cSOliver Tappe return (T*)_PointerList_::BinarySearch(&key, (GenericCompareFunction)func);
715915a7b8cSOliver Tappe }
716915a7b8cSOliver Tappe
717*29e8fa59SJohn Scipione
718915a7b8cSOliver Tappe template<class T>
719915a7b8cSOliver Tappe T*
BinarySearch(const T & key,CompareFunctionWithState func,void * state)720915a7b8cSOliver Tappe BObjectList<T>::BinarySearch(const T& key, CompareFunctionWithState func,
721915a7b8cSOliver Tappe void* state) const
722915a7b8cSOliver Tappe {
723915a7b8cSOliver Tappe return (T*)_PointerList_::BinarySearch(&key,
724915a7b8cSOliver Tappe (GenericCompareFunctionWithState)func, state);
725915a7b8cSOliver Tappe }
726915a7b8cSOliver Tappe
727915a7b8cSOliver Tappe
728915a7b8cSOliver Tappe template<class T>
729915a7b8cSOliver Tappe template<typename Key>
730915a7b8cSOliver Tappe T*
BinarySearchByKey(const Key & key,int (* compare)(const Key *,const T *))731915a7b8cSOliver Tappe BObjectList<T>::BinarySearchByKey(const Key& key,
732915a7b8cSOliver Tappe int (*compare)(const Key*, const T*)) const
733915a7b8cSOliver Tappe {
734915a7b8cSOliver Tappe return (T*)_PointerList_::BinarySearch(&key,
735915a7b8cSOliver Tappe (GenericCompareFunction)compare);
736915a7b8cSOliver Tappe }
737915a7b8cSOliver Tappe
738915a7b8cSOliver Tappe
739915a7b8cSOliver Tappe template<class T>
740915a7b8cSOliver Tappe template<typename Key>
741915a7b8cSOliver Tappe T*
BinarySearchByKey(const Key & key,int (* compare)(const Key *,const T *,void *),void * state)742915a7b8cSOliver Tappe BObjectList<T>::BinarySearchByKey(const Key &key,
743915a7b8cSOliver Tappe int (*compare)(const Key*, const T*, void*), void* state) const
744915a7b8cSOliver Tappe {
745915a7b8cSOliver Tappe return (T*)_PointerList_::BinarySearch(&key,
746915a7b8cSOliver Tappe (GenericCompareFunctionWithState)compare, state);
747915a7b8cSOliver Tappe }
748915a7b8cSOliver Tappe
749915a7b8cSOliver Tappe
750915a7b8cSOliver Tappe template<class T>
751915a7b8cSOliver Tappe int32
BinarySearchIndex(const T & item,CompareFunction compare)752915a7b8cSOliver Tappe BObjectList<T>::BinarySearchIndex(const T& item, CompareFunction compare) const
753915a7b8cSOliver Tappe {
754915a7b8cSOliver Tappe return _PointerList_::BinarySearchIndex(&item,
755915a7b8cSOliver Tappe (GenericCompareFunction)compare);
756915a7b8cSOliver Tappe }
757915a7b8cSOliver Tappe
758915a7b8cSOliver Tappe
759915a7b8cSOliver Tappe template<class T>
760915a7b8cSOliver Tappe int32
BinarySearchIndex(const T & item,CompareFunctionWithState compare,void * state)761915a7b8cSOliver Tappe BObjectList<T>::BinarySearchIndex(const T& item,
762915a7b8cSOliver Tappe CompareFunctionWithState compare, void* state) const
763915a7b8cSOliver Tappe {
764915a7b8cSOliver Tappe return _PointerList_::BinarySearchIndex(&item,
765915a7b8cSOliver Tappe (GenericCompareFunctionWithState)compare, state);
766915a7b8cSOliver Tappe }
767915a7b8cSOliver Tappe
768915a7b8cSOliver Tappe
769915a7b8cSOliver Tappe template<class T>
770915a7b8cSOliver Tappe template<typename Key>
771915a7b8cSOliver Tappe int32
BinarySearchIndexByKey(const Key & key,int (* compare)(const Key *,const T *))772915a7b8cSOliver Tappe BObjectList<T>::BinarySearchIndexByKey(const Key& key,
773915a7b8cSOliver Tappe int (*compare)(const Key*, const T*)) const
774915a7b8cSOliver Tappe {
775915a7b8cSOliver Tappe return _PointerList_::BinarySearchIndex(&key,
776915a7b8cSOliver Tappe (GenericCompareFunction)compare);
777915a7b8cSOliver Tappe }
778915a7b8cSOliver Tappe
779915a7b8cSOliver Tappe
780915a7b8cSOliver Tappe template<class T>
781915a7b8cSOliver Tappe bool
BinaryInsert(T * item,CompareFunction func)782915a7b8cSOliver Tappe BObjectList<T>::BinaryInsert(T* item, CompareFunction func)
783915a7b8cSOliver Tappe {
784915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(item,
785915a7b8cSOliver Tappe (GenericCompareFunction)func);
786915a7b8cSOliver Tappe if (index >= 0) {
787915a7b8cSOliver Tappe // already in list, add after existing
788915a7b8cSOliver Tappe return AddItem(item, index + 1);
789915a7b8cSOliver Tappe }
790915a7b8cSOliver Tappe
791915a7b8cSOliver Tappe return AddItem(item, -index - 1);
792915a7b8cSOliver Tappe }
793915a7b8cSOliver Tappe
794915a7b8cSOliver Tappe
795915a7b8cSOliver Tappe template<class T>
796915a7b8cSOliver Tappe bool
BinaryInsert(T * item,CompareFunctionWithState func,void * state)797915a7b8cSOliver Tappe BObjectList<T>::BinaryInsert(T* item, CompareFunctionWithState func,
798915a7b8cSOliver Tappe void* state)
799915a7b8cSOliver Tappe {
800915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(item,
801915a7b8cSOliver Tappe (GenericCompareFunctionWithState)func, state);
802915a7b8cSOliver Tappe if (index >= 0) {
803915a7b8cSOliver Tappe // already in list, add after existing
804915a7b8cSOliver Tappe return AddItem(item, index + 1);
805915a7b8cSOliver Tappe }
806915a7b8cSOliver Tappe
807915a7b8cSOliver Tappe return AddItem(item, -index - 1);
808915a7b8cSOliver Tappe }
809915a7b8cSOliver Tappe
810915a7b8cSOliver Tappe
811915a7b8cSOliver Tappe template<class T>
812915a7b8cSOliver Tappe bool
BinaryInsertUnique(T * item,CompareFunction func)813915a7b8cSOliver Tappe BObjectList<T>::BinaryInsertUnique(T* item, CompareFunction func)
814915a7b8cSOliver Tappe {
815915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(item,
816915a7b8cSOliver Tappe (GenericCompareFunction)func);
817915a7b8cSOliver Tappe if (index >= 0)
818915a7b8cSOliver Tappe return false;
819915a7b8cSOliver Tappe
820915a7b8cSOliver Tappe return AddItem(item, -index - 1);
821915a7b8cSOliver Tappe }
822915a7b8cSOliver Tappe
823915a7b8cSOliver Tappe
824915a7b8cSOliver Tappe template<class T>
825915a7b8cSOliver Tappe bool
BinaryInsertUnique(T * item,CompareFunctionWithState func,void * state)826915a7b8cSOliver Tappe BObjectList<T>::BinaryInsertUnique(T* item, CompareFunctionWithState func,
827915a7b8cSOliver Tappe void* state)
828915a7b8cSOliver Tappe {
829915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(item,
830915a7b8cSOliver Tappe (GenericCompareFunctionWithState)func, state);
831915a7b8cSOliver Tappe if (index >= 0)
832915a7b8cSOliver Tappe return false;
833915a7b8cSOliver Tappe
834915a7b8cSOliver Tappe return AddItem(item, -index - 1);
835915a7b8cSOliver Tappe }
836915a7b8cSOliver Tappe
837915a7b8cSOliver Tappe
838915a7b8cSOliver Tappe template<class T>
839915a7b8cSOliver Tappe T*
BinaryInsertCopy(const T & copyThis,CompareFunction func)840915a7b8cSOliver Tappe BObjectList<T>::BinaryInsertCopy(const T& copyThis, CompareFunction func)
841915a7b8cSOliver Tappe {
842915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(©This,
843915a7b8cSOliver Tappe (GenericCompareFunction)func);
844915a7b8cSOliver Tappe
845915a7b8cSOliver Tappe if (index >= 0)
846915a7b8cSOliver Tappe index++;
847915a7b8cSOliver Tappe else
848915a7b8cSOliver Tappe index = -index - 1;
849915a7b8cSOliver Tappe
850915a7b8cSOliver Tappe T* newItem = new (std::nothrow) T(copyThis);
851915a7b8cSOliver Tappe AddItem(newItem, index);
852915a7b8cSOliver Tappe return newItem;
853915a7b8cSOliver Tappe }
854915a7b8cSOliver Tappe
855915a7b8cSOliver Tappe
856915a7b8cSOliver Tappe template<class T>
857915a7b8cSOliver Tappe T*
BinaryInsertCopy(const T & copyThis,CompareFunctionWithState func,void * state)858915a7b8cSOliver Tappe BObjectList<T>::BinaryInsertCopy(const T& copyThis,
859915a7b8cSOliver Tappe CompareFunctionWithState func, void* state)
860915a7b8cSOliver Tappe {
861915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(©This,
862915a7b8cSOliver Tappe (GenericCompareFunctionWithState)func, state);
863915a7b8cSOliver Tappe
864915a7b8cSOliver Tappe if (index >= 0)
865915a7b8cSOliver Tappe index++;
866915a7b8cSOliver Tappe else
867915a7b8cSOliver Tappe index = -index - 1;
868915a7b8cSOliver Tappe
869915a7b8cSOliver Tappe T* newItem = new (std::nothrow) T(copyThis);
870915a7b8cSOliver Tappe AddItem(newItem, index);
871915a7b8cSOliver Tappe return newItem;
872915a7b8cSOliver Tappe }
873915a7b8cSOliver Tappe
874915a7b8cSOliver Tappe
875915a7b8cSOliver Tappe template<class T>
876915a7b8cSOliver Tappe T*
BinaryInsertCopyUnique(const T & copyThis,CompareFunction func)877915a7b8cSOliver Tappe BObjectList<T>::BinaryInsertCopyUnique(const T& copyThis, CompareFunction func)
878915a7b8cSOliver Tappe {
879915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(©This,
880915a7b8cSOliver Tappe (GenericCompareFunction)func);
881915a7b8cSOliver Tappe if (index >= 0)
882915a7b8cSOliver Tappe return ItemAt(index);
883915a7b8cSOliver Tappe
884915a7b8cSOliver Tappe index = -index - 1;
885915a7b8cSOliver Tappe T* newItem = new (std::nothrow) T(copyThis);
886915a7b8cSOliver Tappe AddItem(newItem, index);
887915a7b8cSOliver Tappe return newItem;
888915a7b8cSOliver Tappe }
889915a7b8cSOliver Tappe
890915a7b8cSOliver Tappe
891915a7b8cSOliver Tappe template<class T>
892915a7b8cSOliver Tappe T*
BinaryInsertCopyUnique(const T & copyThis,CompareFunctionWithState func,void * state)893915a7b8cSOliver Tappe BObjectList<T>::BinaryInsertCopyUnique(const T& copyThis,
894915a7b8cSOliver Tappe CompareFunctionWithState func, void* state)
895915a7b8cSOliver Tappe {
896915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndex(©This,
897915a7b8cSOliver Tappe (GenericCompareFunctionWithState)func, state);
898915a7b8cSOliver Tappe if (index >= 0)
899915a7b8cSOliver Tappe return ItemAt(index);
900915a7b8cSOliver Tappe
901915a7b8cSOliver Tappe index = -index - 1;
902915a7b8cSOliver Tappe T* newItem = new (std::nothrow) T(copyThis);
903915a7b8cSOliver Tappe AddItem(newItem, index);
904915a7b8cSOliver Tappe return newItem;
905915a7b8cSOliver Tappe }
906915a7b8cSOliver Tappe
907915a7b8cSOliver Tappe
908915a7b8cSOliver Tappe template<class T>
909915a7b8cSOliver Tappe int32
FindBinaryInsertionIndex(const UnaryPredicate<T> & pred,bool * alreadyInList)910915a7b8cSOliver Tappe BObjectList<T>::FindBinaryInsertionIndex(const UnaryPredicate<T>& pred,
911915a7b8cSOliver Tappe bool* alreadyInList) const
912915a7b8cSOliver Tappe {
913915a7b8cSOliver Tappe int32 index = _PointerList_::BinarySearchIndexByPredicate(&pred,
914915a7b8cSOliver Tappe (UnaryPredicateGlue)&UnaryPredicate<T>::_unary_predicate_glue);
915915a7b8cSOliver Tappe
916915a7b8cSOliver Tappe if (alreadyInList)
917915a7b8cSOliver Tappe *alreadyInList = index >= 0;
918915a7b8cSOliver Tappe
919915a7b8cSOliver Tappe if (index < 0)
920915a7b8cSOliver Tappe index = -index - 1;
921915a7b8cSOliver Tappe
922915a7b8cSOliver Tappe return index;
923915a7b8cSOliver Tappe }
924915a7b8cSOliver Tappe
925915a7b8cSOliver Tappe
926915a7b8cSOliver Tappe template<class T>
927915a7b8cSOliver Tappe bool
BinaryInsert(T * item,const UnaryPredicate<T> & pred)928915a7b8cSOliver Tappe BObjectList<T>::BinaryInsert(T* item, const UnaryPredicate<T>& pred)
929915a7b8cSOliver Tappe {
930915a7b8cSOliver Tappe return AddItem(item, FindBinaryInsertionIndex(pred));
931915a7b8cSOliver Tappe }
932915a7b8cSOliver Tappe
933915a7b8cSOliver Tappe
934915a7b8cSOliver Tappe template<class T>
935915a7b8cSOliver Tappe bool
BinaryInsertUnique(T * item,const UnaryPredicate<T> & pred)936915a7b8cSOliver Tappe BObjectList<T>::BinaryInsertUnique(T* item, const UnaryPredicate<T>& pred)
937915a7b8cSOliver Tappe {
938915a7b8cSOliver Tappe bool alreadyInList;
939915a7b8cSOliver Tappe int32 index = FindBinaryInsertionIndex(pred, &alreadyInList);
940915a7b8cSOliver Tappe if (alreadyInList)
941915a7b8cSOliver Tappe return false;
942915a7b8cSOliver Tappe
943915a7b8cSOliver Tappe AddItem(item, index);
944915a7b8cSOliver Tappe return true;
945915a7b8cSOliver Tappe }
946915a7b8cSOliver Tappe
947915a7b8cSOliver Tappe
948*29e8fa59SJohn Scipione #endif // _OBJECT_LIST_H
949