2002-07-09 16:24:59 +04:00
|
|
|
/*
|
|
|
|
Open Tracker License
|
|
|
|
|
|
|
|
Terms and Conditions
|
|
|
|
|
|
|
|
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice applies to all licensees
|
|
|
|
and shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
Except as contained in this notice, the name of Be Incorporated shall not be
|
|
|
|
used in advertising or otherwise to promote the sale, use or other dealings in
|
|
|
|
this Software without prior written authorization from Be Incorporated.
|
|
|
|
|
|
|
|
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
|
|
|
|
of Be Incorporated in the United States and other countries. Other brand product
|
|
|
|
names are registered trademarks or trademarks of their respective holders.
|
|
|
|
All rights reserved.
|
|
|
|
*/
|
2011-01-20 15:09:16 +03:00
|
|
|
#ifndef _OBJECT_LIST_H
|
|
|
|
#define _OBJECT_LIST_H
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
#include <new>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#include <List.h>
|
2009-04-12 01:24:32 +04:00
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
//
|
|
|
|
// ObjectList is a wrapper around BList that adds type safety,
|
|
|
|
// optional object ownership, search, insert operations, etc.
|
|
|
|
//
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T> class BObjectList;
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
|
|
|
struct UnaryPredicate {
|
|
|
|
|
|
|
|
virtual int operator()(const T *) const
|
|
|
|
// virtual could be avoided here if FindBinaryInsertionIndex,
|
|
|
|
// etc. were member template functions
|
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static int _unary_predicate_glue(const void *item, void *context);
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
friend class BObjectList<T>;
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
int
|
2002-07-09 16:24:59 +04:00
|
|
|
UnaryPredicate<T>::_unary_predicate_glue(const void *item, void *context)
|
|
|
|
{
|
|
|
|
return ((UnaryPredicate<T> *)context)->operator()((const T *)item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _PointerList_ : public BList {
|
|
|
|
public:
|
|
|
|
_PointerList_(const _PointerList_ &list);
|
|
|
|
_PointerList_(int32 itemsPerBlock = 20, bool owning = false);
|
|
|
|
~_PointerList_();
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
typedef void *(* GenericEachFunction)(void *, void *);
|
|
|
|
typedef int (* GenericCompareFunction)(const void *, const void *);
|
|
|
|
typedef int (* GenericCompareFunctionWithState)(const void *, const void *,
|
|
|
|
void *);
|
|
|
|
typedef int (* UnaryPredicateGlue)(const void *, void *);
|
|
|
|
|
|
|
|
void *EachElement(GenericEachFunction, void *);
|
|
|
|
void SortItems(GenericCompareFunction);
|
|
|
|
void SortItems(GenericCompareFunctionWithState, void *state);
|
|
|
|
void HSortItems(GenericCompareFunction);
|
|
|
|
void HSortItems(GenericCompareFunctionWithState, void *state);
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
void *BinarySearch(const void *, GenericCompareFunction) const;
|
2011-01-20 15:09:16 +03:00
|
|
|
void *BinarySearch(const void *, GenericCompareFunctionWithState,
|
|
|
|
void *state) const;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
int32 BinarySearchIndex(const void *, GenericCompareFunction) const;
|
2011-01-20 15:09:16 +03:00
|
|
|
int32 BinarySearchIndex(const void *, GenericCompareFunctionWithState,
|
|
|
|
void *state) const;
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 BinarySearchIndexByPredicate(const void *, UnaryPredicateGlue) const;
|
|
|
|
|
|
|
|
bool Owning() const;
|
|
|
|
bool ReplaceItem(int32, void *);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool owning;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
|
|
|
class BObjectList : private _PointerList_ {
|
|
|
|
public:
|
|
|
|
|
|
|
|
// iteration and sorting
|
2011-01-20 15:09:16 +03:00
|
|
|
typedef T* (*EachFunction)(T*, void*);
|
|
|
|
typedef const T* (*ConstEachFunction)(const T*, void*);
|
|
|
|
typedef int (*CompareFunction)(const T*, const T*);
|
|
|
|
typedef int (*CompareFunctionWithState)(const T*, const T*,
|
|
|
|
void* state);
|
|
|
|
|
|
|
|
BObjectList(int32 itemsPerBlock = 20,
|
|
|
|
bool owning = false);
|
|
|
|
BObjectList(const BObjectList& list);
|
|
|
|
// clones list; if list is owning, makes
|
|
|
|
// copies of all the items
|
|
|
|
|
|
|
|
virtual ~BObjectList();
|
|
|
|
|
|
|
|
BObjectList& operator=(const BObjectList& list);
|
|
|
|
// clones list; if list is owning, makes
|
|
|
|
// copies of all the items
|
|
|
|
|
|
|
|
// adding and removing
|
|
|
|
bool AddItem(T*);
|
|
|
|
bool AddItem(T*, int32);
|
|
|
|
bool AddList(BObjectList*);
|
|
|
|
bool AddList(BObjectList*, int32);
|
|
|
|
|
|
|
|
bool RemoveItem(T*, bool deleteIfOwning = true);
|
|
|
|
// if owning, deletes the removed item
|
|
|
|
T* RemoveItemAt(int32);
|
|
|
|
// returns the removed item
|
|
|
|
|
|
|
|
void MakeEmpty(bool deleteIfOwning = true);
|
|
|
|
|
|
|
|
// item access
|
|
|
|
T* ItemAt(int32) const;
|
|
|
|
|
|
|
|
bool ReplaceItem(int32 index, T*);
|
|
|
|
// if list is owning, deletes the item
|
|
|
|
// at <index> first
|
|
|
|
T* SwapWithItem(int32 index, T* newItem);
|
|
|
|
// same as ReplaceItem, except does not
|
|
|
|
// delete old item at <index>, returns it
|
|
|
|
// instead
|
|
|
|
|
|
|
|
T* FirstItem() const;
|
|
|
|
T* LastItem() const;
|
|
|
|
|
|
|
|
// misc. getters
|
|
|
|
int32 IndexOf(const T*) const;
|
|
|
|
bool HasItem(const T*) const;
|
|
|
|
bool IsEmpty() const;
|
|
|
|
int32 CountItems() const;
|
|
|
|
|
|
|
|
T* EachElement(EachFunction, void*);
|
|
|
|
const T* EachElement(ConstEachFunction, void*) const;
|
|
|
|
|
|
|
|
void SortItems(CompareFunction);
|
|
|
|
void SortItems(CompareFunctionWithState,
|
|
|
|
void* state);
|
|
|
|
void HSortItems(CompareFunction);
|
|
|
|
void HSortItems(CompareFunctionWithState,
|
|
|
|
void* state);
|
|
|
|
|
|
|
|
// linear search, returns first item that
|
|
|
|
// matches predicate
|
|
|
|
const T* FindIf(const UnaryPredicate<T>&) const;
|
|
|
|
T* FindIf(const UnaryPredicate<T>&);
|
|
|
|
|
|
|
|
// list must be sorted with CompareFunction for
|
|
|
|
// these to work
|
|
|
|
T* BinarySearch(const T&, CompareFunction) const;
|
|
|
|
T* BinarySearch(const T&, CompareFunctionWithState,
|
|
|
|
void* state) const;
|
|
|
|
|
|
|
|
template<typename Key>
|
|
|
|
T* BinarySearchByKey(const Key& key,
|
|
|
|
int (*compare)(const Key*, const T*)) const;
|
|
|
|
|
|
|
|
template<typename Key>
|
|
|
|
T* BinarySearchByKey(const Key& key,
|
|
|
|
int (*compare)(const Key*, const T*, void*),
|
|
|
|
void* state) const;
|
|
|
|
|
|
|
|
int32 BinarySearchIndex(const T&item,
|
|
|
|
CompareFunction compare) const;
|
|
|
|
int32 BinarySearchIndex(const T&item,
|
|
|
|
CompareFunctionWithState compare,
|
|
|
|
void* state) const;
|
|
|
|
|
|
|
|
template<typename Key>
|
|
|
|
int32 BinarySearchIndexByKey(const Key& key,
|
|
|
|
int (*compare)(const Key*, const T*)) const;
|
|
|
|
|
|
|
|
// Binary insertion - list must be sorted with
|
|
|
|
// CompareFunction for these to work
|
|
|
|
|
|
|
|
// simple insert
|
|
|
|
bool BinaryInsert(T*, CompareFunction);
|
|
|
|
bool BinaryInsert(T*, CompareFunctionWithState,
|
|
|
|
void* state);
|
|
|
|
bool BinaryInsert(T*, const UnaryPredicate<T>&);
|
|
|
|
|
|
|
|
// unique insert, returns false if item already
|
|
|
|
// in list
|
|
|
|
bool BinaryInsertUnique(T*, CompareFunction);
|
|
|
|
bool BinaryInsertUnique(T*, CompareFunctionWithState,
|
|
|
|
void* state);
|
|
|
|
bool BinaryInsertUnique(T*,
|
|
|
|
const UnaryPredicate<T>&);
|
|
|
|
|
|
|
|
// insert a copy of the item, returns new
|
|
|
|
// inserted item
|
|
|
|
T* BinaryInsertCopy(const T& copyThis,
|
|
|
|
CompareFunction);
|
|
|
|
T* BinaryInsertCopy(const T& copyThis,
|
|
|
|
CompareFunctionWithState, void* state);
|
|
|
|
|
|
|
|
// insert a copy of the item if not in list
|
|
|
|
// already returns new inserted item or existing
|
|
|
|
// item in case of a conflict
|
|
|
|
T* BinaryInsertCopyUnique(const T& copyThis,
|
|
|
|
CompareFunction);
|
|
|
|
T* BinaryInsertCopyUnique(const T& copyThis,
|
|
|
|
CompareFunctionWithState, void* state);
|
|
|
|
|
|
|
|
int32 FindBinaryInsertionIndex(
|
|
|
|
const UnaryPredicate<T>&,
|
|
|
|
bool* alreadyInList = 0) const;
|
|
|
|
// returns either the index into which a new
|
|
|
|
// item should be inserted or index of an
|
|
|
|
// existing item that matches the predicate
|
|
|
|
|
|
|
|
class Private;
|
2002-07-09 16:24:59 +04:00
|
|
|
private:
|
2011-01-20 15:09:16 +03:00
|
|
|
friend class Private;
|
|
|
|
|
|
|
|
void _SetItem(int32, T*);
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Result, class Param1>
|
2009-04-12 01:24:32 +04:00
|
|
|
Result
|
2011-01-20 15:09:16 +03:00
|
|
|
WhileEachListItem(BObjectList<Item>* list, Result (Item::*func)(Param1),
|
|
|
|
Param1 p1)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-04-12 01:24:32 +04:00
|
|
|
Result result = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 count = list->CountItems();
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
for (int32 index = 0; index < count; index++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
if ((result = (list->ItemAt(index)->*func)(p1)) != 0)
|
|
|
|
break;
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Result, class Param1>
|
2009-04-12 01:24:32 +04:00
|
|
|
Result
|
2011-01-20 15:09:16 +03:00
|
|
|
WhileEachListItem(BObjectList<Item>* list, Result (*func)(Item*, Param1),
|
|
|
|
Param1 p1)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-04-12 01:24:32 +04:00
|
|
|
Result result = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 count = list->CountItems();
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
for (int32 index = 0; index < count; index++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
if ((result = (*func)(list->ItemAt(index), p1)) != 0)
|
|
|
|
break;
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Result, class Param1, class Param2>
|
2009-04-12 01:24:32 +04:00
|
|
|
Result
|
2011-01-20 15:09:16 +03:00
|
|
|
WhileEachListItem(BObjectList<Item>* list, Result (Item::*func)(Param1, Param2),
|
2002-07-09 16:24:59 +04:00
|
|
|
Param1 p1, Param2 p2)
|
|
|
|
{
|
2009-04-12 01:24:32 +04:00
|
|
|
Result result = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 count = list->CountItems();
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
for (int32 index = 0; index < count; index++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
if ((result = (list->ItemAt(index)->*func)(p1, p2)) != 0)
|
|
|
|
break;
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Result, class Param1, class Param2>
|
2009-04-12 01:24:32 +04:00
|
|
|
Result
|
2011-01-20 15:09:16 +03:00
|
|
|
WhileEachListItem(BObjectList<Item>* list,
|
|
|
|
Result (*func)(Item*, Param1, Param2), Param1 p1, Param2 p2)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-04-12 01:24:32 +04:00
|
|
|
Result result = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 count = list->CountItems();
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
for (int32 index = 0; index < count; index++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
if ((result = (*func)(list->ItemAt(index), p1, p2)) != 0)
|
|
|
|
break;
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
|
|
|
template<class Item, class Result, class Param1, class Param2, class Param3,
|
|
|
|
class Param4>
|
2009-04-12 01:24:32 +04:00
|
|
|
Result
|
2011-01-20 15:09:16 +03:00
|
|
|
WhileEachListItem(BObjectList<Item>* list,
|
|
|
|
Result (*func)(Item*, Param1, Param2, Param3, Param4), Param1 p1, Param2 p2,
|
|
|
|
Param3 p3, Param4 p4)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-04-12 01:24:32 +04:00
|
|
|
Result result = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 count = list->CountItems();
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
for (int32 index = 0; index < count; index++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
if ((result = (*func)(list->ItemAt(index), p1, p2, p3, p4)) != 0)
|
|
|
|
break;
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Result>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
EachListItemIgnoreResult(BObjectList<Item>* list, Result (Item::*func)())
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 count = list->CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++)
|
|
|
|
(list->ItemAt(index)->*func)();
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Param1>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
EachListItem(BObjectList<Item>* list, void (*func)(Item*, Param1), Param1 p1)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 count = list->CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++)
|
|
|
|
(func)(list->ItemAt(index), p1);
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Param1, class Param2>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
EachListItem(BObjectList<Item>* list, void (Item::*func)(Param1, Param2),
|
2002-07-09 16:24:59 +04:00
|
|
|
Param1 p1, Param2 p2)
|
|
|
|
{
|
|
|
|
int32 count = list->CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++)
|
|
|
|
(list->ItemAt(index)->*func)(p1, p2);
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Param1, class Param2>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
EachListItem(BObjectList<Item>* list, void (*func)(Item*,Param1, Param2),
|
2002-07-09 16:24:59 +04:00
|
|
|
Param1 p1, Param2 p2)
|
|
|
|
{
|
|
|
|
int32 count = list->CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++)
|
|
|
|
(func)(list->ItemAt(index), p1, p2);
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class Item, class Param1, class Param2, class Param3>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
EachListItem(BObjectList<Item>* list,
|
|
|
|
void (*func)(Item*,Param1, Param2, Param3), Param1 p1, Param2 p2, Param3 p3)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 count = list->CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++)
|
|
|
|
(func)(list->ItemAt(index), p1, p2, p3);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class Item, class Param1, class Param2, class Param3, class Param4>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
EachListItem(BObjectList<Item>* list,
|
|
|
|
void (*func)(Item*,Param1, Param2, Param3, Param4), Param1 p1, Param2 p2,
|
|
|
|
Param3 p3, Param4 p4)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 count = list->CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++)
|
|
|
|
(func)(list->ItemAt(index), p1, p2, p3, p4);
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// inline code
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
inline bool
|
|
|
|
_PointerList_::Owning() const
|
|
|
|
{
|
|
|
|
return owning;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::BObjectList(int32 itemsPerBlock, bool owning)
|
2011-01-20 15:09:16 +03:00
|
|
|
:
|
|
|
|
_PointerList_(itemsPerBlock, owning)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::BObjectList(const BObjectList<T> &list)
|
2011-01-20 15:09:16 +03:00
|
|
|
:
|
|
|
|
_PointerList_(list)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
owning = list.owning;
|
|
|
|
if (owning) {
|
|
|
|
// make our own copies in an owning list
|
|
|
|
int32 count = list.CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++) {
|
2011-01-20 15:09:16 +03:00
|
|
|
T* item = list.ItemAt(index);
|
2002-07-09 16:24:59 +04:00
|
|
|
if (item)
|
2011-01-20 15:09:16 +03:00
|
|
|
item = new (std::nothrow) T(*item);
|
|
|
|
_SetItem(index, item);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::~BObjectList()
|
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
if (Owning()) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// have to nuke elements first
|
|
|
|
MakeEmpty();
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>&
|
|
|
|
BObjectList<T>::operator=(const BObjectList<T>& list)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
owning = list.owning;
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T> &result = (BObjectList<T>&)_PointerList_::operator=(list);
|
2002-07-09 16:24:59 +04:00
|
|
|
if (owning) {
|
|
|
|
// make our own copies in an owning list
|
|
|
|
int32 count = list.CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++) {
|
2011-01-20 15:09:16 +03:00
|
|
|
T* item = list.ItemAt(index);
|
2002-07-09 16:24:59 +04:00
|
|
|
if (item)
|
2011-01-20 15:09:16 +03:00
|
|
|
item = new (std::nothrow) T(*item);
|
|
|
|
_SetItem(index, item);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::AddItem(T* item)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
// need to cast to void* to make T work for const pointers
|
|
|
|
return _PointerList_::AddItem((void*)item);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::AddItem(T* item, int32 atIndex)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return _PointerList_::AddItem((void*)item, atIndex);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::AddList(BObjectList<T>* newItems)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return _PointerList_::AddList(newItems);
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::AddList(BObjectList<T>* newItems, int32 atIndex)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return _PointerList_::AddList(newItems, atIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::RemoveItem(T* item, bool deleteIfOwning)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
bool result = _PointerList_::RemoveItem((void*)item);
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
if (result && Owning() && deleteIfOwning)
|
|
|
|
delete item;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::RemoveItemAt(int32 index)
|
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return (T*)_PointerList_::RemoveItem(index);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
inline T*
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::ItemAt(int32 index) const
|
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return (T*)_PointerList_::ItemAt(index);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::ReplaceItem(int32 index, T* item)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
if (owning)
|
|
|
|
delete ItemAt(index);
|
2011-01-20 15:09:16 +03:00
|
|
|
return _PointerList_::ReplaceItem(index, (void*)item);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::SwapWithItem(int32 index, T* newItem)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
T* result = ItemAt(index);
|
|
|
|
_PointerList_::ReplaceItem(index, (void*)newItem);
|
2002-07-09 16:24:59 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::_SetItem(int32 index, T* newItem)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
_PointerList_::ReplaceItem(index, (void*)newItem);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
int32
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::IndexOf(const T* item) const
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return _PointerList_::IndexOf((void*)item);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::FirstItem() const
|
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return (T*)_PointerList_::FirstItem();
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::LastItem() const
|
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return (T*)_PointerList_::LastItem();
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::HasItem(const T* item) const
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return _PointerList_::HasItem((void*)item);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
bool
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::IsEmpty() const
|
|
|
|
{
|
|
|
|
return _PointerList_::IsEmpty();
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
int32
|
2002-07-09 16:24:59 +04:00
|
|
|
BObjectList<T>::CountItems() const
|
|
|
|
{
|
|
|
|
return _PointerList_::CountItems();
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
|
|
|
void
|
2009-12-12 21:17:15 +03:00
|
|
|
BObjectList<T>::MakeEmpty(bool deleteIfOwning)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-12-12 21:17:15 +03:00
|
|
|
if (owning && deleteIfOwning) {
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 count = CountItems();
|
|
|
|
for (int32 index = 0; index < count; index++)
|
|
|
|
delete ItemAt(index);
|
|
|
|
}
|
|
|
|
_PointerList_::MakeEmpty();
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::EachElement(EachFunction func, void* params)
|
2009-04-12 01:24:32 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return (T*)_PointerList_::EachElement((GenericEachFunction)func, params);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
const T*
|
|
|
|
BObjectList<T>::EachElement(ConstEachFunction func, void* params) const
|
2009-04-12 01:24:32 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return (const T*)
|
|
|
|
const_cast<BObjectList<T>*>(this)->_PointerList_::EachElement(
|
|
|
|
(GenericEachFunction)func, params);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
const T*
|
|
|
|
BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate) const
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 count = CountItems();
|
2011-01-20 15:09:16 +03:00
|
|
|
for (int32 index = 0; index < count; index++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
if (predicate.operator()(ItemAt(index)) == 0)
|
|
|
|
return ItemAt(index);
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 count = CountItems();
|
2011-01-20 15:09:16 +03:00
|
|
|
for (int32 index = 0; index < count; index++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
if (predicate.operator()(ItemAt(index)) == 0)
|
|
|
|
return ItemAt(index);
|
2011-01-20 15:09:16 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2002-07-09 16:24:59 +04:00
|
|
|
void
|
|
|
|
BObjectList<T>::SortItems(CompareFunction function)
|
2009-04-12 01:24:32 +04:00
|
|
|
{
|
|
|
|
_PointerList_::SortItems((GenericCompareFunction)function);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::SortItems(CompareFunctionWithState function, void* state)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-04-12 01:24:32 +04:00
|
|
|
_PointerList_::SortItems((GenericCompareFunctionWithState)function, state);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
template<class T>
|
2002-07-09 16:24:59 +04:00
|
|
|
void
|
|
|
|
BObjectList<T>::HSortItems(CompareFunction function)
|
2009-04-12 01:24:32 +04:00
|
|
|
{
|
|
|
|
_PointerList_::HSortItems((GenericCompareFunction)function);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::HSortItems(CompareFunctionWithState function, void* state)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-04-12 01:24:32 +04:00
|
|
|
_PointerList_::HSortItems((GenericCompareFunctionWithState)function, state);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::BinarySearch(const T& key, CompareFunction func) const
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2011-01-20 15:09:16 +03:00
|
|
|
return (T*)_PointerList_::BinarySearch(&key, (GenericCompareFunction)func);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::BinarySearch(const T& key, CompareFunctionWithState func,
|
|
|
|
void* state) const
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2009-04-21 16:15:42 +04:00
|
|
|
return (T*)_PointerList_::BinarySearch(&key,
|
2002-07-09 16:24:59 +04:00
|
|
|
(GenericCompareFunctionWithState)func, state);
|
|
|
|
}
|
|
|
|
|
2009-04-21 16:15:42 +04:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<typename Key>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::BinarySearchByKey(const Key& key,
|
|
|
|
int (*compare)(const Key*, const T*)) const
|
2009-04-21 16:15:42 +04:00
|
|
|
{
|
|
|
|
return (T*)_PointerList_::BinarySearch(&key,
|
|
|
|
(GenericCompareFunction)compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<typename Key>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
2009-04-21 16:15:42 +04:00
|
|
|
BObjectList<T>::BinarySearchByKey(const Key &key,
|
2011-01-20 15:09:16 +03:00
|
|
|
int (*compare)(const Key*, const T*, void*), void* state) const
|
2009-04-21 16:15:42 +04:00
|
|
|
{
|
|
|
|
return (T*)_PointerList_::BinarySearch(&key,
|
|
|
|
(GenericCompareFunctionWithState)compare, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-23 01:41:16 +04:00
|
|
|
template<class T>
|
|
|
|
int32
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinarySearchIndex(const T& item, CompareFunction compare) const
|
2009-06-23 01:41:16 +04:00
|
|
|
{
|
|
|
|
return _PointerList_::BinarySearchIndex(&item,
|
|
|
|
(GenericCompareFunction)compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
int32
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinarySearchIndex(const T& item,
|
|
|
|
CompareFunctionWithState compare, void* state) const
|
2009-06-23 01:41:16 +04:00
|
|
|
{
|
|
|
|
return _PointerList_::BinarySearchIndex(&item,
|
|
|
|
(GenericCompareFunctionWithState)compare, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-10 03:24:55 +04:00
|
|
|
template<class T>
|
|
|
|
template<typename Key>
|
|
|
|
int32
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinarySearchIndexByKey(const Key& key,
|
|
|
|
int (*compare)(const Key*, const T*)) const
|
2009-07-10 03:24:55 +04:00
|
|
|
{
|
|
|
|
return _PointerList_::BinarySearchIndex(&key,
|
|
|
|
(GenericCompareFunction)compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2005-11-10 18:31:18 +03:00
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinaryInsert(T* item, CompareFunction func)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(item,
|
|
|
|
(GenericCompareFunction)func);
|
2005-11-10 18:31:18 +03:00
|
|
|
if (index >= 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// already in list, add after existing
|
2005-11-10 18:31:18 +03:00
|
|
|
return AddItem(item, index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddItem(item, -index - 1);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2005-11-10 18:31:18 +03:00
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinaryInsert(T* item, CompareFunctionWithState func,
|
|
|
|
void* state)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(item,
|
|
|
|
(GenericCompareFunctionWithState)func, state);
|
2005-11-10 18:31:18 +03:00
|
|
|
if (index >= 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// already in list, add after existing
|
2005-11-10 18:31:18 +03:00
|
|
|
return AddItem(item, index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddItem(item, -index - 1);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinaryInsertUnique(T* item, CompareFunction func)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(item,
|
|
|
|
(GenericCompareFunction)func);
|
|
|
|
if (index >= 0)
|
|
|
|
return false;
|
|
|
|
|
2005-11-10 18:31:18 +03:00
|
|
|
return AddItem(item, -index - 1);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinaryInsertUnique(T* item, CompareFunctionWithState func,
|
|
|
|
void* state)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(item,
|
|
|
|
(GenericCompareFunctionWithState)func, state);
|
|
|
|
if (index >= 0)
|
|
|
|
return false;
|
|
|
|
|
2005-11-10 18:31:18 +03:00
|
|
|
return AddItem(item, -index - 1);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::BinaryInsertCopy(const T& copyThis, CompareFunction func)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(©This,
|
|
|
|
(GenericCompareFunction)func);
|
|
|
|
|
|
|
|
if (index >= 0)
|
|
|
|
index++;
|
|
|
|
else
|
|
|
|
index = -index - 1;
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
T* newItem = new (std::nothrow) T(copyThis);
|
2002-07-09 16:24:59 +04:00
|
|
|
AddItem(newItem, index);
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::BinaryInsertCopy(const T& copyThis,
|
|
|
|
CompareFunctionWithState func, void* state)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(©This,
|
|
|
|
(GenericCompareFunctionWithState)func, state);
|
|
|
|
|
|
|
|
if (index >= 0)
|
|
|
|
index++;
|
|
|
|
else
|
|
|
|
index = -index - 1;
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
T* newItem = new (std::nothrow) T(copyThis);
|
2002-07-09 16:24:59 +04:00
|
|
|
AddItem(newItem, index);
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::BinaryInsertCopyUnique(const T& copyThis, CompareFunction func)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(©This,
|
|
|
|
(GenericCompareFunction)func);
|
|
|
|
if (index >= 0)
|
|
|
|
return ItemAt(index);
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
index = -index - 1;
|
2011-01-20 15:09:16 +03:00
|
|
|
T* newItem = new (std::nothrow) T(copyThis);
|
2002-07-09 16:24:59 +04:00
|
|
|
AddItem(newItem, index);
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2011-01-20 15:09:16 +03:00
|
|
|
T*
|
|
|
|
BObjectList<T>::BinaryInsertCopyUnique(const T& copyThis,
|
|
|
|
CompareFunctionWithState func, void* state)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndex(©This,
|
|
|
|
(GenericCompareFunctionWithState)func, state);
|
|
|
|
if (index >= 0)
|
|
|
|
return ItemAt(index);
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
index = -index - 1;
|
2011-01-20 15:09:16 +03:00
|
|
|
T* newItem = new (std::nothrow) T(copyThis);
|
2002-07-09 16:24:59 +04:00
|
|
|
AddItem(newItem, index);
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
int32
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::FindBinaryInsertionIndex(const UnaryPredicate<T>& pred,
|
|
|
|
bool* alreadyInList) const
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int32 index = _PointerList_::BinarySearchIndexByPredicate(&pred,
|
|
|
|
(UnaryPredicateGlue)&UnaryPredicate<T>::_unary_predicate_glue);
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
if (alreadyInList)
|
|
|
|
*alreadyInList = index >= 0;
|
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
if (index < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
index = -index - 1;
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2005-11-10 18:31:18 +03:00
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinaryInsert(T* item, const UnaryPredicate<T>& pred)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-11-10 18:31:18 +03:00
|
|
|
return AddItem(item, FindBinaryInsertionIndex(pred));
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class T>
|
2005-11-10 18:31:18 +03:00
|
|
|
bool
|
2011-01-20 15:09:16 +03:00
|
|
|
BObjectList<T>::BinaryInsertUnique(T* item, const UnaryPredicate<T>& pred)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
bool alreadyInList;
|
|
|
|
int32 index = FindBinaryInsertionIndex(pred, &alreadyInList);
|
|
|
|
if (alreadyInList)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AddItem(item, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:09:16 +03:00
|
|
|
|
|
|
|
#endif /* _OBJECT_LIST_H */
|