0c8bdbafd5
We now keep track of a lower bound as to when the list should scale itself back down. When increasing the list size, we double the current, with the lower bound set to 1/4 of the current size, not allowing it to go any smaller than the block size. These combined allow us to do very cheap tests to see if an operation requires a resize at all, and minimize how often the list actually needs to be resized, since the difference in upper and lower bounds prevents bouncing back and forth between a size in the case of adding/removing an item while close to a boundary. All in all this should make BList noticably more scalable when doing large numbers of add/remove operations. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25946 a95241bf-73f2-0310-859d-f6bbb57e9c96
70 lines
1.6 KiB
C++
70 lines
1.6 KiB
C++
/*
|
|
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved.
|
|
* Distributed under the terms of the MIT License.
|
|
*/
|
|
#ifndef _BE_LIST_H
|
|
#define _BE_LIST_H
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
class BList {
|
|
public:
|
|
BList(int32 count = 20);
|
|
BList(const BList& anotherList);
|
|
virtual ~BList();
|
|
|
|
BList& operator =(const BList &);
|
|
|
|
/* Adding and removing items. */
|
|
bool AddItem(void* item, int32 index);
|
|
bool AddItem(void* item);
|
|
bool AddList(const BList* list, int32 index);
|
|
bool AddList(const BList* list);
|
|
bool RemoveItem(void* item);
|
|
void* RemoveItem(int32 index);
|
|
bool RemoveItems(int32 index, int32 count);
|
|
bool ReplaceItem(int32 index, void* newItem);
|
|
void MakeEmpty();
|
|
|
|
// Reorder items
|
|
void SortItems(int (*compareFunc)(const void*, const void*));
|
|
bool SwapItems(int32 indexA, int32 indexB);
|
|
bool MoveItem(int32 fromIndex, int32 toIndex);
|
|
|
|
// Retrieve items
|
|
void* ItemAt(int32 index) const;
|
|
void* FirstItem() const;
|
|
void* ItemAtFast(int32) const;
|
|
// does not check the array bounds!
|
|
|
|
void* LastItem() const;
|
|
void* Items() const;
|
|
|
|
// Query
|
|
bool HasItem(void* item) const;
|
|
int32 IndexOf(void* item) const;
|
|
int32 CountItems() const;
|
|
bool IsEmpty() const;
|
|
|
|
// Iteration
|
|
void DoForEach(bool (*func)(void* item));
|
|
void DoForEach(bool (*func)(void* item, void* arg2), void *arg2);
|
|
|
|
private:
|
|
virtual void _ReservedList1();
|
|
virtual void _ReservedList2();
|
|
|
|
bool _ResizeArray(int32 count);
|
|
private:
|
|
void** fObjectList;
|
|
int32 fPhysicalSize;
|
|
int32 fItemCount;
|
|
int32 fBlockSize;
|
|
int32 fResizeThreshold;
|
|
uint32 _reserved[1];
|
|
};
|
|
|
|
#endif // _BE_LIST_H
|