Implemented hash functions for DirectoryBlock.

Added HashIterator::InitCheck() method (as the allocation of the buffer
might fail).
Added a HashIterator::Goto() method to jump to the specified index in
the hash (handy for hash lookup).


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4707 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2003-09-16 02:33:48 +00:00
parent 479e4ab5cf
commit 09fb7d4bbc
2 changed files with 42 additions and 4 deletions

View File

@ -73,10 +73,30 @@ BaseBlock::ValidateCheckSum() const
// #pragma mark -
int32
DirectoryBlock::HashIndexFor(const char *name)
char
DirectoryBlock::ToUpperChar(int32 type, char c) const
{
return 0;
// Taken from Ralph Babel's "The Amiga Guru Book" (1993), section 15.3.4.3
if (type == DT_AMIGA_OFS || type == DT_AMIGA_FFS)
return c >= 'a' && c <= 'z' ? c + ('A' - 'a') : c;
return (c >= '\340' && c <= '\376' && c != '\367')
|| (c >= 'a' && c <= 'z') ? c + ('A' - 'a') : c;
}
int32
DirectoryBlock::HashIndexFor(int32 type, const char *name) const
{
int32 hash = strlen(name);
while (name[0]) {
hash = (hash * 13 + ToUpperChar(type, name[0])) & 0x7ff;
name++;
}
return hash % HashSize();
}
@ -131,6 +151,21 @@ HashIterator::~HashIterator()
}
status_t
HashIterator::InitCheck()
{
return fData != NULL ? B_OK : B_NO_MEMORY;
}
void
HashIterator::Goto(int32 index)
{
fCurrent = index;
fBlock = fDirectory.HashValueAt(index);
}
NodeBlock *
HashIterator::GetNext(int32 &block)
{

View File

@ -87,7 +87,8 @@ class DirectoryBlock : public NodeBlock {
DirectoryBlock(int32 blockSize) : NodeBlock(blockSize) {}
DirectoryBlock(void *data, int32 blockSize) : NodeBlock(data, blockSize) {}
int32 HashIndexFor(const char *name);
char ToUpperChar(int32 type, char c) const;
int32 HashIndexFor(int32 type, const char *name) const;
int32 HashValueAt(int32 index) const;
int32 NextHashValue(int32 &index) const;
@ -143,6 +144,8 @@ class HashIterator {
HashIterator(int32 device, DirectoryBlock &node);
~HashIterator();
status_t InitCheck();
void Goto(int32 index);
NodeBlock *GetNext(int32 &block);
void Rewind();