* BinaryInsertUnique() didn't work at all.

* both, BinaryInsert() and BinaryInsertUnique() now propagate the result of
  the AddItem() call - which could fail because of low memory.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14825 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-11-10 15:31:18 +00:00
parent 2522a90fd1
commit d5f377d66c
+32 -36
View File
@@ -144,7 +144,7 @@ public:
BObjectList &operator=(const BObjectList &list); BObjectList &operator=(const BObjectList &list);
// clones list; if list is owning, makes copies of all // clones list; if list is owning, makes copies of all
// the items // the items
// adding and removing // adding and removing
// ToDo: // ToDo:
// change Add calls to return const item // change Add calls to return const item
@@ -152,12 +152,12 @@ public:
bool AddItem(T *, int32); bool AddItem(T *, int32);
bool AddList(BObjectList *); bool AddList(BObjectList *);
bool AddList(BObjectList *, int32); bool AddList(BObjectList *, int32);
bool RemoveItem(T *, bool deleteIfOwning = true); bool RemoveItem(T *, bool deleteIfOwning = true);
// if owning, deletes the removed item // if owning, deletes the removed item
T *RemoveItemAt(int32); T *RemoveItemAt(int32);
// returns the removed item // returns the removed item
void MakeEmpty(); void MakeEmpty();
// item access // item access
@@ -168,10 +168,10 @@ public:
T *SwapWithItem(int32 index, T *newItem); T *SwapWithItem(int32 index, T *newItem);
// same as ReplaceItem, except does not delete old item at <index>, // same as ReplaceItem, except does not delete old item at <index>,
// returns it instead // returns it instead
T *FirstItem() const; T *FirstItem() const;
T *LastItem() const; T *LastItem() const;
// misc. getters // misc. getters
int32 IndexOf(const T *) const; int32 IndexOf(const T *) const;
bool HasItem(const T *) const; bool HasItem(const T *) const;
@@ -189,19 +189,19 @@ public:
// linear search, returns first item that matches predicate // linear search, returns first item that matches predicate
const T *FindIf(const UnaryPredicate<T> &) const; const T *FindIf(const UnaryPredicate<T> &) const;
T *FindIf(const UnaryPredicate<T> &); T *FindIf(const UnaryPredicate<T> &);
// list must be sorted with CompareFunction for these to work // list must be sorted with CompareFunction for these to work
const T *BinarySearch(const T &, CompareFunction) const; const T *BinarySearch(const T &, CompareFunction) const;
const T *BinarySearch(const T &, CompareFunctionWithState, void *state) const; const T *BinarySearch(const T &, CompareFunctionWithState, void *state) const;
// Binary insertion - list must be sorted with CompareFunction for // Binary insertion - list must be sorted with CompareFunction for
// these to work // these to work
// simple insert // simple insert
void BinaryInsert(T *, CompareFunction); bool BinaryInsert(T *, CompareFunction);
void BinaryInsert(T *, CompareFunctionWithState, void *state); bool BinaryInsert(T *, CompareFunctionWithState, void *state);
void BinaryInsert(T *, const UnaryPredicate<T> &); bool BinaryInsert(T *, const UnaryPredicate<T> &);
// unique insert, returns false if item already in list // unique insert, returns false if item already in list
bool BinaryInsertUnique(T *, CompareFunction); bool BinaryInsertUnique(T *, CompareFunction);
bool BinaryInsertUnique(T *, CompareFunctionWithState, void *state); bool BinaryInsertUnique(T *, CompareFunctionWithState, void *state);
@@ -210,13 +210,12 @@ public:
// insert a copy of the item, returns new inserted item // insert a copy of the item, returns new inserted item
T *BinaryInsertCopy(const T &copyThis, CompareFunction); T *BinaryInsertCopy(const T &copyThis, CompareFunction);
T *BinaryInsertCopy(const T &copyThis, CompareFunctionWithState, void *state); T *BinaryInsertCopy(const T &copyThis, CompareFunctionWithState, void *state);
// insert a copy of the item if not in list already // insert a copy of the item if not in list already
// returns new inserted item or existing item in case of a conflict // returns new inserted item or existing item in case of a conflict
T *BinaryInsertCopyUnique(const T &copyThis, CompareFunction); T *BinaryInsertCopyUnique(const T &copyThis, CompareFunction);
T *BinaryInsertCopyUnique(const T &copyThis, CompareFunctionWithState, void *state); T *BinaryInsertCopyUnique(const T &copyThis, CompareFunctionWithState, void *state);
int32 FindBinaryInsertionIndex(const UnaryPredicate<T> &, bool *alreadyInList = 0) const; int32 FindBinaryInsertionIndex(const UnaryPredicate<T> &, bool *alreadyInList = 0) const;
// returns either the index into which a new item should be inserted // returns either the index into which a new item should be inserted
// or index of an existing item that matches the predicate // or index of an existing item that matches the predicate
@@ -399,7 +398,6 @@ BObjectList<T>::~BObjectList()
if (Owning()) if (Owning())
// have to nuke elements first // have to nuke elements first
MakeEmpty(); MakeEmpty();
} }
template<class T> template<class T>
@@ -641,55 +639,55 @@ BObjectList<T>::BinarySearch(const T &key, CompareFunctionWithState func, void *
} }
template<class T> template<class T>
void bool
BObjectList<T>::BinaryInsert(T *item, CompareFunction func) BObjectList<T>::BinaryInsert(T *item, CompareFunction func)
{ {
int32 index = _PointerList_::BinarySearchIndex(item, int32 index = _PointerList_::BinarySearchIndex(item,
(GenericCompareFunction)func); (GenericCompareFunction)func);
if (index >= 0) if (index >= 0) {
// already in list, add after existing // already in list, add after existing
AddItem(item, index + 1); return AddItem(item, index + 1);
else }
AddItem(item, -index - 1);
return AddItem(item, -index - 1);
} }
template<class T> template<class T>
void bool
BObjectList<T>::BinaryInsert(T *item, CompareFunctionWithState func, void *state) BObjectList<T>::BinaryInsert(T *item, CompareFunctionWithState func, void *state)
{ {
int32 index = _PointerList_::BinarySearchIndex(item, int32 index = _PointerList_::BinarySearchIndex(item,
(GenericCompareFunctionWithState)func, state); (GenericCompareFunctionWithState)func, state);
if (index >= 0) if (index >= 0) {
// already in list, add after existing // already in list, add after existing
AddItem(item, index + 1); return AddItem(item, index + 1);
else }
AddItem(item, -index - 1);
return AddItem(item, -index - 1);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsertUnique(T *, CompareFunction func) BObjectList<T>::BinaryInsertUnique(T *item, CompareFunction func)
{ {
int32 index = _PointerList_::BinarySearchIndex(item, int32 index = _PointerList_::BinarySearchIndex(item,
(GenericCompareFunction)func); (GenericCompareFunction)func);
if (index >= 0) if (index >= 0)
return false; return false;
AddItem(item, -index - 1); return AddItem(item, -index - 1);
return true;
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsertUnique(T *, CompareFunctionWithState func, void *state) BObjectList<T>::BinaryInsertUnique(T *item, CompareFunctionWithState func, void *state)
{ {
int32 index = _PointerList_::BinarySearchIndex(item, int32 index = _PointerList_::BinarySearchIndex(item,
(GenericCompareFunctionWithState)func, state); (GenericCompareFunctionWithState)func, state);
if (index >= 0) if (index >= 0)
return false; return false;
AddItem(item, -index - 1); return AddItem(item, -index - 1);
return true;
} }
@@ -776,15 +774,14 @@ BObjectList<T>::FindBinaryInsertionIndex(const UnaryPredicate<T> &pred, bool *al
} }
template<class T> template<class T>
void bool
BObjectList<T>::BinaryInsert(T *item, const UnaryPredicate<T> &pred) BObjectList<T>::BinaryInsert(T *item, const UnaryPredicate<T> &pred)
{ {
int32 index = FindBinaryInsertionIndex(pred); return AddItem(item, FindBinaryInsertionIndex(pred));
AddItem(item, index);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsertUnique(T *item, const UnaryPredicate<T> &pred) BObjectList<T>::BinaryInsertUnique(T *item, const UnaryPredicate<T> &pred)
{ {
bool alreadyInList; bool alreadyInList;
@@ -796,5 +793,4 @@ BObjectList<T>::BinaryInsertUnique(T *item, const UnaryPredicate<T> &pred)
return true; return true;
} }
#endif /* __OBJECT_LIST__ */
#endif