Author: czeidler Date: 2011-07-25 01:49:30 +0200 (Mon, 25 Jul 2011) New Revision: 42475 Changeset: https://dev.haiku-os.org/changeset/42475 Modified: haiku/trunk/headers/os/support/ObjectList.h haiku/trunk/src/kits/support/PointerList.cpp Log: Add MoveItem method to easily move a item within a list. Fix line limit. Modified: haiku/trunk/headers/os/support/ObjectList.h =================================================================== --- haiku/trunk/headers/os/support/ObjectList.h 2011-07-24 18:27:05 UTC (rev 42474) +++ haiku/trunk/headers/os/support/ObjectList.h 2011-07-24 23:49:30 UTC (rev 42475) @@ -101,6 +101,7 @@ bool Owning() const; bool ReplaceItem(int32, void *); + bool MoveItem(int32 from, int32 to); protected: bool owning; @@ -154,6 +155,7 @@ // same as ReplaceItem, except does not // delete old item at <index>, returns it // instead + bool MoveItem(int32 from, int32 to); T* FirstItem() const; T* LastItem() const; @@ -553,6 +555,14 @@ template<class T> +bool +BObjectList<T>::MoveItem(int32 from, int32 to) +{ + return _PointerList_::MoveItem(from, to); +} + + +template<class T> void BObjectList<T>::_SetItem(int32 index, T* newItem) { Modified: haiku/trunk/src/kits/support/PointerList.cpp =================================================================== --- haiku/trunk/src/kits/support/PointerList.cpp 2011-07-24 18:27:05 UTC (rev 42474) +++ haiku/trunk/src/kits/support/PointerList.cpp 2011-07-24 23:49:30 UTC (rev 42475) @@ -18,10 +18,10 @@ #include <ObjectList.h> +#include <algorithm> #include <assert.h> - -#include <algorithm> #include <functional> +#include <string.h> #include <List.h> @@ -76,7 +76,8 @@ // Methods that do the actual work: inline void Swap(void **items, int32 i, int32 j); - void* BinarySearch(const void *key, const void **items, int32 numItems, int32 &index); + void* BinarySearch(const void *key, const void **items, int32 numItems, + int32 &index); void QuickSort(void **items, int32 low, int32 high); // Method to be implemented by sub classes @@ -151,7 +152,8 @@ void * -AbstractPointerListHelper::BinarySearch(const void *key, const void **items, int32 numItems, int32 &index) +AbstractPointerListHelper::BinarySearch(const void *key, const void **items, + int32 numItems, int32 &index) { const void** end = &items[numItems]; const void** found = lower_bound(items, end, key, comparator(this)); @@ -289,7 +291,8 @@ void -_PointerList_::SortItems(GenericCompareFunctionWithState compareFunc, void *state) +_PointerList_::SortItems(GenericCompareFunctionWithState compareFunc, + void *state) { PointerListHelperWithState helper(compareFunc, state); helper.SortItems(this); @@ -305,7 +308,8 @@ void -_PointerList_::HSortItems(GenericCompareFunctionWithState compareFunc, void *state) +_PointerList_::HSortItems(GenericCompareFunctionWithState compareFunc, + void *state) { PointerListHelperWithState helper(compareFunc, state); helper.HSortItems(this); @@ -313,7 +317,8 @@ void * -_PointerList_::BinarySearch(const void *key, GenericCompareFunction compareFunc) const +_PointerList_::BinarySearch(const void *key, + GenericCompareFunction compareFunc) const { PointerListHelper helper(compareFunc); return helper.BinarySearch(key, this); @@ -330,7 +335,8 @@ int32 -_PointerList_::BinarySearchIndex(const void *key, GenericCompareFunction compareFunc) const +_PointerList_::BinarySearchIndex(const void *key, + GenericCompareFunction compareFunc) const { PointerListHelper helper(compareFunc); return helper.BinarySearchIndex(key, this); @@ -347,7 +353,8 @@ int32 -_PointerList_::BinarySearchIndexByPredicate(const void *key, UnaryPredicateGlue predicate) const +_PointerList_::BinarySearchIndexByPredicate(const void *key, + UnaryPredicateGlue predicate) const { PointerListHelperUsePredicate helper(predicate); return helper.BinarySearchIndex(key, this); @@ -365,3 +372,26 @@ return true; } + +bool +_PointerList_::MoveItem(int32 from, int32 to) +{ + if (from == to) + return true; + + void* fromItem = ItemAt(from); + void* toItem = ItemAt(to); + if (fromItem == NULL || toItem == NULL) + return false; + + void** items = static_cast<void**>(Items()); + if (from < to) + memmove(items + from, items + from + 1, (to - from) * sizeof(void*)); + else + memmove(items + to + 1, items + to, (from - to) * sizeof(void*)); + + items[to] = fromItem; + return true; +} + +