فهرست منبع

added functions for traversing hash table

Vaclav Kubart 19 سال پیش
والد
کامیت
c8530cf8c4
3فایلهای تغییر یافته به همراه49 افزوده شده و 0 حذف شده
  1. 1 0
      lib/cds/ChangeLog
  2. 38 0
      lib/cds/hash_table.c
  3. 10 0
      lib/cds/hash_table.h

+ 1 - 0
lib/cds/ChangeLog

@@ -1,5 +1,6 @@
 2006-06-13
 	* added few common SIP/SER functions into sip_utils.h
+	* added functions for traversing hash table
 
 2006-05-11
 	* added changelog

+ 38 - 0
lib/cds/hash_table.c

@@ -173,6 +173,44 @@ void ht_clear_statistic(hash_table_t *ht)
 	ht->missed_cnt = 0;
 }
 
+/* --------- hash table traversing functions -------- */
+
+ht_element_t *get_first_ht_element(hash_table_t *ht, ht_traversal_info_t *info)
+{
+	int i;
+	if (!info) return NULL;
+	info->ht = ht;
+	info->current = NULL;
+	for (i = 0; i < ht->size; i++) {
+		if (ht->cslots[i].first) {
+			info->current = ht->cslots[i].first;
+			break;
+		}
+	}
+	info->slot_pos = i;
+	return info->current;
+}
+
+ht_element_t *get_next_ht_element(ht_traversal_info_t *info)
+{
+	int i;
+	if (!info) return NULL;
+
+	if (info->current) info->current = info->current->next;
+	
+	if (info->current) return info->current;
+	else {
+		for (i = info->slot_pos + 1; i < info->ht->size; i++) {
+			if (info->ht->cslots[i].first) {
+				info->current = info->ht->cslots[i].first;
+				break;
+			}
+		}
+		info->slot_pos = i;
+	}
+	return info->current;
+}
+
 /* --------- HASH functions -------- */
 
 unsigned int rshash(const char* str, unsigned int len)

+ 10 - 0
lib/cds/hash_table.h

@@ -74,6 +74,16 @@ ht_data_t ht_find(hash_table_t *ht, ht_key_t key);
 void ht_get_statistic(hash_table_t *ht, ht_statistic_t *s);
 void ht_clear_statistic(hash_table_t *ht);
 
+/* traversing through whole hash table */
+typedef struct {
+	hash_table_t *ht;
+	int slot_pos;
+	ht_element_t *current;
+} ht_traversal_info_t;
+
+ht_element_t *get_first_ht_element(hash_table_t *ht, ht_traversal_info_t *info);
+ht_element_t *get_next_ht_element(ht_traversal_info_t *info);
+
 /* hash functions */
 unsigned int rshash(const char* str, unsigned int len);