* 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
+16 -6
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved.
* Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _BE_LIST_H
@@ -15,28 +15,33 @@ class BList {
BList(const BList& anotherList);
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);
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*));
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;
void* ItemAtFast(int32 index) const;
// does not check the array bounds!
void* LastItem() const;
@@ -44,25 +49,30 @@ class BList {
// Query
bool HasItem(void* item) const;
bool HasItem(const void* item) const;
int32 IndexOf(void* item) const;
int32 IndexOf(const 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);
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];
};
+107 -90
View File
@@ -1,44 +1,26 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2008, Haiku, Inc.
//
// 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 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 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.
//------------------------------------------------------------------------------
/*
* Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* The Storage kit Team
* Isaac Yonemoto
* Rene Gollent
* Stephan Aßmus
*/
//! BList class provides storage for pointers. Not thread safe.
// Standard Includes -----------------------------------------------------------
#include <List.h>
// System Includes -------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// helper function
static inline
void
static inline void
move_items(void** items, int32 offset, int32 count)
{
if (count > 0 && offset != 0)
@@ -46,9 +28,9 @@ move_items(void** items, int32 offset, int32 count)
}
// constructor
BList::BList(int32 count)
: fObjectList(NULL),
:
fObjectList(NULL),
fPhysicalSize(0),
fItemCount(0),
fBlockSize(count),
@@ -60,9 +42,9 @@ BList::BList(int32 count)
}
// copy constructor
BList::BList(const BList& anotherList)
: fObjectList(NULL),
:
fObjectList(NULL),
fPhysicalSize(0),
fItemCount(0),
fBlockSize(anotherList.fBlockSize)
@@ -71,28 +53,52 @@ BList::BList(const BList& anotherList)
}
// destructor
BList::~BList()
{
free(fObjectList);
}
// =
BList&
BList::operator=(const BList& list)
{
if (&list != this) {
fBlockSize = list.fBlockSize;
if (_ResizeArray(list.fItemCount)) {
fItemCount = list.fItemCount;
memcpy(fObjectList, list.fObjectList, fItemCount * sizeof(void*));
}
}
return *this;
}
// AddItem
bool
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)
{
@@ -112,7 +118,6 @@ BList::AddItem(void *item, int32 index)
}
// AddItem
bool
BList::AddItem(void* item)
{
@@ -130,7 +135,6 @@ BList::AddItem(void *item)
}
// AddList
bool
BList::AddList(const BList* list, int32 index)
{
@@ -150,7 +154,6 @@ BList::AddList(const BList *list, int32 index)
}
// AddList
bool
BList::AddList(const BList* list)
{
@@ -170,7 +173,6 @@ BList::AddList(const BList *list)
}
// RemoveItem
bool
BList::RemoveItem(void* item)
{
@@ -182,7 +184,6 @@ BList::RemoveItem(void *item)
}
// RemoveItem
void*
BList::RemoveItem(int32 index)
{
@@ -198,7 +199,6 @@ BList::RemoveItem(int32 index)
}
// RemoveItems
bool
BList::RemoveItems(int32 index, int32 count)
{
@@ -219,7 +219,6 @@ BList::RemoveItems(int32 index, int32 count)
}
//ReplaceItem
bool
BList::ReplaceItem(int32 index, void* newItem)
{
@@ -233,7 +232,6 @@ BList::ReplaceItem(int32 index, void *newItem)
}
// MakeEmpty
void
BList::MakeEmpty()
{
@@ -242,8 +240,9 @@ BList::MakeEmpty()
}
/* Reordering items. */
// SortItems
// #pragma mark - Reordering items.
void
BList::SortItems(int (*compareFunc)(const void*, const void*))
{
@@ -252,7 +251,6 @@ BList::SortItems(int (*compareFunc)(const void *, const void *))
}
//SwapItems
bool
BList::SwapItems(int32 indexA, int32 indexB)
{
@@ -272,18 +270,20 @@ BList::SwapItems(int32 indexA, int32 indexB)
}
// MoveItem
// This moves a list item from posititon a to position b, moving the appropriate
// block of list elements to make up for the move. For example, in the array:
// A B C D E F G H I J
// Moveing 1(B)->6(G) would result in this:
// A C D E F G B H I J
/*! This moves a list item from posititon a to position b, moving the
appropriate block of list elements to make up for the move. For example,
in the array:
A B C D E F G H I J
Moveing 1(B)->6(G) would result in this:
A C D E F G B H I J
*/
bool
BList::MoveItem(int32 fromIndex, int32 toIndex)
{
if ((fromIndex >= fItemCount) || (toIndex >= fItemCount) || (fromIndex < 0)
|| (toIndex < 0))
|| (toIndex < 0)) {
return false;
}
void* tmpMover = fObjectList[fromIndex];
if (fromIndex < toIndex) {
@@ -299,8 +299,9 @@ BList::MoveItem(int32 fromIndex, int32 toIndex)
}
/* Retrieving items. */
// ItemAt
// #pragma mark - Retrieving items.
void*
BList::ItemAt(int32 index) const
{
@@ -311,7 +312,6 @@ BList::ItemAt(int32 index) const
}
// FirstItem
void*
BList::FirstItem() const
{
@@ -322,7 +322,6 @@ BList::FirstItem() const
}
// ItemAtFast
void*
BList::ItemAtFast(int32 index) const
{
@@ -330,7 +329,6 @@ BList::ItemAtFast(int32 index) const
}
// Items
void*
BList::Items() const
{
@@ -338,7 +336,6 @@ BList::Items() const
}
// LastItem
void*
BList::LastItem() const
{
@@ -349,8 +346,9 @@ BList::LastItem() const
}
/* Querying the list. */
// HasItem
// #pragma mark - Querying the list.
bool
BList::HasItem(void* item) const
{
@@ -358,7 +356,13 @@ BList::HasItem(void *item) const
}
// IndexOf
bool
BList::HasItem(const void* item) const
{
return (IndexOf(item) >= 0);
}
int32
BList::IndexOf(void* item) const
{
@@ -370,7 +374,17 @@ BList::IndexOf(void *item) const
}
// CountItems
int32
BList::IndexOf(const void* item) const
{
for (int32 i = 0; i < fItemCount; i++) {
if (fObjectList[i] == item)
return i;
}
return -1;
}
int32
BList::CountItems() const
{
@@ -378,49 +392,52 @@ BList::CountItems() const
}
// IsEmpty
bool
BList::IsEmpty() const
{
return (fItemCount == 0);
return fItemCount == 0;
}
/* Iterating over the list. */
//iterate a function over the whole list. If the function outputs a true
//value, then the process is terminated.
// #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.
*/
void
BList::DoForEach(bool (*func)(void*))
{
bool terminate = false; int32 index = 0; //set terminate condition variables to go.
if (func != NULL)
{
while ((!terminate) && (index < fItemCount)) //check terminate condition.
{
terminate = func(fObjectList[index]); //reset immediate terminator
index++; //advance along the list.
};
}
if (func == NULL)
return;
bool terminate = false;
int32 index = 0;
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
BList::DoForEach(bool (*func)(void*, void*), void* arg)
{
if (func == NULL)
return;
bool terminate = false; int32 index = 0;
if (func != NULL)
{
while ((!terminate) && (index < fItemCount))
{
while ((!terminate) && (index < fItemCount)) {
terminate = func(fObjectList[index], arg);
index++;
};
}
}
#if (__GNUC__ == 2)
// This is somewhat of a hack for backwards compatibility -
@@ -449,9 +466,9 @@ AddList__5BListP5BList(BList* self, BList* list)
void BList::_ReservedList1() {}
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
BList::_ResizeArray(int32 count)
{