* Code and header indentation cleanup

* Added operator== and !=
 * Added check for list != this in operator=
 * Added HasItem() and IndexOf() versions that take const void*, duplicating
   the code, since I didn't want to introduce another function call in these
   potentially time critical methods.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34520 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-12-06 12:44:38 +00:00
parent bc69c291c2
commit b0850e9ba1
2 changed files with 206 additions and 179 deletions
+58 -48
View File
@@ -1,8 +1,8 @@
/* /*
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved. * Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _BE_LIST_H #ifndef _BE_LIST_H
#define _BE_LIST_H #define _BE_LIST_H
@@ -10,60 +10,70 @@
class BList { class BList {
public: public:
BList(int32 count = 20); BList(int32 count = 20);
BList(const BList& anotherList); BList(const BList& anotherList);
virtual ~BList(); virtual ~BList();
BList& operator =(const BList &); BList& operator=(const BList& other);
bool operator==(const BList& other) const;
bool operator!=(const BList& other) const;
/* Adding and removing items. */ // Adding and removing items.
bool AddItem(void* item, int32 index); bool AddItem(void* item, int32 index);
bool AddItem(void* item); bool AddItem(void* item);
bool AddList(const BList* list, int32 index); bool AddList(const BList* list, int32 index);
bool AddList(const BList* list); 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 bool RemoveItem(void* item);
void SortItems(int (*compareFunc)(const void*, const void*)); void* RemoveItem(int32 index);
bool SwapItems(int32 indexA, int32 indexB); bool RemoveItems(int32 index, int32 count);
bool MoveItem(int32 fromIndex, int32 toIndex); bool ReplaceItem(int32 index, void* newItem);
// Retrieve items void MakeEmpty();
void* ItemAt(int32 index) const;
void* FirstItem() const;
void* ItemAtFast(int32) const;
// does not check the array bounds!
void* LastItem() const; // Reorder items
void* Items() const; void SortItems(int (*compareFunc)(const void*,
const void*));
bool SwapItems(int32 indexA, int32 indexB);
bool MoveItem(int32 fromIndex, int32 toIndex);
// Query // Retrieve items
bool HasItem(void* item) const; void* ItemAt(int32 index) const;
int32 IndexOf(void* item) const; void* FirstItem() const;
int32 CountItems() const; void* ItemAtFast(int32 index) const;
bool IsEmpty() const; // does not check the array bounds!
// Iteration void* LastItem() const;
void DoForEach(bool (*func)(void* item)); void* Items() const;
void DoForEach(bool (*func)(void* item, void* arg2), void *arg2);
private: // Query
virtual void _ReservedList1(); bool HasItem(void* item) const;
virtual void _ReservedList2(); bool HasItem(const void* item) const;
int32 IndexOf(void* item) const;
int32 IndexOf(const void* item) const;
int32 CountItems() const;
bool IsEmpty() const;
bool _ResizeArray(int32 count); // Iteration
private: void DoForEach(bool (*func)(void* item));
void** fObjectList; void DoForEach(bool (*func)(void* item,
int32 fPhysicalSize; void* arg2), void* arg2);
int32 fItemCount;
int32 fBlockSize; private:
int32 fResizeThreshold; virtual void _ReservedList1();
uint32 _reserved[1]; 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 #endif // _BE_LIST_H
+148 -131
View File
@@ -1,44 +1,26 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2008, Haiku, Inc. * Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * The Storage kit Team
// the rights to use, copy, modify, merge, publish, distribute, sublicense, * Isaac Yonemoto
// and/or sell copies of the Software, and to permit persons to whom the * Rene Gollent
// Software is furnished to do so, subject to the following conditions: * Stephan Aßmus
// */
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. //! BList class provides storage for pointers. Not thread safe.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS 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.
//
// File Name: List.cpp
// Author(s): The Storage kit Team
// Isaac Yonemoto
// Rene Gollent
// Description: BList class provides storage for pointers.
// Not thread safe.
//------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <List.h> #include <List.h>
// System Includes -------------------------------------------------------------
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// helper function // helper function
static inline static inline void
void
move_items(void** items, int32 offset, int32 count) move_items(void** items, int32 offset, int32 count)
{ {
if (count > 0 && offset != 0) if (count > 0 && offset != 0)
@@ -46,13 +28,13 @@ move_items(void** items, int32 offset, int32 count)
} }
// constructor
BList::BList(int32 count) BList::BList(int32 count)
: fObjectList(NULL), :
fPhysicalSize(0), fObjectList(NULL),
fItemCount(0), fPhysicalSize(0),
fBlockSize(count), fItemCount(0),
fResizeThreshold(0) fBlockSize(count),
fResizeThreshold(0)
{ {
if (fBlockSize <= 0) if (fBlockSize <= 0)
fBlockSize = 1; fBlockSize = 1;
@@ -60,41 +42,65 @@ BList::BList(int32 count)
} }
// copy constructor
BList::BList(const BList& anotherList) BList::BList(const BList& anotherList)
: fObjectList(NULL), :
fPhysicalSize(0), fObjectList(NULL),
fItemCount(0), fPhysicalSize(0),
fBlockSize(anotherList.fBlockSize) fItemCount(0),
fBlockSize(anotherList.fBlockSize)
{ {
*this = anotherList; *this = anotherList;
} }
// destructor
BList::~BList() BList::~BList()
{ {
free(fObjectList); free(fObjectList);
} }
// =
BList& BList&
BList::operator =(const BList &list) BList::operator=(const BList& list)
{ {
fBlockSize = list.fBlockSize; if (&list != this) {
if (_ResizeArray(list.fItemCount)) { fBlockSize = list.fBlockSize;
fItemCount = list.fItemCount; if (_ResizeArray(list.fItemCount)) {
memcpy(fObjectList, list.fObjectList, fItemCount * sizeof(void*)); fItemCount = list.fItemCount;
memcpy(fObjectList, list.fObjectList, fItemCount * sizeof(void*));
}
} }
return *this; return *this;
} }
// AddItem
bool bool
BList::AddItem(void *item, int32 index) BList::operator==(const BList& list) const
{
if (&list == this)
return true;
if (list.fItemCount != fItemCount)
return false;
if (fItemCount > 0) {
return memcmp(fObjectList, list.fObjectList,
fItemCount * sizeof(void*)) == 0;
}
return true;
}
bool
BList::operator!=(const BList& list) const
{
return !(*this == list);
}
bool
BList::AddItem(void* item, int32 index)
{ {
if (index < 0 || index > fItemCount) if (index < 0 || index > fItemCount)
return false; return false;
@@ -112,9 +118,8 @@ BList::AddItem(void *item, int32 index)
} }
// AddItem
bool bool
BList::AddItem(void *item) BList::AddItem(void* item)
{ {
bool result = true; bool result = true;
if (fPhysicalSize > fItemCount) { if (fPhysicalSize > fItemCount) {
@@ -130,9 +135,8 @@ BList::AddItem(void *item)
} }
// AddList
bool bool
BList::AddList(const BList *list, int32 index) BList::AddList(const BList* list, int32 index)
{ {
bool result = (list && index >= 0 && index <= fItemCount); bool result = (list && index >= 0 && index <= fItemCount);
if (result && list->fItemCount > 0) { if (result && list->fItemCount > 0) {
@@ -143,16 +147,15 @@ BList::AddList(const BList *list, int32 index)
fItemCount += count; fItemCount += count;
move_items(fObjectList + index, count, fItemCount - index - count); move_items(fObjectList + index, count, fItemCount - index - count);
memcpy(fObjectList + index, list->fObjectList, memcpy(fObjectList + index, list->fObjectList,
list->fItemCount * sizeof(void *)); list->fItemCount * sizeof(void*));
} }
} }
return result; return result;
} }
// AddList
bool bool
BList::AddList(const BList *list) BList::AddList(const BList* list)
{ {
bool result = (list != NULL); bool result = (list != NULL);
if (result && list->fItemCount > 0) { if (result && list->fItemCount > 0) {
@@ -163,16 +166,15 @@ BList::AddList(const BList *list)
if (result) { if (result) {
fItemCount += count; fItemCount += count;
memcpy(fObjectList + index, list->fObjectList, memcpy(fObjectList + index, list->fObjectList,
list->fItemCount * sizeof(void *)); list->fItemCount * sizeof(void*));
} }
} }
return result; return result;
} }
// RemoveItem
bool bool
BList::RemoveItem(void *item) BList::RemoveItem(void* item)
{ {
int32 index = IndexOf(item); int32 index = IndexOf(item);
bool result = (index >= 0); bool result = (index >= 0);
@@ -182,11 +184,10 @@ BList::RemoveItem(void *item)
} }
// RemoveItem void*
void *
BList::RemoveItem(int32 index) BList::RemoveItem(int32 index)
{ {
void *item = NULL; void* item = NULL;
if (index >= 0 && index < fItemCount) { if (index >= 0 && index < fItemCount) {
item = fObjectList[index]; item = fObjectList[index];
move_items(fObjectList + index + 1, -1, fItemCount - index - 1); move_items(fObjectList + index + 1, -1, fItemCount - index - 1);
@@ -198,7 +199,6 @@ BList::RemoveItem(int32 index)
} }
// RemoveItems
bool bool
BList::RemoveItems(int32 index, int32 count) BList::RemoveItems(int32 index, int32 count)
{ {
@@ -219,9 +219,8 @@ BList::RemoveItems(int32 index, int32 count)
} }
//ReplaceItem
bool bool
BList::ReplaceItem(int32 index, void *newItem) BList::ReplaceItem(int32 index, void* newItem)
{ {
bool result = false; bool result = false;
@@ -233,7 +232,6 @@ BList::ReplaceItem(int32 index, void *newItem)
} }
// MakeEmpty
void void
BList::MakeEmpty() BList::MakeEmpty()
{ {
@@ -242,17 +240,17 @@ BList::MakeEmpty()
} }
/* Reordering items. */ // #pragma mark - Reordering items.
// SortItems
void void
BList::SortItems(int (*compareFunc)(const void *, const void *)) BList::SortItems(int (*compareFunc)(const void*, const void*))
{ {
if (compareFunc) if (compareFunc)
qsort(fObjectList, fItemCount, sizeof(void *), compareFunc); qsort(fObjectList, fItemCount, sizeof(void*), compareFunc);
} }
//SwapItems
bool bool
BList::SwapItems(int32 indexA, int32 indexB) BList::SwapItems(int32 indexA, int32 indexB)
{ {
@@ -261,7 +259,7 @@ BList::SwapItems(int32 indexA, int32 indexB)
if (indexA >= 0 && indexA < fItemCount if (indexA >= 0 && indexA < fItemCount
&& indexB >= 0 && indexB < fItemCount) { && indexB >= 0 && indexB < fItemCount) {
void *tmpItem = fObjectList[indexA]; void* tmpItem = fObjectList[indexA];
fObjectList[indexA] = fObjectList[indexB]; fObjectList[indexA] = fObjectList[indexB];
fObjectList[indexB] = tmpItem; fObjectList[indexB] = tmpItem;
@@ -272,26 +270,28 @@ BList::SwapItems(int32 indexA, int32 indexB)
} }
// MoveItem /*! This moves a list item from posititon a to position b, moving the
// This moves a list item from posititon a to position b, moving the appropriate appropriate block of list elements to make up for the move. For example,
// block of list elements to make up for the move. For example, in the array: in the array:
// A B C D E F G H I J A B C D E F G H I J
// Moveing 1(B)->6(G) would result in this: Moveing 1(B)->6(G) would result in this:
// A C D E F G B H I J A C D E F G B H I J
*/
bool bool
BList::MoveItem(int32 fromIndex, int32 toIndex) BList::MoveItem(int32 fromIndex, int32 toIndex)
{ {
if ((fromIndex >= fItemCount) || (toIndex >= fItemCount) || (fromIndex < 0) if ((fromIndex >= fItemCount) || (toIndex >= fItemCount) || (fromIndex < 0)
|| (toIndex < 0)) || (toIndex < 0)) {
return false; return false;
}
void * tmpMover = fObjectList[fromIndex]; void* tmpMover = fObjectList[fromIndex];
if (fromIndex < toIndex) { if (fromIndex < toIndex) {
memmove(fObjectList + fromIndex, fObjectList + fromIndex + 1, memmove(fObjectList + fromIndex, fObjectList + fromIndex + 1,
(toIndex - fromIndex) * sizeof(void *)); (toIndex - fromIndex) * sizeof(void*));
} else if (fromIndex > toIndex) { } else if (fromIndex > toIndex) {
memmove(fObjectList + toIndex + 1, fObjectList + toIndex, memmove(fObjectList + toIndex + 1, fObjectList + toIndex,
(fromIndex - toIndex) * sizeof(void *)); (fromIndex - toIndex) * sizeof(void*));
}; };
fObjectList[toIndex] = tmpMover; fObjectList[toIndex] = tmpMover;
@@ -299,9 +299,10 @@ BList::MoveItem(int32 fromIndex, int32 toIndex)
} }
/* Retrieving items. */ // #pragma mark - Retrieving items.
// ItemAt
void *
void*
BList::ItemAt(int32 index) const BList::ItemAt(int32 index) const
{ {
void *item = NULL; void *item = NULL;
@@ -311,8 +312,7 @@ BList::ItemAt(int32 index) const
} }
// FirstItem void*
void *
BList::FirstItem() const BList::FirstItem() const
{ {
void *item = NULL; void *item = NULL;
@@ -322,45 +322,60 @@ BList::FirstItem() const
} }
// ItemAtFast void*
void *
BList::ItemAtFast(int32 index) const BList::ItemAtFast(int32 index) const
{ {
return fObjectList[index]; return fObjectList[index];
} }
// Items void*
void *
BList::Items() const BList::Items() const
{ {
return fObjectList; return fObjectList;
} }
// LastItem void*
void *
BList::LastItem() const BList::LastItem() const
{ {
void *item = NULL; void* item = NULL;
if (fItemCount > 0) if (fItemCount > 0)
item = fObjectList[fItemCount - 1]; item = fObjectList[fItemCount - 1];
return item; return item;
} }
/* Querying the list. */ // #pragma mark - Querying the list.
// HasItem
bool bool
BList::HasItem(void *item) const BList::HasItem(void* item) const
{
return (IndexOf(item) >= 0);
}
bool
BList::HasItem(const void* item) const
{ {
return (IndexOf(item) >= 0); return (IndexOf(item) >= 0);
} }
// IndexOf
int32 int32
BList::IndexOf(void *item) const BList::IndexOf(void* item) const
{
for (int32 i = 0; i < fItemCount; i++) {
if (fObjectList[i] == item)
return i;
}
return -1;
}
int32
BList::IndexOf(const void* item) const
{ {
for (int32 i = 0; i < fItemCount; i++) { for (int32 i = 0; i < fItemCount; i++) {
if (fObjectList[i] == item) if (fObjectList[i] == item)
@@ -370,7 +385,6 @@ BList::IndexOf(void *item) const
} }
// CountItems
int32 int32
BList::CountItems() const BList::CountItems() const
{ {
@@ -378,49 +392,52 @@ BList::CountItems() const
} }
// IsEmpty
bool bool
BList::IsEmpty() const BList::IsEmpty() const
{ {
return (fItemCount == 0); return fItemCount == 0;
} }
/* Iterating over the list. */ // #pragma mark - Iterating over the list.
//iterate a function over the whole list. If the function outputs a true
//value, then the process is terminated.
/*! Iterate a function over the whole list. If the function outputs a true
value, then the process is terminated.
*/
void void
BList::DoForEach(bool (*func)(void *)) BList::DoForEach(bool (*func)(void*))
{ {
bool terminate = false; int32 index = 0; //set terminate condition variables to go. if (func == NULL)
if (func != NULL) return;
{
while ((!terminate) && (index < fItemCount)) //check terminate condition. bool terminate = false;
{ int32 index = 0;
terminate = func(fObjectList[index]); //reset immediate terminator
index++; //advance along the list. while ((!terminate) && (index < fItemCount)) {
}; terminate = func(fObjectList[index]);
index++;
} }
} }
//same as above, except this function takes an argument. /*! Iterate a function over the whole list. If the function outputs a true
value, then the process is terminated. This version takes an additional
argument which is passed to the function.
*/
void void
BList::DoForEach(bool (*func)(void *, void*), void * arg) BList::DoForEach(bool (*func)(void*, void*), void* arg)
{ {
if (func == NULL)
return;
bool terminate = false; int32 index = 0; bool terminate = false; int32 index = 0;
if (func != NULL) while ((!terminate) && (index < fItemCount)) {
{ terminate = func(fObjectList[index], arg);
while ((!terminate) && (index < fItemCount)) index++;
{
terminate = func(fObjectList[index], arg);
index++;
};
} }
} }
#if (__GNUC__ == 2) #if (__GNUC__ == 2)
// This is somewhat of a hack for backwards compatibility - // This is somewhat of a hack for backwards compatibility -
@@ -441,7 +458,7 @@ AddList__5BListP5BListl(BList* self, BList* list, int32 index)
extern "C" bool extern "C" bool
AddList__5BListP5BList(BList* self, BList* list) AddList__5BListP5BList(BList* self, BList* list)
{ {
return self->AddList((const BList *)list); return self->AddList((const BList*)list);
} }
#endif #endif
@@ -449,9 +466,9 @@ AddList__5BListP5BList(BList* self, BList* list)
void BList::_ReservedList1() {} void BList::_ReservedList1() {}
void BList::_ReservedList2() {} void BList::_ReservedList2() {}
// Resize
// /*! Resizes fObjectList to be large enough to contain count items.
// Resizes fObjectList to be large enough to contain count items. */
bool bool
BList::_ResizeArray(int32 count) BList::_ResizeArray(int32 count)
{ {