simpleHash.c 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
#include "simpleHash.h"

uint32_t mySimpleHash(const char* data, size_t n, uint32_t seed) {
  // Similar to murmur hash
  const uint32_t m = 0xc6a4a793;
  const uint32_t r = 24;
  const char*    limit = data + n;
  uint32_t       h = seed ^ (n * m);

  // Pick up four bytes at a time
  while (data + 4 <= limit) {
    // uint32_t w = DecodeFixed32(data);
    uint32_t w;
    memcpy(&w, data, 4);

    data += 4;
    h += w;
    h *= m;
    h ^= (h >> 16);
  }

  // Pick up remaining bytes
  switch (limit - data) {
    case 3:
      h += (unsigned char)(data[2]) << 16;
      do {
      } while (0);
    case 2:
      h += (unsigned char)(data[1]) << 8;
      do {
      } while (0);
    case 1:
      h += (unsigned char)(data[0]);
      h *= m;
      h ^= (h >> r);
      break;
  }
  return h;
}

int insertCStrSimpleHash(struct SimpleHash* ths, char* key, char* data) {
  return insertSimpleHash(ths, key, strlen(key) + 1, data, strlen(data) + 1);
}

int removeCStrSimpleHash(struct SimpleHash* ths, char* key) { return removeSimpleHash(ths, key, strlen(key) + 1); }

SimpleHashNode** findCStrSimpleHash(struct SimpleHash* ths, char* key) {
  return findSimpleHash(ths, key, strlen(key) + 1);
}

int insertSimpleHash(struct SimpleHash* ths, char* key, size_t keyLen, char* data, size_t dataLen) {
  SimpleHashNode** pp = ths->find(ths, key, keyLen);
  if (*pp != NULL) {
    fprintf(stderr, "insertSimpleHash, already has key \n");
    return -1;
  }

  SimpleHashNode* node = malloc(sizeof(*node));
  node->hashCode = ths->hashFunc(key, keyLen);
  node->key = malloc(keyLen);
  node->keyLen = keyLen;
  memcpy(node->key, key, keyLen);
  node->data = malloc(dataLen);
  node->dataLen = dataLen;
  memcpy(node->data, data, dataLen);
  node->next = NULL;

  // printf("insertSimpleHash: <%s, %ld, %s, %ld, %u> \n", node->key, node->keyLen, node->data, node->dataLen,
  //       node->hashCode);

  size_t index = node->hashCode & (ths->length - 1);

  SimpleHashNode* ptr = ths->table[index];
  if (ptr != NULL) {
    node->next = ptr;
    ths->table[index] = node;

  } else {
    ths->table[index] = node;
  }
  ths->elems++;
  if (ths->elems > 2 * ths->length) {
    ths->resize(ths);
  }

  return 0;
}

int removeSimpleHash(struct SimpleHash* ths, char* key, size_t keyLen) {
  SimpleHashNode** pp = ths->find(ths, key, keyLen);
  if (*pp == NULL) {
    fprintf(stderr, "removeSimpleHash, key not exist \n");
    return -1;
  }

  SimpleHashNode* del = *pp;
  *pp = del->next;
  free(del->key);
  free(del->data);
  free(del);
  ths->elems--;

  return 0;
}

SimpleHashNode** findSimpleHash(struct SimpleHash* ths, char* key, size_t keyLen) {
  uint32_t hashCode = ths->hashFunc(key, keyLen);
  // size_t   index = hashCode % ths->length;
  size_t index = hashCode & (ths->length - 1);

  // printf("findSimpleHash: %s %ld %u \n", key, keyLen, hashCode);

  SimpleHashNode** pp = &(ths->table[index]);
  while (*pp != NULL && ((*pp)->hashCode != hashCode || memcmp(key, (*pp)->key, keyLen) != 0)) {
    pp = &((*pp)->next);
  }

  return pp;
}

void printCStrSimpleHash(struct SimpleHash* ths) {
  printf("\n--- printCStrSimpleHash: elems:%d length:%d \n", ths->elems, ths->length);
  for (size_t i = 0; i < ths->length; ++i) {
    SimpleHashNode* ptr = ths->table[i];
    if (ptr != NULL) {
      printf("%zu: ", i);
      while (ptr != NULL) {
        printf("<%u, %s, %ld, %s, %ld> ", ptr->hashCode, (char*)ptr->key, ptr->keyLen, (char*)ptr->data, ptr->dataLen);
        ptr = ptr->next;
      }
      printf("\n");
    }
  }
  printf("---------------\n");
}

void destroySimpleHash(struct SimpleHash* ths) {
  for (size_t i = 0; i < ths->length; ++i) {
    SimpleHashNode* ptr = ths->table[i];
    while (ptr != NULL) {
      SimpleHashNode* tmp = ptr;
      ptr = ptr->next;
      free(tmp->key);
      free(tmp->data);
      free(tmp);
    }
  }

  ths->length = 0;
  ths->elems = 0;
  free(ths->table);
  free(ths);
}

void resizeSimpleHash(struct SimpleHash* ths) {
  uint32_t new_length = ths->length;
  while (new_length < ths->elems) {
    new_length *= 2;
  }

  printf("resizeSimpleHash: %p from %u to %u \n", ths, ths->length, new_length);

  SimpleHashNode** new_table = malloc(new_length * sizeof(SimpleHashNode*));
  memset(new_table, 0, new_length * sizeof(SimpleHashNode*));

  uint32_t count = 0;
  for (uint32_t i = 0; i < ths->length; i++) {
    if (ths->table[i] == NULL) {
      continue;
    }

    SimpleHashNode* it = ths->table[i];
    while (it != NULL) {
      SimpleHashNode* move_node = it;
      it = it->next;

      // move move_node
      move_node->next = NULL;
      size_t index = move_node->hashCode & (new_length - 1);

      SimpleHashNode* ptr = new_table[index];
      if (ptr != NULL) {
        move_node->next = ptr;
        new_table[index] = move_node;
      } else {
        new_table[index] = move_node;
      }
      count++;
    }
  }

  assert(ths->elems == count);
  free(ths->table);
  ths->table = new_table;
  ths->length = new_length;
}

uint32_t simpleHashFunc(const char* key, size_t keyLen) { return mySimpleHash(key, keyLen, 1); }

struct SimpleHash* newSimpleHash(size_t length) {
  struct SimpleHash* ths = malloc(sizeof(*ths));

  ths->length = length;
  ths->elems = 0;
  ths->table = malloc(length * sizeof(SimpleHashNode*));
  memset(ths->table, 0, length * sizeof(SimpleHashNode*));

  ths->insert = insertSimpleHash;
  ths->remove = removeSimpleHash;
  ths->find = findSimpleHash;
  ths->insert_cstr = insertCStrSimpleHash;
  ths->remove_cstr = removeCStrSimpleHash;
  ths->find_cstr = findCStrSimpleHash;
  ths->print_cstr = printCStrSimpleHash;
  ths->destroy = destroySimpleHash;
  ths->resize = resizeSimpleHash;
  ths->hashFunc = simpleHashFunc;
}