ramfs: Drop OpenTracker OpenHashTable and the custom AreaUtils.
Following removal of NodeChildTable, they aren't used.
This commit is contained in:
@@ -1,138 +0,0 @@
|
||||
/*
|
||||
* Copyright 2003, Ingo Weinhold, [email protected].
|
||||
* All rights reserved. Distributed under the terms of the MIT license.
|
||||
*/
|
||||
|
||||
#include <OS.h>
|
||||
|
||||
#include "AreaUtils.h"
|
||||
#include "DebugSupport.h"
|
||||
#include "Misc.h"
|
||||
|
||||
#ifndef USE_STANDARD_FUNCTIONS
|
||||
#define USE_STANDARD_FUNCTIONS 0
|
||||
#endif
|
||||
|
||||
|
||||
// area_info_for
|
||||
static
|
||||
status_t
|
||||
area_info_for(void *address, area_info *info)
|
||||
{
|
||||
status_t error = B_OK;
|
||||
if (address) {
|
||||
// get the area ID for the ptr
|
||||
area_id area = area_for(address);
|
||||
// check if supplied pointer points to the beginning of the area
|
||||
if (area >= 0)
|
||||
error = get_area_info(area, info);
|
||||
else
|
||||
error = area;
|
||||
} else
|
||||
error = B_BAD_VALUE;
|
||||
return error;
|
||||
}
|
||||
|
||||
// calloc
|
||||
void *
|
||||
AreaUtils::calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
//PRINT(("AreaUtils::calloc(%lu, %lu)\n", nmemb, size));
|
||||
return AreaUtils::malloc(nmemb * size);
|
||||
}
|
||||
|
||||
// free
|
||||
void
|
||||
AreaUtils::free(void *ptr)
|
||||
{
|
||||
//PRINT(("AreaUtils::free(%p)\n", ptr));
|
||||
#if USE_STANDARD_FUNCTIONS
|
||||
return ::free(ptr);
|
||||
#else
|
||||
if (ptr) {
|
||||
// get the area for the pointer
|
||||
area_info info;
|
||||
if (area_info_for(ptr, &info) == B_OK) {
|
||||
if (ptr == info.address) {
|
||||
// everything is fine, delete the area
|
||||
delete_area(info.area);
|
||||
} else {
|
||||
INFORM("WARNING: AreaUtils::free(%p): area begin is %p."
|
||||
"Ignored.\n", ptr, info.address);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// malloc
|
||||
void *
|
||||
AreaUtils::malloc(size_t size)
|
||||
{
|
||||
//PRINT(("AreaUtils::malloc(%lu)\n", size));
|
||||
#if USE_STANDARD_FUNCTIONS
|
||||
return ::malloc(size);
|
||||
#else
|
||||
void *address = NULL;
|
||||
if (size > 0) {
|
||||
// round to multiple of page size
|
||||
size = (size + B_PAGE_SIZE - 1) / B_PAGE_SIZE * B_PAGE_SIZE;
|
||||
// create an area
|
||||
#if USER
|
||||
area_id area = create_area("AreaUtils::malloc", &address,
|
||||
B_ANY_ADDRESS, size, B_NO_LOCK,
|
||||
B_WRITE_AREA | B_READ_AREA);
|
||||
#else
|
||||
area_id area = create_area("AreaUtils::malloc", &address,
|
||||
B_ANY_KERNEL_ADDRESS, size, B_FULL_LOCK,
|
||||
B_READ_AREA | B_WRITE_AREA);
|
||||
#endif
|
||||
if (area < 0)
|
||||
address = NULL;
|
||||
}
|
||||
return address;
|
||||
#endif
|
||||
}
|
||||
|
||||
// realloc
|
||||
void *
|
||||
AreaUtils::realloc(void * ptr, size_t size)
|
||||
{
|
||||
//PRINT(("AreaUtils::realloc(%p, %lu)\n", ptr, size))
|
||||
#if USE_STANDARD_FUNCTIONS
|
||||
return ::realloc(ptr, size);
|
||||
#else
|
||||
void *newAddress = NULL;
|
||||
if (size == 0) {
|
||||
AreaUtils::free(ptr);
|
||||
} else if (ptr) {
|
||||
// get the area for the pointer
|
||||
area_info info;
|
||||
if (area_info_for(ptr, &info) == B_OK) {
|
||||
if (ptr == info.address) {
|
||||
// round to multiple of page size
|
||||
size = (size + B_PAGE_SIZE - 1) / B_PAGE_SIZE * B_PAGE_SIZE;
|
||||
if (size == info.size) {
|
||||
// nothing to do
|
||||
newAddress = ptr;
|
||||
} else if (resize_area(info.area, size) == B_OK) {
|
||||
// resizing the area went fine
|
||||
newAddress = ptr;
|
||||
} else {
|
||||
// resizing the area failed: we need to allocate a new one
|
||||
newAddress = AreaUtils::malloc(size);
|
||||
if (newAddress) {
|
||||
memcpy(newAddress, ptr, min(size, info.size));
|
||||
delete_area(info.area);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
INFORM("WARNING: AreaUtils::realloc(%p): area begin is %p."
|
||||
"Ignored.\n", ptr, info.address);
|
||||
}
|
||||
}
|
||||
}
|
||||
return newAddress;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
* Copyright 2003, Ingo Weinhold, [email protected].
|
||||
* All rights reserved. Distributed under the terms of the MIT license.
|
||||
*/
|
||||
#ifndef AREA_UTILS_H
|
||||
#define AREA_UTILS_H
|
||||
|
||||
namespace AreaUtils {
|
||||
|
||||
void *calloc(size_t nmemb, size_t size);
|
||||
void free(void *ptr);
|
||||
void *malloc(size_t size);
|
||||
void *realloc(void * ptr, size_t size);
|
||||
|
||||
};
|
||||
|
||||
#endif // AREA_UTILS_H
|
||||
@@ -8,7 +8,6 @@ DEFINES += DEBUG_APP="\\\"ramfs\\\"" ;
|
||||
KernelAddon ramfs
|
||||
:
|
||||
AllocationInfo.cpp
|
||||
AreaUtils.cpp
|
||||
Attribute.cpp
|
||||
AttributeIndex.cpp
|
||||
AttributeIndexImpl.cpp
|
||||
|
||||
@@ -1,247 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007, Ingo Weinhold, [email protected].
|
||||
* All rights reserved. Distributed under the terms of the MIT license.
|
||||
*/
|
||||
#ifndef NODE_CHILD_TABLE_H
|
||||
#define NODE_CHILD_TABLE_H
|
||||
|
||||
#include "AllocationInfo.h"
|
||||
#include "DebugSupport.h"
|
||||
#include "Misc.h"
|
||||
#include "Node.h"
|
||||
#include "OpenHashTable.h"
|
||||
|
||||
// NodeChildHashElement
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
class NodeChildHashElement : public OpenHashElement {
|
||||
private:
|
||||
typedef NodeChildHashElement<ParentNode, NodeChild> Element;
|
||||
public:
|
||||
|
||||
NodeChildHashElement() : OpenHashElement(), fID(-1), fChild(NULL)
|
||||
{
|
||||
fNext = -1;
|
||||
}
|
||||
|
||||
static inline uint32 HashFor(ino_t id, const char *name)
|
||||
{
|
||||
return node_child_hash(id, name);
|
||||
}
|
||||
|
||||
static inline uint32 HashFor(ParentNode *parent, NodeChild *child)
|
||||
{
|
||||
return node_child_hash(parent->GetID(), child->GetName());
|
||||
}
|
||||
|
||||
inline uint32 Hash() const
|
||||
{
|
||||
return HashFor(fID, fChild->GetName());
|
||||
}
|
||||
|
||||
inline bool Equals(ino_t id, const char *name)
|
||||
{
|
||||
return (fID == id && !strcmp(fChild->GetName(), name));
|
||||
}
|
||||
|
||||
inline bool operator==(const OpenHashElement &_element) const
|
||||
{
|
||||
const Element &element = static_cast<const Element&>(_element);
|
||||
return Equals(element.fID, element.fChild->GetName());
|
||||
}
|
||||
|
||||
inline void Adopt(Element &element)
|
||||
{
|
||||
fID = element.fID;
|
||||
fChild = element.fChild;
|
||||
}
|
||||
|
||||
ino_t fID;
|
||||
NodeChild *fChild;
|
||||
};
|
||||
|
||||
// NodeChildTable
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
class NodeChildTable {
|
||||
public:
|
||||
NodeChildTable();
|
||||
~NodeChildTable();
|
||||
|
||||
status_t InitCheck() const;
|
||||
|
||||
status_t AddNodeChild(ParentNode *node, NodeChild *child);
|
||||
status_t AddNodeChild(ino_t, NodeChild *child);
|
||||
status_t RemoveNodeChild(ParentNode *node, NodeChild *child);
|
||||
status_t RemoveNodeChild(ino_t id, NodeChild *child);
|
||||
status_t RemoveNodeChild(ino_t id, const char *name);
|
||||
NodeChild *GetNodeChild(ino_t id, const char *name);
|
||||
|
||||
protected:
|
||||
typedef NodeChildHashElement<ParentNode, NodeChild> Element;
|
||||
|
||||
private:
|
||||
Element *_FindElement(ino_t id, const char *name) const;
|
||||
|
||||
protected:
|
||||
OpenHashElementArray<Element> fElementArray;
|
||||
OpenHashTable<Element, OpenHashElementArray<Element> > fTable;
|
||||
};
|
||||
|
||||
// define convenient instantiation types
|
||||
|
||||
// DirectoryEntryTable
|
||||
class DirectoryEntryTable : public NodeChildTable<Directory, Entry> {
|
||||
public:
|
||||
DirectoryEntryTable() {}
|
||||
~DirectoryEntryTable() {}
|
||||
|
||||
void GetAllocationInfo(AllocationInfo &info)
|
||||
{
|
||||
info.AddDirectoryEntryTableAllocation(fTable.ArraySize(),
|
||||
fTable.VectorSize(),
|
||||
sizeof(Element),
|
||||
fTable.CountElements());
|
||||
}
|
||||
};
|
||||
|
||||
// NodeAttributeTable
|
||||
class NodeAttributeTable : public NodeChildTable<Node, Attribute> {
|
||||
public:
|
||||
NodeAttributeTable() {}
|
||||
~NodeAttributeTable() {}
|
||||
|
||||
void GetAllocationInfo(AllocationInfo &info)
|
||||
{
|
||||
info.AddNodeAttributeTableAllocation(fTable.ArraySize(),
|
||||
fTable.VectorSize(),
|
||||
sizeof(Element),
|
||||
fTable.CountElements());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// NodeChildTable implementation
|
||||
|
||||
// constructor
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
NodeChildTable<ParentNode, NodeChild>::NodeChildTable()
|
||||
: fElementArray(1000),
|
||||
fTable(1000, &fElementArray)
|
||||
{
|
||||
}
|
||||
|
||||
// destructor
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
NodeChildTable<ParentNode, NodeChild>::~NodeChildTable()
|
||||
{
|
||||
}
|
||||
|
||||
// InitCheck
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
status_t
|
||||
NodeChildTable<ParentNode, NodeChild>::InitCheck() const
|
||||
{
|
||||
RETURN_ERROR(fTable.InitCheck() && fElementArray.InitCheck()
|
||||
? B_OK : B_NO_MEMORY);
|
||||
}
|
||||
|
||||
// AddNodeChild
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
status_t
|
||||
NodeChildTable<ParentNode, NodeChild>::AddNodeChild(ParentNode *node,
|
||||
NodeChild *child)
|
||||
{
|
||||
status_t error = (node && child ? B_OK : B_BAD_VALUE);
|
||||
if (error == B_OK)
|
||||
error = AddNodeChild(node->GetID(), child);
|
||||
return error;
|
||||
}
|
||||
|
||||
// AddNodeChild
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
status_t
|
||||
NodeChildTable<ParentNode, NodeChild>::AddNodeChild(ino_t id,
|
||||
NodeChild *child)
|
||||
{
|
||||
status_t error = (child ? B_OK : B_BAD_VALUE);
|
||||
if (error == B_OK) {
|
||||
Element *element = fTable.Add(Element::HashFor(id, child->GetName()));
|
||||
if (element) {
|
||||
element->fID = id;
|
||||
element->fChild = child;
|
||||
} else
|
||||
SET_ERROR(error, B_NO_MEMORY);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
// RemoveNodeChild
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
status_t
|
||||
NodeChildTable<ParentNode, NodeChild>::RemoveNodeChild(ParentNode *node,
|
||||
NodeChild *child)
|
||||
{
|
||||
status_t error = (node && child ? B_OK : B_BAD_VALUE);
|
||||
if (error == B_OK)
|
||||
error = RemoveNodeChild(node->GetID(), child->GetName());
|
||||
return error;
|
||||
}
|
||||
|
||||
// RemoveNodeChild
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
status_t
|
||||
NodeChildTable<ParentNode, NodeChild>::RemoveNodeChild(ino_t id,
|
||||
NodeChild *child)
|
||||
{
|
||||
status_t error = (child ? B_OK : B_BAD_VALUE);
|
||||
if (error == B_OK)
|
||||
error = RemoveNodeChild(id, child->GetName());
|
||||
return error;
|
||||
}
|
||||
|
||||
// RemoveNodeChild
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
status_t
|
||||
NodeChildTable<ParentNode, NodeChild>::RemoveNodeChild(ino_t id,
|
||||
const char *name)
|
||||
{
|
||||
status_t error = B_OK;
|
||||
if (Element *element = _FindElement(id, name))
|
||||
fTable.Remove(element);
|
||||
else
|
||||
error = B_ERROR;
|
||||
return error;
|
||||
}
|
||||
|
||||
// GetNodeChild
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
NodeChild *
|
||||
NodeChildTable<ParentNode, NodeChild>::GetNodeChild(ino_t id,
|
||||
const char *name)
|
||||
{
|
||||
NodeChild *child = NULL;
|
||||
if (Element *element = _FindElement(id, name))
|
||||
child = element->fChild;
|
||||
return child;
|
||||
}
|
||||
|
||||
// _FindElement
|
||||
template<typename ParentNode, typename NodeChild>
|
||||
typename NodeChildTable<ParentNode, NodeChild>::Element *
|
||||
NodeChildTable<ParentNode, NodeChild>::_FindElement(ino_t id,
|
||||
const char *name) const
|
||||
{
|
||||
Element *element = fTable.FindFirst(Element::HashFor(id, name));
|
||||
while (element && !element->Equals(id, name)) {
|
||||
if (element->fNext >= 0)
|
||||
element = fTable.ElementAt(element->fNext);
|
||||
else
|
||||
element = NULL;
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
|
||||
// undefine the PRINT from <Debug.h>
|
||||
//#undef PRINT
|
||||
|
||||
#endif // NODE_CHILD_TABLE_H
|
||||
@@ -1,498 +0,0 @@
|
||||
/*
|
||||
Open Tracker License
|
||||
|
||||
Terms and Conditions
|
||||
|
||||
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
|
||||
|
||||
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 applies to all licensees
|
||||
and 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 TITLE, MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
BE INCORPORATED 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.
|
||||
|
||||
Except as contained in this notice, the name of Be Incorporated shall not be
|
||||
used in advertising or otherwise to promote the sale, use or other dealings in
|
||||
this Software without prior written authorization from Be Incorporated.
|
||||
|
||||
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
|
||||
of Be Incorporated in the United States and other countries. Other brand product
|
||||
names are registered trademarks or trademarks of their respective holders.
|
||||
All rights reserved.
|
||||
*/
|
||||
|
||||
// bonefish:
|
||||
// * removed need for exceptions
|
||||
// * fixed warnings
|
||||
// * implemented rehashing
|
||||
// * hash array and element vector use areas for allocations
|
||||
// TODO:
|
||||
// * shrinking of element vectors
|
||||
|
||||
// Hash table with open addresssing
|
||||
|
||||
#ifndef __OPEN_HASH_TABLE__
|
||||
#define __OPEN_HASH_TABLE__
|
||||
|
||||
#include <malloc.h>
|
||||
#include <new>
|
||||
|
||||
#include "AreaUtils.h"
|
||||
#include "Misc.h"
|
||||
|
||||
// don't include <Debug.h>
|
||||
#ifndef ASSERT
|
||||
# define ASSERT(E) (void)0
|
||||
#endif
|
||||
#define TRESPASS() (void)0
|
||||
|
||||
//namespace BPrivate {
|
||||
|
||||
template <class Element>
|
||||
class ElementVector {
|
||||
// element vector for OpenHashTable needs to implement this
|
||||
// interface
|
||||
public:
|
||||
Element &At(int32 index);
|
||||
Element *Add();
|
||||
int32 IndexOf(const Element &) const;
|
||||
void Remove(int32 index);
|
||||
};
|
||||
|
||||
class OpenHashElement {
|
||||
public:
|
||||
uint32 Hash() const;
|
||||
bool operator==(const OpenHashElement &) const;
|
||||
void Adopt(OpenHashElement &);
|
||||
// low overhead copy, original element is in undefined state
|
||||
// after call (calls Adopt on BString members, etc.)
|
||||
int32 fNext;
|
||||
};
|
||||
|
||||
const uint32 kPrimes [] = {
|
||||
509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139,
|
||||
524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, 67108859,
|
||||
134217689, 268435399, 536870909, 1073741789, 2147483647, 0
|
||||
};
|
||||
|
||||
template <class Element, class ElementVec = ElementVector<Element> >
|
||||
class OpenHashTable {
|
||||
public:
|
||||
OpenHashTable(int32 minSize, ElementVec *elementVector = 0,
|
||||
float maxLoadFactor = 0.8);
|
||||
// it is up to the subclass of OpenHashTable to supply
|
||||
// elementVector
|
||||
~OpenHashTable();
|
||||
|
||||
bool InitCheck() const;
|
||||
|
||||
void SetElementVector(ElementVec *elementVector);
|
||||
|
||||
Element *FindFirst(uint32 elementHash) const;
|
||||
Element *Add(uint32 elementHash);
|
||||
|
||||
void Remove(Element *);
|
||||
|
||||
// when calling Add, any outstanding element pointer may become
|
||||
// invalid; to deal with this, get the element index and restore
|
||||
// it after the add
|
||||
int32 ElementIndex(const Element *) const;
|
||||
Element *ElementAt(int32 index) const;
|
||||
|
||||
int32 ArraySize() const;
|
||||
int32 VectorSize() const;
|
||||
int32 CountElements() const;
|
||||
|
||||
protected:
|
||||
static int32 OptimalSize(int32 minSize);
|
||||
|
||||
private:
|
||||
bool _RehashIfNeeded();
|
||||
bool _Rehash();
|
||||
|
||||
int32 fArraySize;
|
||||
int32 fInitialSize;
|
||||
int32 fElementCount;
|
||||
int32 *fHashArray;
|
||||
ElementVec *fElementVector;
|
||||
float fMaxLoadFactor;
|
||||
};
|
||||
|
||||
template <class Element>
|
||||
class OpenHashElementArray : public ElementVector<Element> {
|
||||
// this is a straightforward implementation of an element vector
|
||||
// deleting is handled by linking deleted elements into a free list
|
||||
// the vector never shrinks
|
||||
public:
|
||||
OpenHashElementArray(int32 initialSize);
|
||||
~OpenHashElementArray();
|
||||
|
||||
bool InitCheck() const;
|
||||
|
||||
Element &At(int32 index);
|
||||
const Element &At(int32 index) const;
|
||||
Element *Add(const Element &);
|
||||
Element *Add();
|
||||
void Remove(int32 index);
|
||||
int32 IndexOf(const Element &) const;
|
||||
int32 Size() const;
|
||||
|
||||
private:
|
||||
Element *fData;
|
||||
int32 fSize;
|
||||
int32 fNextFree;
|
||||
int32 fNextDeleted;
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
OpenHashTable<Element, ElementVec>::OpenHashTable(int32 minSize,
|
||||
ElementVec *elementVector, float maxLoadFactor)
|
||||
: fArraySize(OptimalSize(minSize)),
|
||||
fInitialSize(fArraySize),
|
||||
fElementCount(0),
|
||||
fElementVector(elementVector),
|
||||
fMaxLoadFactor(maxLoadFactor)
|
||||
{
|
||||
// sanity check the maximal load factor
|
||||
if (fMaxLoadFactor < 0.5)
|
||||
fMaxLoadFactor = 0.5;
|
||||
// allocate and init the array
|
||||
fHashArray = (int32*)AreaUtils::calloc(fArraySize, sizeof(int32));
|
||||
if (fHashArray) {
|
||||
for (int32 index = 0; index < fArraySize; index++)
|
||||
fHashArray[index] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
OpenHashTable<Element, ElementVec>::~OpenHashTable()
|
||||
{
|
||||
AreaUtils::free(fHashArray);
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
bool
|
||||
OpenHashTable<Element, ElementVec>::InitCheck() const
|
||||
{
|
||||
return (fHashArray && fElementVector);
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
int32
|
||||
OpenHashTable<Element, ElementVec>::OptimalSize(int32 minSize)
|
||||
{
|
||||
for (int32 index = 0; ; index++)
|
||||
if (!kPrimes[index] || kPrimes[index] >= (uint32)minSize)
|
||||
return (int32)kPrimes[index];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
Element *
|
||||
OpenHashTable<Element, ElementVec>::FindFirst(uint32 hash) const
|
||||
{
|
||||
ASSERT(fElementVector);
|
||||
hash %= fArraySize;
|
||||
if (fHashArray[hash] < 0)
|
||||
return 0;
|
||||
|
||||
return &fElementVector->At(fHashArray[hash]);
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
int32
|
||||
OpenHashTable<Element, ElementVec>::ElementIndex(const Element *element) const
|
||||
{
|
||||
return fElementVector->IndexOf(*element);
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
Element *
|
||||
OpenHashTable<Element, ElementVec>::ElementAt(int32 index) const
|
||||
{
|
||||
return &fElementVector->At(index);
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
int32
|
||||
OpenHashTable<Element, ElementVec>::ArraySize() const
|
||||
{
|
||||
return fArraySize;
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
int32
|
||||
OpenHashTable<Element, ElementVec>::VectorSize() const
|
||||
{
|
||||
return fElementVector->Size();
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
int32
|
||||
OpenHashTable<Element, ElementVec>::CountElements() const
|
||||
{
|
||||
return fElementCount;
|
||||
}
|
||||
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
Element *
|
||||
OpenHashTable<Element, ElementVec>::Add(uint32 hash)
|
||||
{
|
||||
ASSERT(fElementVector);
|
||||
_RehashIfNeeded();
|
||||
hash %= fArraySize;
|
||||
Element *result = fElementVector->Add();
|
||||
if (result) {
|
||||
result->fNext = fHashArray[hash];
|
||||
fHashArray[hash] = fElementVector->IndexOf(*result);
|
||||
fElementCount++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
void
|
||||
OpenHashTable<Element, ElementVec>::Remove(Element *element)
|
||||
{
|
||||
_RehashIfNeeded();
|
||||
uint32 hash = element->Hash() % fArraySize;
|
||||
int32 next = fHashArray[hash];
|
||||
ASSERT(next >= 0);
|
||||
|
||||
if (&fElementVector->At(next) == element) {
|
||||
fHashArray[hash] = element->fNext;
|
||||
fElementVector->Remove(next);
|
||||
fElementCount--;
|
||||
return;
|
||||
}
|
||||
|
||||
for (int32 index = next; index >= 0; ) {
|
||||
// look for an existing match in table
|
||||
next = fElementVector->At(index).fNext;
|
||||
if (next < 0) {
|
||||
TRESPASS();
|
||||
return;
|
||||
}
|
||||
|
||||
if (&fElementVector->At(next) == element) {
|
||||
fElementVector->At(index).fNext = element->fNext;
|
||||
fElementVector->Remove(next);
|
||||
fElementCount--;
|
||||
return;
|
||||
}
|
||||
index = next;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Element, class ElementVec>
|
||||
void
|
||||
OpenHashTable<Element, ElementVec>::SetElementVector(ElementVec *elementVector)
|
||||
{
|
||||
fElementVector = elementVector;
|
||||
}
|
||||
|
||||
// _RehashIfNeeded
|
||||
template<class Element, class ElementVec>
|
||||
bool
|
||||
OpenHashTable<Element, ElementVec>::_RehashIfNeeded()
|
||||
{
|
||||
// The load factor range [fMaxLoadFactor / 3, fMaxLoadFactor] is fine,
|
||||
// I think. After rehashing the load factor will be about
|
||||
// fMaxLoadFactor * 2 / 3, respectively fMaxLoadFactor / 2.
|
||||
float loadFactor = (float)fElementCount / (float)fArraySize;
|
||||
if (loadFactor > fMaxLoadFactor
|
||||
|| (fArraySize > fInitialSize && loadFactor < fMaxLoadFactor / 3)) {
|
||||
return _Rehash();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// _Rehash
|
||||
template<class Element, class ElementVec>
|
||||
bool
|
||||
OpenHashTable<Element, ElementVec>::_Rehash()
|
||||
{
|
||||
bool result = true;
|
||||
int32 newSize = max(fInitialSize,
|
||||
int32(fElementCount * 1.73 * fMaxLoadFactor));
|
||||
newSize = OptimalSize(newSize);
|
||||
if (newSize != fArraySize) {
|
||||
PRINT(("_Rehash(): %lu -> %lu (currently %lu entries)\n", fArraySize, newSize,
|
||||
fElementCount));
|
||||
// allocate a new array
|
||||
int32 *newHashArray
|
||||
= (int32*)AreaUtils::calloc(newSize, sizeof(int32));
|
||||
if (newHashArray) {
|
||||
// init the new hash array
|
||||
for (int32 index = 0; index < newSize; index++)
|
||||
newHashArray[index] = -1;
|
||||
// iterate through all elements and put them into the new
|
||||
// hash array
|
||||
for (int i = 0; i < fArraySize; i++) {
|
||||
int32 index = fHashArray[i];
|
||||
while (index >= 0) {
|
||||
// insert the element in the new array
|
||||
Element &element = fElementVector->At(index);
|
||||
int32 next = element.fNext;
|
||||
uint32 hash = (element.Hash() % newSize);
|
||||
element.fNext = newHashArray[hash];
|
||||
newHashArray[hash] = index;
|
||||
// next element in old list
|
||||
index = next;
|
||||
}
|
||||
}
|
||||
// delete the old array and set the new one
|
||||
AreaUtils::free(fHashArray);
|
||||
fHashArray = newHashArray;
|
||||
fArraySize = newSize;
|
||||
} else
|
||||
result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
template<class Element>
|
||||
OpenHashElementArray<Element>::OpenHashElementArray(int32 initialSize)
|
||||
: fSize(initialSize),
|
||||
fNextFree(0),
|
||||
fNextDeleted(-1)
|
||||
{
|
||||
fData = (Element*)AreaUtils::calloc((size_t)initialSize, sizeof(Element));
|
||||
}
|
||||
|
||||
template<class Element>
|
||||
OpenHashElementArray<Element>::~OpenHashElementArray()
|
||||
{
|
||||
AreaUtils::free(fData);
|
||||
}
|
||||
|
||||
template<class Element>
|
||||
bool
|
||||
OpenHashElementArray<Element>::InitCheck() const
|
||||
{
|
||||
return fData;
|
||||
}
|
||||
|
||||
template<class Element>
|
||||
Element &
|
||||
OpenHashElementArray<Element>::At(int32 index)
|
||||
{
|
||||
ASSERT(index < fSize);
|
||||
return fData[index];
|
||||
}
|
||||
|
||||
template<class Element>
|
||||
const Element &
|
||||
OpenHashElementArray<Element>::At(int32 index) const
|
||||
{
|
||||
ASSERT(index < fSize);
|
||||
return fData[index];
|
||||
}
|
||||
|
||||
template<class Element>
|
||||
int32
|
||||
OpenHashElementArray<Element>::IndexOf(const Element &element) const
|
||||
{
|
||||
int32 result = &element - fData;
|
||||
if (result < 0 || result > fSize)
|
||||
return -1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class Element>
|
||||
int32
|
||||
OpenHashElementArray<Element>::Size() const
|
||||
{
|
||||
return fSize;
|
||||
}
|
||||
|
||||
|
||||
template<class Element>
|
||||
Element *
|
||||
OpenHashElementArray<Element>::Add(const Element &newElement)
|
||||
{
|
||||
Element *element = Add();
|
||||
if (element)
|
||||
element.Adopt(newElement);
|
||||
return element;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
const int32 kGrowChunk = 10;
|
||||
#else
|
||||
const int32 kGrowChunk = 1024;
|
||||
#endif
|
||||
|
||||
template<class Element>
|
||||
Element *
|
||||
OpenHashElementArray<Element>::Add()
|
||||
{
|
||||
int32 index = fNextFree;
|
||||
if (fNextDeleted >= 0) {
|
||||
index = fNextDeleted;
|
||||
fNextDeleted = At(index).fNext;
|
||||
} else if (fNextFree >= fSize - 1) {
|
||||
int32 newSize = fSize + kGrowChunk;
|
||||
/*
|
||||
Element *newData = (Element *)calloc((size_t)newSize , sizeof(Element));
|
||||
if (!newData)
|
||||
return NULL;
|
||||
memcpy(newData, fData, fSize * sizeof(Element));
|
||||
free(fData);
|
||||
*/
|
||||
Element *newData = (Element*)AreaUtils::realloc(fData,
|
||||
(size_t)newSize * sizeof(Element));
|
||||
if (!newData)
|
||||
return NULL;
|
||||
|
||||
fData = newData;
|
||||
fSize = newSize;
|
||||
index = fNextFree;
|
||||
fNextFree++;
|
||||
} else
|
||||
fNextFree++;
|
||||
|
||||
new (&At(index)) Element;
|
||||
// call placement new to initialize the element properly
|
||||
ASSERT(At(index).fNext == -1);
|
||||
|
||||
return &At(index);
|
||||
}
|
||||
|
||||
template<class Element>
|
||||
void
|
||||
OpenHashElementArray<Element>::Remove(int32 index)
|
||||
{
|
||||
// delete by chaining empty elements in a single linked
|
||||
// list, reusing the next field
|
||||
ASSERT(index < fSize);
|
||||
At(index).~Element();
|
||||
// call the destructor explicitly to destroy the element
|
||||
// properly
|
||||
At(index).fNext = fNextDeleted;
|
||||
fNextDeleted = index;
|
||||
}
|
||||
|
||||
//} // namespace BPrivate
|
||||
|
||||
//using namespace BPrivate;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user