Import backup from 2005-09-01
This commit is contained in:
parent
9e03bfc8b6
commit
396c5f3452
@ -31,6 +31,7 @@ public:
|
||||
/* Constructors/Destructor */
|
||||
vector<type>();
|
||||
vector<type>(unsigned int size);
|
||||
vector<type>(vector<type> & v1);
|
||||
~vector<type>();
|
||||
|
||||
/* Returns the size of the vector */
|
||||
@ -67,6 +68,16 @@ vector<type>::vector<type>(unsigned int size)
|
||||
myAllocated = size;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type>::vector<type>(vector<type> & v1)
|
||||
{
|
||||
mySize = v1.mySize;
|
||||
myAllocated = v1.myAllocated;
|
||||
myData = new (type *)[myAllocated];
|
||||
for (int i = 0; i < mySize; i++)
|
||||
myData[i] = new type(*v1.myData[i]);
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type>::~vector<type>()
|
||||
{
|
||||
|
134
kernel/proc/hash.cpp
Normal file
134
kernel/proc/hash.cpp
Normal file
@ -0,0 +1,134 @@
|
||||
// hash.cpp
|
||||
// Author: Josh Holtrop
|
||||
// Date: 08/28/05
|
||||
// Modified: 08/28/05
|
||||
// Implements a basic hash table (u32_t -> void*)
|
||||
|
||||
#define __HOS_CPP__
|
||||
|
||||
#include "hos_defines.h"
|
||||
#include "hash.h"
|
||||
#include "lang/vector.h"
|
||||
|
||||
extern "C"{
|
||||
#include "display/kout.h"
|
||||
#include "mm/vmm.h"
|
||||
}
|
||||
|
||||
/* Hash Primes come from
|
||||
* http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
|
||||
*/
|
||||
const u32_t hash_primes[] =
|
||||
{53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317,
|
||||
196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843,
|
||||
50331653, 100663319, 201326611, 402653189, 805306457, 1610612741};
|
||||
|
||||
hash::hash()
|
||||
{
|
||||
mySizeIndex = 0;
|
||||
mySize = 0;
|
||||
for (u32_t i = 0; i < hash_primes[mySizeIndex]; i++)
|
||||
myTable.add(vector<hash_entry_t>());
|
||||
}
|
||||
|
||||
int hash::add(u32_t key, void *val)
|
||||
{
|
||||
kprintf("$1$");
|
||||
u32_t hashed = hash_key(key);
|
||||
vector<hash_entry_t *> *vec = myTable[hashed];
|
||||
kprintf("$2$");
|
||||
if (!vec)
|
||||
myTable[hashed] = vec = new vector<hash_entry_t *>;
|
||||
for (u32_t i = 0; i < vec->size(); i++)
|
||||
if ( (*vec)[i]->key == key )
|
||||
return -1; // key exists already
|
||||
kprintf("$3$");
|
||||
hash_entry_t *he = (hash_entry_t *) New(hash_entry_t);
|
||||
kprintf("(");
|
||||
for (u32_t i = 0; i < vec->size(); i++)
|
||||
kprintf("%d,", (*vec)[i]->key);
|
||||
kprintf(")");
|
||||
he->key = key;
|
||||
he->data = val;
|
||||
vec->add(he);
|
||||
mySize++;
|
||||
kprintf("*%d*\t", mySize);
|
||||
if ( mySize > (hash_primes[mySizeIndex] << 1) ) // re-hash
|
||||
{
|
||||
kprintf("*1*");
|
||||
vector<hash_entry_t *> **oldTable = myTable;
|
||||
mySizeIndex++;
|
||||
myTable = new (vector<hash_entry_t *> *)[hash_primes[mySizeIndex]];
|
||||
for (u32_t i = 0; i < hash_primes[mySizeIndex]; i++)
|
||||
myTable[i] = NULL;
|
||||
mySize = 0;
|
||||
kprintf("*2*");
|
||||
for (u32_t i = 0; i < hash_primes[mySizeIndex - 1]; i++)
|
||||
{
|
||||
if (oldTable[i])
|
||||
{
|
||||
for (u32_t j = 0; j < oldTable[i]->size(); j++)
|
||||
{
|
||||
add( (*(oldTable[i]))[j]->key,
|
||||
(*(oldTable[i]))[j]->data );
|
||||
kfree( (*(oldTable[i]))[j] );
|
||||
}
|
||||
delete oldTable[i];
|
||||
}
|
||||
}
|
||||
kprintf("*3*");
|
||||
delete oldTable;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *hash::get(u32_t key)
|
||||
{
|
||||
u32_t hashed = hash_key(key);
|
||||
vector<hash_entry_t *> *vec = myTable[hashed];
|
||||
if (!vec)
|
||||
return NULL;
|
||||
for (u32_t i = 0; i < vec->size(); i++)
|
||||
if ( (*vec)[i]->key == key )
|
||||
return (*vec)[i]->data;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int hash::remove(u32_t key)
|
||||
{
|
||||
u32_t hashed = hash_key(key);
|
||||
vector<hash_entry_t *> *vec = myTable[hashed];
|
||||
if (!vec)
|
||||
return -1;
|
||||
for (u32_t i = 0; i < vec->size(); i++)
|
||||
if ( (*vec)[i]->key == key )
|
||||
{
|
||||
kfree( (*vec)[i] );
|
||||
vec->remove(i); // remove key
|
||||
mySize--;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int hash::exists(u32_t key)
|
||||
{
|
||||
u32_t hashed = hash_key(key);
|
||||
vector<hash_entry_t *> *vec = myTable[hashed];
|
||||
if (!vec)
|
||||
return 0;
|
||||
for (u32_t i = 0; i < vec->size(); i++)
|
||||
if ( (*vec)[i]->key == key )
|
||||
return 1; // key exists
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32_t hash::hash_key(u32_t key)
|
||||
{
|
||||
return key % hash_primes[mySizeIndex];
|
||||
}
|
||||
|
||||
u32_t hash::size()
|
||||
{
|
||||
return mySize;
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
// hash.h
|
||||
// Author: Josh Holtrop
|
||||
// Date: 08/28/05
|
||||
// Modified: 08/28/05
|
||||
// Implements a basic hash table (u32_t -> void*)
|
||||
|
||||
#ifndef __HOS_HASH_H__
|
||||
#define __HOS_HASH_H__ __HOS_HASH_H__
|
||||
|
||||
#include "hos_defines.h"
|
||||
#include "lang/vector.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u32_t key;
|
||||
void *data;
|
||||
} hash_entry_t;
|
||||
|
||||
class hash
|
||||
{
|
||||
private:
|
||||
/* index into hash_primes[] */
|
||||
u32_t mySizeIndex;
|
||||
|
||||
/* How many values in the hash table */
|
||||
u32_t mySize;
|
||||
|
||||
/* array of hash entries */
|
||||
vector< vector<hash_entry_t> > myTable;
|
||||
|
||||
/* hash the key to a table index */
|
||||
u32_t hash_key(u32_t key);
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
hash();
|
||||
|
||||
/* Add a value to the hash table */
|
||||
int add(u32_t key, void *val);
|
||||
|
||||
/* Retrieve a value from the hash table */
|
||||
void *get(u32_t key);
|
||||
|
||||
/* Remove a value from the hash table */
|
||||
int remove(u32_t key);
|
||||
|
||||
/* Check if the given key exists in the hash table */
|
||||
int exists(u32_t key);
|
||||
|
||||
/* How many values are in the hash table */
|
||||
u32_t size();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user