php hash代码下载,PHP中的哈希表 hash_insert
[php]代碼庫int hash_insert(HashTable *ht, char *key, void *value)
{
// check if we need to resize the hashtable
resize_hash_table_if_needed(ht); // 哈希表不固定大小,當插入的內容快占滿哈表的存儲空間
// 將對哈希表進行擴容, 以便容納所有的元素
int index = HASH_INDEX(ht, key); // 找到key所映射到的索引
Bucket *org_bucket = ht->buckets[index];
Bucket *bucket = (Bucket *)malloc(sizeof(Bucket)); // 為新元素申請空間
bucket->key = strdup(key);
// 將值內容保存進來, 這里只是簡單的將指針指向要存儲的內容,而沒有將內容復制。
bucket->value = value;
LOG_MSG("Insert data p: %p\n", value);
ht->elem_num += 1; // 記錄一下現在哈希表中的元素個數
if(org_bucket != NULL) { // 發生了碰撞,將新元素放置在鏈表的頭部
LOG_MSG("Index collision found with org hashtable: %p\n", org_bucket);
bucket->next = org_bucket;
}
ht->buckets[index]= bucket;
LOG_MSG("Element inserted at index %i, now we have: %i elements\n",
index, ht->elem_num);
return SUCCESS;
}
總結
以上是生活随笔為你收集整理的php hash代码下载,PHP中的哈希表 hash_insert的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 只闻其声不见其人是什么意思
- 下一篇: php中双引号的区别,PHP中单引号和双