summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-07-26 21:58:35 +0000
committerMike Buland <eichlan@xagasoft.com>2006-07-26 21:58:35 +0000
commit76ed3c164662f4ee4c109bb2054c61c99ea86251 (patch)
tree301d4687f95a457304b0e796984b62fa89992718 /src
parent579c3ac445043122b0a702bdb2542d9ea404b62e (diff)
downloadlibbu++-76ed3c164662f4ee4c109bb2054c61c99ea86251.tar.gz
libbu++-76ed3c164662f4ee4c109bb2054c61c99ea86251.tar.bz2
libbu++-76ed3c164662f4ee4c109bb2054c61c99ea86251.tar.xz
libbu++-76ed3c164662f4ee4c109bb2054c61c99ea86251.zip
Added the not-yet-working hash class. More thought must be done. This doesn't
actually change any existing code really just adds a new class that you can't use because it's commented out. I'll probably move it to a branch.
Diffstat (limited to 'src')
-rw-r--r--src/hash.cpp1
-rw-r--r--src/hash.h106
-rw-r--r--src/hashable.cpp1
-rw-r--r--src/hashable.h10
-rw-r--r--src/staticstring.cpp25
-rw-r--r--src/staticstring.h11
-rw-r--r--src/test/exception/exception.cpp8
-rw-r--r--src/test/hash/main.cpp10
8 files changed, 165 insertions, 7 deletions
diff --git a/src/hash.cpp b/src/hash.cpp
new file mode 100644
index 0000000..b4aac77
--- /dev/null
+++ b/src/hash.cpp
@@ -0,0 +1 @@
#include "hash.h"
diff --git a/src/hash.h b/src/hash.h
new file mode 100644
index 0000000..8385bb9
--- /dev/null
+++ b/src/hash.h
@@ -0,0 +1,106 @@
1#ifndef HASH_H
2#define HASH_H
3/*
4#include <stddef.h>
5#include <string.h>
6#include "hashable.h"
7
8#define bitsToBytes( n ) (n/8+(n%8>0 ? 1 : 0))
9
10template<class key, class value>
11class Hash
12{
13 class iterator
14 {
15 friend class Hash<key, value>;
16 private:
17 iterator( Hash<key, value> *hsh, int nIndex, key keyval, bool bInsert ) :
18 hsh( hsh ),
19 nIndex( nIndex ),
20 keyval( keyval ),
21 bInsert( bInsert )
22 {
23 }
24
25 public:
26 iterator() :
27 hsh( NULL ),
28 nIndex( -1 )
29 {
30 }
31
32 iterator &operator=( iterator &src )
33 {
34 this->hsh = src.hsh;
35 this->nIndex = src.nIndex;
36 }
37
38 iterator &operator=( value &src )
39 {
40 if( bInsert )
41 printf("You wanted to insert %d\n", src );
42 else
43 printf("You wanted to insert %d\n", src );
44 }
45
46 private:
47 Hash<key, value> *hsh;
48 int nIndex;
49 bool bInsert;
50 key keyval;
51 };
52
53 template<class refval>
54 class VRef
55 {
56 public:
57 VRef( refval &data ) :
58 data( data )
59 {
60 }
61 refval &data;
62 };
63
64public:
65 Hash() :
66 nCapacity( 11 ),
67 nFilled( 0 ),
68 bFilled( NULL ),
69 aKeys( NULL ),
70 aValues( NULL ),
71 aHashCodes( NULL )
72 {
73 int nKeysSize = bitsToBytes( nCapacity );
74 bFilled = new unsigned char[ nKeysSize ];
75 memset( bFilled, 0, nKeysSize );
76
77 aKeys = new VRef<key>*[nCapacity];
78 aValues = new value[nCapacity];
79 }
80
81 virtual ~Hash()
82 {
83 }
84
85 iterator operator[]( key keyval )
86 {
87 //iterator i( this, 4, keyval, true );
88 //return i;
89 printf("%s\n", keyval.getString() );
90 }
91
92 int hasKey( key keyval )
93 {
94 printf("%s\n", keyval.getString() );
95 }
96
97private:
98 int nCapacity;
99 int nFilled;
100 unsigned char *bFilled;
101 VRef<key> **aKeys;
102 unsigned long int *aHashCodes;
103 value *aValues;
104};
105*/
106#endif
diff --git a/src/hashable.cpp b/src/hashable.cpp
new file mode 100644
index 0000000..8565956
--- /dev/null
+++ b/src/hashable.cpp
@@ -0,0 +1 @@
#include "hashable.h"
diff --git a/src/hashable.h b/src/hashable.h
new file mode 100644
index 0000000..33ff8b8
--- /dev/null
+++ b/src/hashable.h
@@ -0,0 +1,10 @@
1#ifndef HASHABLE_H
2#define HASHABLE_H
3/*
4class Hashable
5{
6public:
7 virtual unsigned long int getHashCode() = 0;
8};
9*/
10#endif
diff --git a/src/staticstring.cpp b/src/staticstring.cpp
index fa61e62..9eac92e 100644
--- a/src/staticstring.cpp
+++ b/src/staticstring.cpp
@@ -25,6 +25,13 @@ StaticString::StaticString( const char *lpNewStr, int nNewLen )
25 setString( lpNewStr, nNewLen ); 25 setString( lpNewStr, nNewLen );
26} 26}
27 27
28StaticString::StaticString( const char *lpNewStr )
29{
30 lpStr = NULL;
31 nLen = 0;
32 setString( lpNewStr, -1 );
33}
34
28StaticString::StaticString( StaticString &xSrcStr, int nNewLen ) 35StaticString::StaticString( StaticString &xSrcStr, int nNewLen )
29{ 36{
30 lpStr = NULL; 37 lpStr = NULL;
@@ -32,6 +39,13 @@ StaticString::StaticString( StaticString &xSrcStr, int nNewLen )
32 setString( xSrcStr, nNewLen ); 39 setString( xSrcStr, nNewLen );
33} 40}
34 41
42StaticString::StaticString( StaticString &xSrcStr )
43{
44 lpStr = NULL;
45 nLen = 0;
46 setString( xSrcStr, -1 );
47}
48
35StaticString::~StaticString() 49StaticString::~StaticString()
36{ 50{
37 if( lpStr != NULL ) delete[] lpStr; 51 if( lpStr != NULL ) delete[] lpStr;
@@ -225,3 +239,14 @@ bool StaticString::operator!=( StaticString &str )
225 for(; *a == *b; a++, b++ ) if( *a == '\0' && *b == '\0' ) return false; 239 for(; *a == *b; a++, b++ ) if( *a == '\0' && *b == '\0' ) return false;
226 return true; 240 return true;
227} 241}
242
243unsigned long int StaticString::getHashCode()
244{
245 unsigned long int nPos = nLen;
246 for( const char *s = (const char *)lpStr; *s; s++ )
247 {
248 nPos = *s + (nPos << 6) + (nPos << 16) - nPos;
249 }
250 return nPos;
251}
252
diff --git a/src/staticstring.h b/src/staticstring.h
index 732e860..68ca7ea 100644
--- a/src/staticstring.h
+++ b/src/staticstring.h
@@ -3,6 +3,7 @@
3 3
4#include <string> 4#include <string>
5#include "serializable.h" 5#include "serializable.h"
6//#include "hashable.h"
6 7
7/** 8/**
8 * Simple string managing class. Allows for dynamically allocated string data 9 * Simple string managing class. Allows for dynamically allocated string data
@@ -11,12 +12,14 @@
11 * and making accessing meta-info like length fast and reliable as well. 12 * and making accessing meta-info like length fast and reliable as well.
12 *@author Mike Buland 13 *@author Mike Buland
13 */ 14 */
14class StaticString : public Serializable 15class StaticString : public Serializable//, public Hashable
15{ 16{
16public: 17public:
17 StaticString(); 18 StaticString();
18 StaticString( const char *lpNewStr, int nNewLen=-1 ); 19 StaticString( const char *lpNewStr, int nNewLen );
19 StaticString( StaticString &xSrcStr, int nNewLen=-1 ); 20 StaticString( const char *lpNewStr );
21 StaticString( StaticString &xSrcStr, int nNewLen );
22 StaticString( StaticString &xSrcStr );
20 StaticString( int nLength ); 23 StaticString( int nLength );
21 virtual ~StaticString(); 24 virtual ~StaticString();
22 25
@@ -47,6 +50,8 @@ public:
47 50
48 virtual void serialize( class Serializer &ar ); 51 virtual void serialize( class Serializer &ar );
49 52
53 virtual unsigned long int getHashCode();
54
50private: 55private:
51 char *lpStr; 56 char *lpStr;
52 int nLen; 57 int nLen;
diff --git a/src/test/exception/exception.cpp b/src/test/exception/exception.cpp
index 33dcd5e..6417692 100644
--- a/src/test/exception/exception.cpp
+++ b/src/test/exception/exception.cpp
@@ -1,16 +1,16 @@
1#include <iostream> 1#include <iostream>
2#include "exception.h" 2#include "exceptions.h"
3 3
4int main() 4int main()
5{ 5{
6 try 6 try
7 { 7 {
8 throw Exception( 42, "There was an error on line: %d", __LINE__ ); 8 throw ExceptionBase( 42, "There was an error on line: %d", __LINE__ );
9 } 9 }
10 catch( Exception &e ) 10 catch( ExceptionBase &e )
11 { 11 {
12 std::cout << "Error "<< e.getErrorCode() << ": " << e.what() << "\n"; 12 std::cout << "Error "<< e.getErrorCode() << ": " << e.what() << "\n";
13 } 13 }
14 14
15 throw Exception( 112, "This exception wasn't caught!"); 15 throw ExceptionBase( 112, "This exception wasn't caught!");
16} 16}
diff --git a/src/test/hash/main.cpp b/src/test/hash/main.cpp
new file mode 100644
index 0000000..d0f5fa6
--- /dev/null
+++ b/src/test/hash/main.cpp
@@ -0,0 +1,10 @@
1#include "hash.h"
2#include "staticstring.h"
3
4int main()
5{
6 //Hash<class StaticString, int> sTest;
7
8 //sTest.hasKey("hello");
9}
10