View difference between Paste ID: gx4gnYy8 and
SHOW: | | - or go back to the newest paste.
1-
1+
#include <cstdio>
2
#include <cstring>
3
#include <cstdlib>
4
#include <fcntl.h>
5
#include <unistd.h>
6
//#include "socket.h"
7
8
#define STRINGHASH_ALLOC_INCREMENT 20
9
10
void raise(const char* msg) {
11
  printf("\n\n%s\n\n", msg);
12
  exit(-1);
13
}
14
15
class StringHashHelper {
16
  public:
17
    StringHashHelper() {
18
      data = NULL;
19
    }
20
    
21
    StringHashHelper(const char* input) {
22
      setData(input);
23
    }
24
    
25
    ~StringHashHelper() {
26
      if(data != NULL) {delete [] data;}
27
    }
28
    
29
    operator const char*() {
30
      return (const char*)data;
31
    }
32
    
33
    void operator=(const char* input) {
34
      setData(input);
35
    }
36
    
37
  private:
38
    char* data;
39
    void setData(const char* input) {
40
      if(data != NULL) {delete [] data;}
41
      int inLen = strlen(input) + 1;
42
      data = new char[inLen];
43
      if(data == NULL) {raise("Allocation failure in StringHashHelper::operator=()");}
44
      strcpy(data, input);
45
    }
46
};
47
48
class StringHash {
49
  public:
50
    StringHash() {
51
      allocSize = STRINGHASH_ALLOC_INCREMENT;
52
      hashSize = 0;
53
      hkeys = new char*[allocSize];
54
      if(hkeys == NULL) {raise("Allocation failure in StringHash::StringHash() (1)");}
55
      values = new StringHashHelper[allocSize];
56
      if(values == NULL) {raise("Allocation failure in StringHash::StringHash() (2)");}
57
    }
58
    
59
    ~StringHash() {
60
      for(int i = 0; i < hashSize; ++i) {delete [] hkeys[i];}
61
      delete [] hkeys;
62
      delete [] values;
63
    }
64
    
65
    StringHashHelper &operator[](const char* key) {
66
      for(int i = 0; i < hashSize; ++i) {
67
        if(strcmp(hkeys[i], key) == 0) {return values[i];}
68
      }
69
      if(hashSize == allocSize) {
70
        allocSize += STRINGHASH_ALLOC_INCREMENT;
71
        char** tempKeys = new char*[allocSize];
72
        if(tempKeys == NULL) {raise("Allocation failure in StringHash::addEntry() (1)");}
73
        StringHashHelper* tempVals = new StringHashHelper[allocSize];
74
        if(tempVals == NULL) {raise("Allocation failure in StringHash::addEntry() (2)");}
75
        int tkl;
76
        for(int i = 0; i < hashSize; ++i) {
77
          tempKeys[i] = hkeys[i];
78
          tempVals[i] = (const char*)values[i];
79
        }
80
        delete [] hkeys;
81
        hkeys = tempKeys;
82
        delete [] values;
83
        values = tempVals;
84
      }
85
      
86
      int keyLen = strlen(key) + 1;
87
      hkeys[hashSize] = new char[keyLen];
88
      if(hkeys[hashSize] == NULL) {raise("Allocation failure in StringHash::addEntry() (3)");}
89
      strcpy(hkeys[hashSize], key);
90
      return values[hashSize++];
91
    }
92
    
93
    const char** keys() {
94
      return (const char**)hkeys;
95
    }
96
    
97
    int size() {
98
      return hashSize;
99
    }
100
    
101
    void show() {
102
      for(int i = 0; i < hashSize; ++i) {
103
        printf("%i) \"", i);
104
        printf(hkeys[i]);
105
        printf("\" -> \"");
106
        printf(values[i]);
107
        printf("\"\n");
108
      }
109
    }
110
    
111
  private:
112
    int allocSize;
113
    int hashSize;
114
    char** hkeys;
115
    StringHashHelper* values;
116
};
117
118
int main() {
119
  StringHash hash;
120
  hash["a"]  = "This is a test.\n";
121
  hash["b"] = "This is a second line.\n";
122
  hash["c"]  = "The test has three lines.\n";
123
  printf(hash["a"]);
124
  printf(hash["c"]);
125
  printf(hash["b"]);
126
  printf(hash["a"]);
127
  return 0;
128
}