Files
haiku-beta6/src/kits/support/List.cpp
T

397 lines
7.3 KiB
C++
Raw Normal View History

//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// 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
// Description: BList class provides storage for pointers.
// Not thread safe.
//------------------------------------------------------------------------------
2002-07-09 12:24:59 +00:00
#include "List.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// helper function
static inline
void
move_items(void** items, int32 offset, int32 count)
{
if (count > 0 && offset != 0)
memmove(items + offset, items, count * sizeof(void*));
}
2002-07-09 12:24:59 +00:00
// constructor
BList::BList(int32 count)
: fObjectList(NULL),
fPhysicalSize(0),
fItemCount(0),
fBlockSize(count)
{
if (fBlockSize <= 0)
fBlockSize = 1;
Resize(fItemCount);
}
2002-07-09 12:24:59 +00:00
// copy constructor
BList::BList(const BList& anotherList)
: fObjectList(NULL),
fPhysicalSize(0),
fItemCount(0),
fBlockSize(anotherList.fBlockSize)
{
*this = anotherList;
}
2002-07-09 12:24:59 +00:00
// destructor
BList::~BList()
{
free(fObjectList);
}
// =
BList&
BList::operator =(const BList &list)
2002-07-09 12:24:59 +00:00
{
fBlockSize = list.fBlockSize;
Resize(list.fItemCount);
memcpy(fObjectList, list.fObjectList, fItemCount * sizeof(void*));
return *this;
2002-07-09 12:24:59 +00:00
}
2002-07-09 12:24:59 +00:00
// AddItem
bool
BList::AddItem(void *item, int32 index)
{
bool result = (index >= 0 && index <= fItemCount
&& Resize(fItemCount + 1));
if (result) {
move_items(fObjectList + index, 1, fItemCount - index - 1);
fObjectList[index] = item;
}
return result;
}
// AddItem
bool
BList::AddItem(void *item)
{
bool result = true;
if ((int32)fPhysicalSize > fItemCount) {
fObjectList[fItemCount] = item;
fItemCount++;
} else {
if ((result = Resize(fItemCount + 1)))
fObjectList[fItemCount - 1] = item;
}
return result;
}
// AddList
bool
BList::AddList(BList *list, int32 index)
{
bool result = (list && index >= 0 && index <= fItemCount);
if (result && list->fItemCount > 0) {
int32 count = list->fItemCount;
result = Resize(fItemCount + count);
if (result) {
move_items(fObjectList + index, count, fItemCount - index - count);
memcpy(fObjectList + index, list->fObjectList,
list->fItemCount * sizeof(void *));
}
}
return result;
}
// AddList
bool
BList::AddList(BList *list)
{
bool result = (list);
if (result && list->fItemCount > 0) {
int32 index = fItemCount;
int32 count = list->fItemCount;
result = Resize(fItemCount + count);
if (result) {
memcpy(fObjectList + index, list->fObjectList,
list->fItemCount * sizeof(void *));
}
}
return result;
}
// RemoveItem
bool
BList::RemoveItem(void *item)
2002-07-09 12:24:59 +00:00
{
int32 index = IndexOf(item);
bool result = (index >= 0);
if (result)
RemoveItem(index);
return result;
2002-07-09 12:24:59 +00:00
}
// RemoveItem
void *
BList::RemoveItem(int32 index)
2002-07-09 12:24:59 +00:00
{
void *item = NULL;
if (index >= 0 && index < fItemCount) {
item = fObjectList[index];
move_items(fObjectList + index + 1, -1, fItemCount - index - 1);
Resize(fItemCount - 1);
2002-07-09 12:24:59 +00:00
}
return item;
2002-07-09 12:24:59 +00:00
}
// RemoveItems
bool
BList::RemoveItems(int32 index, int32 count)
2002-07-09 12:24:59 +00:00
{
bool result = (index >= 0 && index <= fItemCount);
if (result) {
if (index + count > fItemCount)
count = fItemCount - index;
if (count > 0) {
move_items(fObjectList + index + count, -count,
fItemCount - index - count);
Resize(fItemCount - count);
} else
result = false;
2002-07-09 12:24:59 +00:00
}
return result;
2002-07-09 12:24:59 +00:00
}
//ReplaceItem
bool
BList::ReplaceItem(int32 index, void *newItem)
2002-07-09 12:24:59 +00:00
{
bool result = false;
if (index >= 0 && index < fItemCount) {
fObjectList[index] = newItem;
result = true;
}
return result;
2002-07-09 12:24:59 +00:00
}
// MakeEmpty
void
BList::MakeEmpty()
2002-07-09 12:24:59 +00:00
{
Resize(0);
2002-07-09 12:24:59 +00:00
}
/* Reordering items. */
// SortItems
void
BList::SortItems(int (*compareFunc)(const void *, const void *))
2002-07-09 12:24:59 +00:00
{
if (compareFunc)
qsort(fObjectList, fItemCount, sizeof(void *), compareFunc);
2002-07-09 12:24:59 +00:00
}
//SwapItems
2002-07-09 12:24:59 +00:00
bool
BList::SwapItems(int32 indexA, int32 indexB)
2002-07-09 12:24:59 +00:00
{
bool result = false;
if (indexA >= 0 && indexA < fItemCount
&& indexB >= 0 && indexB < fItemCount) {
void *tmpItem = fObjectList[indexA];
fObjectList[indexA] = fObjectList[indexB];
fObjectList[indexB] = tmpItem;
result = true;
}
return result;
2002-07-09 12:24:59 +00:00
}
//MoveItem
bool
BList::MoveItem(int32 fromIndex, int32 toIndex)
{
//TODO: Implement
return false;
}
/* Retrieving items. */
2002-07-09 12:24:59 +00:00
// ItemAt
void *
BList::ItemAt(int32 index) const
{
void *item = NULL;
if (index >= 0 && index < fItemCount)
item = fObjectList[index];
return item;
}
// FirstItem
void *
BList::FirstItem() const
{
void *item = NULL;
if (fItemCount > 0)
item = fObjectList[0];
return item;
}
// ItemAtFast
void *
BList::ItemAtFast(int32 index) const
{
return fObjectList[index];
}
2002-07-09 12:24:59 +00:00
// Items
void *
BList::Items() const
{
return fObjectList;
}
2002-07-09 12:24:59 +00:00
// LastItem
void *
BList::LastItem() const
{
void *item = NULL;
if (fItemCount > 0)
item = fObjectList[fItemCount - 1];
return item;
}
/* Querying the list. */
// HasItem
2002-07-09 12:24:59 +00:00
bool
BList::HasItem(void *item) const
2002-07-09 12:24:59 +00:00
{
return (IndexOf(item) >= 0);
2002-07-09 12:24:59 +00:00
}
// IndexOf
int32
BList::IndexOf(void *item) const
2002-07-09 12:24:59 +00:00
{
for (int32 i = 0; i < fItemCount; i++) {
if (fObjectList[i] == item)
return i;
2002-07-09 12:24:59 +00:00
}
return -1;
2002-07-09 12:24:59 +00:00
}
// CountItems
int32
BList::CountItems() const
2002-07-09 12:24:59 +00:00
{
return fItemCount;
2002-07-09 12:24:59 +00:00
}
// IsEmpty
bool
BList::IsEmpty() const
2002-07-09 12:24:59 +00:00
{
return (fItemCount == 0);
2002-07-09 12:24:59 +00:00
}
/* Iterating over the list. */
// DoForEach
2002-07-09 12:24:59 +00:00
void
BList::DoForEach(bool (*func)(void *))
2002-07-09 12:24:59 +00:00
{
if (func) {
for (int32 i = 0; i < fItemCount; i++)
(*func)(fObjectList[i]);
}
2002-07-09 12:24:59 +00:00
}
// DoForEach
2002-07-09 12:24:59 +00:00
void
BList::DoForEach(bool (*func)(void *, void *), void *arg2)
2002-07-09 12:24:59 +00:00
{
if (func) {
for (int32 i = 0; i < fItemCount; i++)
(*func)(fObjectList[i], arg2);
}
2002-07-09 12:24:59 +00:00
}
// FBC
void BList::_ReservedList1() {}
void BList::_ReservedList2() {}
2002-07-09 12:24:59 +00:00
// Resize
//
// Resizes fObjectList to be large enough to contain count items.
// fItemCount is adjusted accordingly.
bool
BList::Resize(int32 count)
{
bool result = true;
// calculate the new physical size
int32 newSize = count;
if (newSize <= 0)
newSize = 1;
newSize = ((newSize - 1) / fBlockSize + 1) * fBlockSize;
// resize if necessary
if ((size_t)newSize != fPhysicalSize) {
void** newObjectList
= (void**)realloc(fObjectList, newSize * sizeof(void*));
if (newObjectList) {
fObjectList = newObjectList;
fPhysicalSize = newSize;
} else
result = false;
}
if (result)
fItemCount = count;
return result;
}