예제 #1
0
파일: hashmap.c 프로젝트: po56/projectfiles
int testHash(char *key, int hMapSize) {
	int hash = hashmap_get_hash(key, hMapSize);
	if (hash <= hMapSize) {
		return 0;
	}
	return -1;
}
예제 #2
0
파일: hashmap.c 프로젝트: po56/projectfiles
entry *hashmap_get_entry_by_key(char *key, map *hmap) {

	int hash = hashmap_get_hash(key, hmap->size);

	entry *entryItem = hmap->table[hash];

	while (entryItem != NULL) {
		if (entryItem->key == key) {
			return entryItem;
		}
		entryItem = entryItem->nextInChain;

	}
	return entryItem;
}
예제 #3
0
static inline Node *hashmap_find(var_p_t map, const char *key) {
  int length = strlen(key);
  int index = hashmap_get_hash(key, length) % map->v.m.size;
  Node **table = (Node **)map->v.m.map;
  Node *result = table[index];
  if (result != NULL) {
    int r = tree_compare(key, length, result->key);
    if (r < 0 && result->left != NULL) {
      result = tree_find(&result->left, key, length);
    } else if (r > 0 && result->right != NULL) {
      result = tree_find(&result->right, key, length);
    } else if (r != 0) {
      result = NULL;
    }
  }
  return result;
}
예제 #4
0
static inline Node *hashmap_search(var_p_t map, const char *key, int length) {
  int index = hashmap_get_hash(key, length) % map->v.m.size;
  Node **table = (Node **)map->v.m.map;
  Node *result = table[index];
  if (result == NULL) {
    // new entry
    result = table[index] = tree_create_node(NULL);
  } else {
    int r = tree_compare(key, length, result->key);
    if (r < 0) {
      result = tree_search(&result->left, key, length);
    } else if (r > 0) {
      result = tree_search(&result->right, key, length);
    }
  }
  return result;
}
예제 #5
0
파일: hashmap.c 프로젝트: po56/projectfiles
int testInsert(map *hmap) {

	char *key1 = "12345678";
	char *data1 = "newData";

	int hash = hashmap_get_hash(key1, hmap->size);

	assert(hashmap_insert_entry(key1, data1, hmap) == 0);

	assert(hashmap_get_entry_by_key(key1, hmap) != NULL);

	assert(hashmap_insert_entry(key1, data1, hmap) == 0);

	assert(
			(hmap->table[hash]->key == key1)
					&& (hmap->table[hash]->nextInChain->key == key1));
	return 0;
}
예제 #6
0
파일: hashmap.c 프로젝트: po56/projectfiles
int hashmap_insert_entry(char *key, void *data, map *hmap) {
	entry *newEntry;
	newEntry = (entry*) malloc(sizeof(newEntry));

	newEntry->key = key;
	newEntry->data = data;

	int hash = hashmap_get_hash(key, hmap->size);

	if (!newEntry) {
		return -1;
	}

	newEntry->nextInChain = hmap->table[hash];
	hmap->table[hash] = newEntry;

	return 0;
}