]> andersk Git - moira.git/blobdiff - lib/hash.c
Code style cleanup. (No functional changes)
[moira.git] / lib / hash.c
index 665925d4077759ed2b0db156b14c7714b8c6724f..214eae4caad54b1348bad1828f0ca1912cf0d424 100644 (file)
 
 /* Create a hash table.  The size is just a hint, not a maximum. */
 
-struct hash *create_hash(size)
-int size;
+struct hash *create_hash(int size)
 {
-    struct hash *h;
-
-    h = (struct hash *) malloc(sizeof(struct hash));
-    if (h == (struct hash *) NULL)
-      return((struct hash *) NULL);
-    h->size = size;
-    h->data = (struct bucket **) malloc(size * sizeof(char *));
-    if (h->data == (struct bucket **) NULL) {
-       free(h);
-       return((struct hash *) NULL);
+  struct hash *h;
+
+  h = malloc(sizeof(struct hash));
+  if (!h)
+    return NULL;
+  h->size = size;
+  h->data = malloc(size * sizeof(char *));
+  if (!h->data)
+    {
+      free(h);
+      return NULL;
     }
-    memset(h->data, 0, size * sizeof(char *));
-    return(h);
+  memset(h->data, 0, size * sizeof(char *));
+  return h;
 }
 
 /* Lookup an object in the hash table.  Returns the value associated with
  * the key, or NULL (thus NULL is not a very good value to store...)
  */
 
-char *hash_lookup(h, key)
-struct hash *h;
-register int key;
+char *hash_lookup(struct hash *h, register int key)
 {
-    register struct bucket *b;
-
-    b = h->data[hash_func(h, key)];
-    while (b && b->key != key)
-      b = b->next;
-    if (b && b->key == key)
-      return(b->data);
-    else
-      return(NULL);
+  register struct bucket *b;
+
+  b = h->data[hash_func(h, key)];
+  while (b && b->key != key)
+    b = b->next;
+  if (b && b->key == key)
+    return b->data;
+  else
+    return NULL;
 }
 
 
@@ -62,21 +60,20 @@ register int key;
  * existed, or 0 if not.
  */
 
-int hash_update(h, key, value)
-struct hash *h;
-register int key;
-char *value;
+int hash_update(struct hash *h, register int key, char *value)
 {
-    register struct bucket *b;
-
-    b = h->data[hash_func(h, key)];
-    while (b && b->key != key)
-      b = b->next;
-    if (b && b->key == key) {
-       b->data = value;
-       return(1);
-    } else
-      return(0);
+  register struct bucket *b;
+
+  b = h->data[hash_func(h, key)];
+  while (b && b->key != key)
+    b = b->next;
+  if (b && b->key == key)
+    {
+      b->data = value;
+      return 1;
+    }
+  else
+    return 0;
 }
 
 
@@ -84,37 +81,36 @@ char *value;
  * there, 1 if it was, or -1 if we ran out of memory.
  */
 
-int hash_store(h, key, value)
-struct hash *h;
-register int key;
-char *value;
+int hash_store(struct hash *h, register int key, char *value)
 {
-    register struct bucket *b, **p;
-
-    p = &(h->data[hash_func(h, key)]);
-    if (*p == NULL) {
-       b = *p = (struct bucket *) malloc(sizeof(struct bucket));
-       if (b == (struct bucket *) NULL)
-         return(-1);
-       b->next = NULL;
-       b->key = key;
-       b->data = value;
-       return(0);
+  register struct bucket *b, **p;
+
+  p = &(h->data[hash_func(h, key)]);
+  if (!*p)
+    {
+      b = *p = malloc(sizeof(struct bucket));
+      if (!b)
+       return -1;
+      b->next = NULL;
+      b->key = key;
+      b->data = value;
+      return 0;
     }
 
-    for (b = *p; b && b->key != key; b = *p)
-      p = (struct bucket **) *p;
-    if (b && b->key == key) {
-       b->data = value;
-       return(1);
+  for (b = *p; b && b->key != key; b = *p)
+    p = (struct bucket **) *p;
+  if (b && b->key == key)
+    {
+      b->data = value;
+      return 1;
     }
-    b = *p = (struct bucket *) malloc(sizeof(struct bucket));
-    if (b == (struct bucket *) NULL)
-      return(-1);
-    b->next = NULL;
-    b->key = key;
-    b->data = value;
-    return(0);
+  b = *p = malloc(sizeof(struct bucket));
+  if (!b)
+    return -1;
+  b->next = NULL;
+  b->key = key;
+  b->data = value;
+  return 0;
 }
 
 
@@ -122,17 +118,16 @@ char *value;
  * data with that value, call the callback proc with the corresponding key.
  */
 
-hash_search(h, value, callback)
-struct hash *h;
-register char *value;
-void (*callback)();
+hash_search(struct hash *h, register char *value, void (*callback)())
 {
-    register struct bucket *b, **p;
-
-    for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
-       for (b = *p; b; b = b->next) {
-           if (b->data == value)
-             (*callback)(b->key);
+  register struct bucket *b, **p;
+
+  for (p = &(h->data[h->size - 1]); p >= h->data; p--)
+    {
+      for (b = *p; b; b = b->next)
+       {
+         if (b->data == value)
+           (*callback)(b->key);
        }
     }
 }
@@ -141,32 +136,30 @@ void (*callback)();
 /* Step through the hash table, calling the callback proc with each key.
  */
 
-hash_step(h, callback, hint)
-struct hash *h;
-void (*callback)();
-char *hint;
+hash_step(struct hash *h, void (*callback)(), char *hint)
 {
-    register struct bucket *b, **p;
+  register struct bucket *b, **p;
 
-    for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
-       for (b = *p; b; b = b->next) {
-           (*callback)(b->key, b->data, hint);
-       }
+  for (p = &(h->data[h->size - 1]); p >= h->data; p--)
+    {
+      for (b = *p; b; b = b->next)
+       (*callback)(b->key, b->data, hint);
     }
 }
 
 
 /* Deallocate all of the memory associated with a table */
 
-hash_destroy(h)
-struct hash *h;
+hash_destroy(struct hash *h)
 {
-    register struct bucket *b, **p, *b1;
-
-    for (p = &(h->data[h->size - 1]); p >= h->data; p--) {
-       for (b = *p; b; b = b1) {
-           b1 = b->next;
-           free(b);
+  register struct bucket *b, **p, *b1;
+
+  for (p = &(h->data[h->size - 1]); p >= h->data; p--)
+    {
+      for (b = *p; b; b = b1)
+       {
+         b1 = b->next;
+         free(b);
        }
     }
 }
This page took 0.04225 seconds and 4 git commands to generate.