From eed1b5d2fca21abae2540b500f4a4ed94a809403 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 4 Feb 2021 15:34:57 +0100
Subject: [PATCH] Various small fixes

---
 src/BufferTop.c     | 14 ++++++++------
 src/BufferTop.h     |  6 ++----
 src/HashTable.c     | 11 ++++++-----
 src/HashTable.h     | 12 ++++--------
 src/Heap.c          |  9 +++------
 src/Heap.h          | 10 ++++------
 src/List.h          | 14 ++++++--------
 src/PriorityQueue.c |  2 +-
 src/PriorityQueue.h |  4 +---
 src/Queue.h         |  6 ++----
 src/Stack.h         |  8 +++-----
 src/Tree.h          | 16 +++++++---------
 src/Vector.h        | 16 +++++++---------
 test/t.HashTable.c  | 41 ++++++++++++++++++++---------------------
 14 files changed, 74 insertions(+), 95 deletions(-)

diff --git a/src/BufferTop.c b/src/BufferTop.c
index 4f1ac49..46a793e 100644
--- a/src/BufferTop.c
+++ b/src/BufferTop.c
@@ -4,15 +4,17 @@
 
 #include "cgds/BufferTop.h"
 
-// NOTE: no _init() method here, since BufferTop has no specific initialization
+// NOTE: no init() method here, since BufferTop has no specific initialization
 
-BufferTop* _buffertop_new(size_t dataSize, UInt capacity, OrderType bType, UInt arity)
+BufferTop* _buffertop_new(
+  size_t dataSize, UInt capacity, OrderType bType, UInt arity)
 {
   BufferTop* bufferTop = (BufferTop*) safe_malloc(sizeof (BufferTop));
   bufferTop->capacity = capacity;
   bufferTop->bType = bType; //redondant, but facilitate understanding
   // WARNING: heap must have opposite type: "smallest" element first
-  bufferTop->heap = _heap_new(dataSize, (bType == MAX_T ? MIN_T : MAX_T), arity);
+  bufferTop->heap =
+    _heap_new(dataSize, (bType == MAX_T ? MIN_T : MAX_T), arity);
   return bufferTop;
 }
 
@@ -62,15 +64,15 @@ void _buffertop_tryadd(BufferTop* bufferTop, void* item, Real value)
     (bufferTop->bType == MAX_T &&
     value <= ((ItemValue*) (bufferTop->heap->array->datas[0]))->value)))
   {
-    // shortcut : if value "worse" than top->value and buffer is full, skip
+    // Shortcut : if value "worse" than top->value and buffer is full, skip
     return;
   }
 
-  // insertion somewhere in the item-values heap
+  // Insertion somewhere in the item-values heap
   _heap_insert(bufferTop->heap, item, value);
 
   if (heap_size(bufferTop->heap) > bufferTop->capacity)
-    // we must remove current root
+    // We must remove current root
     heap_pop(bufferTop->heap);
 }
 
diff --git a/src/BufferTop.h b/src/BufferTop.h
index f399bc2..89a5e1f 100644
--- a/src/BufferTop.h
+++ b/src/BufferTop.h
@@ -41,9 +41,7 @@ BufferTop* _buffertop_new(
  * Usage: BufferTop* buffertop_new(<Type> type, UInt capacity, OrderTypebType, UInt arity)
  */
 #define buffertop_new(type, capacity, bType, arity) \
-{ \
-  _buffertop_new(sizeof(type), capacity, bType, arity); \
-}
+  _buffertop_new(sizeof(type), capacity, bType, arity)
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -92,7 +90,7 @@ void _buffertop_tryadd(
  */
 #define buffertop_tryadd(bufferTop, item, value) \
 { \
-  typeof((item)) tmp = item; \
+  typeof(item) tmp = item; \
   _buffertop_tryadd(bufferTop, &tmp, value); \
 }
 
diff --git a/src/HashTable.c b/src/HashTable.c
index 9e818b0..d54934b 100644
--- a/src/HashTable.c
+++ b/src/HashTable.c
@@ -16,7 +16,7 @@ void _hashtable_init(HashTable* hashTable, size_t dataSize, size_t hashSize)
 
 HashTable* _hashtable_new(size_t dataSize, size_t hashSize)
 {
-  HashTable* hashTable = (HashTable*) safe_malloc(sizeof (HashTable));
+  HashTable* hashTable = (HashTable*) safe_malloc(sizeof(HashTable));
   _hashtable_init(hashTable, dataSize, hashSize);
   return hashTable;
 }
@@ -34,7 +34,7 @@ HashTable* hashtable_copy(HashTable* hashTable)
     while (cell != NULL)
     {
       // cellCopy == NULL (from empty list)
-      cellCopy = (HashCell*) safe_malloc(sizeof(HashCell*));
+      cellCopy = (HashCell*) safe_malloc(sizeof(HashCell));
       cellCopy->key = (char*) safe_malloc(strlen(cell->key) + 1);
       strcpy(cellCopy->key, cell->key);
       cellCopy->data = safe_malloc(hashTable->dataSize);
@@ -65,8 +65,8 @@ UInt _compute_hash(char* key, size_t hashSize)
   UInt res = 0;
   for (unsigned char* s = key; *s != '\0'; s++)
     // NOTE: '31' from here https://stackoverflow.com/a/4384446
-    res += *s + 31 * res;
-  return res % hashSize;
+    res = (*s + 31 * res) % hashSize;
+  return res;
 }
 
 void* _hashtable_get(HashTable* hashTable, char* key)
@@ -75,7 +75,8 @@ void* _hashtable_get(HashTable* hashTable, char* key)
   HashCell* cell = hashTable->head[hashIdx];
   while (cell != NULL)
   {
-    if (strcmp(cell->key, key) == 0) return cell->data;
+    if (strcmp(cell->key, key) == 0)
+      return cell->data;
     cell = cell->next;
   }
   return NULL;
diff --git a/src/HashTable.h b/src/HashTable.h
index 2d5659f..686e2bb 100644
--- a/src/HashTable.h
+++ b/src/HashTable.h
@@ -9,7 +9,6 @@
 #include <string.h>
 #include "cgds/safe_alloc.h"
 #include "cgds/types.h"
-#include "cgds/Vector.h"
 
 /**
  * @brief Cell of a dictionary.
@@ -54,9 +53,7 @@ HashTable* _hashtable_new(
  * Usage: HashTable* hashtable_new(<Type> type, UInt hash_size)
  */
 #define hashtable_new(type, hsize) \
-{ \
-  _hashtable_new(sizeof(type), hsize); \
-}
+  _hashtable_new(sizeof(type), hsize)
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -91,14 +88,13 @@ void* _hashtable_get(
  * @brief Lookup element of given key.
  * @param hashTable "this" pointer.
  * @param key Key of the element to retrieve..
- * @param data 'out' variable to contain the result.
+ * @param data 'out' variable (ptr) to contain the result.
  *
  * Usage: void hashtable_get(HashTable* hashTable, char* key, void data)
  */
 #define hashtable_get(hashTable, key, data) \
 { \
-  void* pData = _hashtable_get(hashTable, key); \
-  data = *((typeof(&data))pData); \
+  data = (typeof(data))_hashtable_get(hashTable, key); \
 }
 
 /**
@@ -120,7 +116,7 @@ void _hashtable_set(
  */
 #define hashtable_set(hashTable, key, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _hashtable_set(hashTable, key, &tmp); \
 }
 
diff --git a/src/Heap.c b/src/Heap.c
index e1cb8ce..6bdbc0e 100644
--- a/src/Heap.c
+++ b/src/Heap.c
@@ -4,7 +4,7 @@
 
 #include "cgds/Heap.h"
 
-// NOTE: no _init() method here, since Heap has no specific initialization
+// NOTE: no init() method here, since Heap has no specific initialization
 
 Heap* _heap_new(size_t dataSize, OrderType hType, UInt arity)
 {
@@ -25,7 +25,7 @@ Heap* heap_copy(Heap* heap)
   heapCopy->array->capacity = heap->array->capacity;
   heapCopy->array->datas =
     (void**)safe_malloc(heap->array->capacity*sizeof(void*));
-  for (UInt i=0; i<heap->array->size; i++)
+  for (UInt i = 0; i < heap->array->size; i++)
   {
     heapCopy->array->datas[i] = safe_malloc(sizeof(ItemValue));
     ItemValue itemValueCopy = (ItemValue){
@@ -94,7 +94,7 @@ void _heap_bubble_down(Heap* heap, UInt startIndex)
     // find top child (min or max)
     UInt topChildIndex;
     Real topChildValue = (heap->hType == MIN_T ? INFINITY : -INFINITY);
-    for (Int i=0; i<heap->arity; i++)
+    for (UInt i = 0; i < heap->arity; i++)
     {
       UInt childIndex = i + currentIndex * heap->arity;
       if (childIndex >= heap->array->size)
@@ -170,11 +170,8 @@ void heap_pop(Heap* heap)
 void heap_clear(Heap* heap)
 {
   for (UInt i = 0; i < heap->array->size; i++)
-  {
     // Extra memory releases which wouldn't be done in vector_clear()
     safe_free(((ItemValue*)(heap->array->datas[i]))->item);
-    //safe_free((ItemValue*)heap->array->datas[i]);
-  }
   vector_clear(heap->array);
 }
 
diff --git a/src/Heap.h b/src/Heap.h
index e360829..3452aa0 100644
--- a/src/Heap.h
+++ b/src/Heap.h
@@ -40,9 +40,7 @@ Heap* _heap_new(
  * Usage: Heap* heap_new(<Type> type, OrderType hType, UInt arity)
  */
 #define heap_new(type, hType, arity) \
-{ \
-  _heap_new(sizeof(type), hType, arity); \
-}
+  _heap_new(sizeof(type), hType, arity)
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -102,7 +100,7 @@ void _heap_insert(
  */
 #define heap_insert(heap, item, value) \
 { \
-  typeof((item)) tmp = item; \
+  typeof(item) tmp = item; \
   _heap_insert(heap, &tmp, value); \
 }
 
@@ -129,7 +127,7 @@ void _heap_modify(
 #define heap_modify(heap, item_, newValue) \
 { \
   UInt index = 0; \
-  typeof((item_)) item__ = item_; \
+  typeof(item_) item__ = item_; \
   for (; index<heap->array->size; index++) \
   { \
     void* pItem = ((ItemValue*)(heap->array->datas[index]))->item; \
@@ -159,7 +157,7 @@ void _heap_remove(
 #define heap_remove(heap, item_) \
 { \
   UInt index = 0; \
-  typeof((item_)) item__ = item_; \
+  typeof(item_) item__ = item_; \
   for (; index<heap->array->size; index++) \
   { \
     void* pItem = ((ItemValue*)(heap->array->datas[index]))->item; \
diff --git a/src/List.h b/src/List.h
index 6150d2b..a6e9822 100644
--- a/src/List.h
+++ b/src/List.h
@@ -56,9 +56,7 @@ List* _list_new(
  * Usage: List* list_new(<Type> type)
  */
 #define list_new(type) \
-{ \
-  _list_new(sizeof(type)); \
-}
+  _list_new(sizeof(type))
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -120,7 +118,7 @@ void _list_set(
  */
 #define list_set(list, listCell, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _list_set(list, listCell, &tmp); \
 }
 
@@ -151,7 +149,7 @@ void _list_insert_before(
  */
 #define list_insert_before(list, listCell, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _list_insert_before(list, listCell, &tmp); \
 }
 
@@ -174,7 +172,7 @@ void _list_insert_after(
  */
 #define list_insert_after(list, listCell, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _list_insert_after(list, listCell, &tmp); \
 }
 
@@ -195,7 +193,7 @@ void _list_insert_front(
  */
 #define list_insert_front(list, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _list_insert_front(list, &tmp); \
 }
 
@@ -216,7 +214,7 @@ void _list_insert_back(
  */
 #define list_insert_back(list, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _list_insert_back(list, &tmp); \
 }
 
diff --git a/src/PriorityQueue.c b/src/PriorityQueue.c
index f9bdab7..ef7079b 100644
--- a/src/PriorityQueue.c
+++ b/src/PriorityQueue.c
@@ -4,7 +4,7 @@
 
 #include "cgds/PriorityQueue.h"
 
-// NOTE: no _init() method here,
+// NOTE: no init() method here,
 // since PriorityQueue has no specific initialization
 
 PriorityQueue* _priorityqueue_new(size_t dataSize, OrderType pType, UInt arity)
diff --git a/src/PriorityQueue.h b/src/PriorityQueue.h
index 01b7789..4ddabfd 100644
--- a/src/PriorityQueue.h
+++ b/src/PriorityQueue.h
@@ -36,9 +36,7 @@ PriorityQueue* _priorityqueue_new(
  * Usage: PriorityQueue* priorityqueue_new(<Type> type, OrderType pType, UInt arity)
  */
 #define priorityqueue_new(type, pType, arity) \
-{ \
-  _priorityqueue_new(sizeof(type), pType, arity); \
-}
+  _priorityqueue_new(sizeof(type), pType, arity)
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
diff --git a/src/Queue.h b/src/Queue.h
index 09b61cf..22d76dc 100644
--- a/src/Queue.h
+++ b/src/Queue.h
@@ -44,9 +44,7 @@ Queue* _queue_new(
  * Usage: Queue* queue_new(<Type> type)
  */
 #define queue_new(type) \
-{ \
-  _queue_new(sizeof(type)); \
-}
+  _queue_new(sizeof(type))
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -86,7 +84,7 @@ void _queue_push(
  */
 #define queue_push(queue, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _queue_push(queue, &tmp); \
 }
 
diff --git a/src/Stack.h b/src/Stack.h
index 57bb615..b6a3bf4 100644
--- a/src/Stack.h
+++ b/src/Stack.h
@@ -41,9 +41,7 @@ Stack* _stack_new(
  * Usage: Stack* stack_new(<Type> type)
  */
 #define stack_new(type) \
-{ \
-  _stack_new(sizeof(type)); \
-}
+  _stack_new(sizeof(type))
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -83,8 +81,8 @@ void _stack_push(
  */
 #define stack_push(stack, data) \
 { \
-  typeof((data)) tmp = data; \
-  _stack_push(stack,&tmp); \
+  typeof(data) tmp = data; \
+  _stack_push(stack, &tmp); \
 }
 
 /**
diff --git a/src/Tree.h b/src/Tree.h
index 6ac0864..a997525 100644
--- a/src/Tree.h
+++ b/src/Tree.h
@@ -57,9 +57,7 @@ Tree* _tree_new(
  * Usage: Tree* tree_new(<Type> type)
  */
 #define tree_new(type) \
-{ \
-  _tree_new(sizeof(type)); \
-}
+  _tree_new(sizeof(type))
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -120,7 +118,7 @@ void _tree_set_root(
  */
 #define tree_set_root(tree, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _tree_set_root(tree, &tmp); \
 }
 
@@ -163,8 +161,8 @@ void _tree_set(
  */
 #define tree_set(tree, treeNode, data) \
 { \
-  typeof((data)) tmp = data; \
-  _tree_set(tree,treeNode,&tmp); \
+  typeof(data) tmp = data; \
+  _tree_set(tree, treeNode, &tmp); \
 }
 
 /**
@@ -186,8 +184,8 @@ void _tree_add_child(
  */
 #define tree_add_child(tree,treeNode,data) \
 { \
-  typeof((data)) tmp = data; \
-  _tree_add_child(tree,treeNode,&tmp); \
+  typeof(data) tmp = data; \
+  _tree_add_child(tree, treeNode, &tmp); \
 }
 
 /**
@@ -209,7 +207,7 @@ void _tree_add_sibling(
  */
 #define tree_add_sibling(tree, treeNode, data) \
 { \
-  typeof((data)) tmp = data; \
+  typeof(data) tmp = data; \
   _tree_add_sibling(tree, treeNode, &tmp); \
 }
 
diff --git a/src/Vector.h b/src/Vector.h
index 0f3ef58..b6584ac 100644
--- a/src/Vector.h
+++ b/src/Vector.h
@@ -46,9 +46,7 @@ Vector* _vector_new(
  * Usage: Vector* vector_new(<Type> type)
  */
 #define vector_new(type) \
-{ \
-  _vector_new(sizeof(type)); \
-}
+  _vector_new(sizeof(type))
 
 /**
  * @brief Copy constructor (shallow copy, ok for basic types).
@@ -96,8 +94,8 @@ void _vector_push(
  */
 #define vector_push(vector, data) \
 { \
-  typeof((data)) tmp = data; \
-  _vector_push(vector,&tmp); \
+  typeof(data) tmp = data; \
+  _vector_push(vector, &tmp); \
 }
 
 /**
@@ -148,8 +146,8 @@ void _vector_set(
  */
 #define vector_set(vector, index, data) \
 { \
-  typeof((data)) tmp = data; \
-  _vector_set(vector,index,&tmp); \
+  typeof(data) tmp = data; \
+  _vector_set(vector, index, &tmp); \
 }
 
 /**
@@ -243,8 +241,8 @@ void _vectorI_set(
  */
 #define vectorI_set(vectorI, data) \
 { \
-  typeof((data)) tmp = data; \
-  _vectorI_set(vectorI,&tmp); \
+  typeof(data) tmp = data; \
+  _vectorI_set(vectorI, &tmp); \
 }
 
 /**
diff --git a/test/t.HashTable.c b/test/t.HashTable.c
index 5c23cb7..a77b2ca 100644
--- a/test/t.HashTable.c
+++ b/test/t.HashTable.c
@@ -64,17 +64,16 @@ void t_hashtable_set_remove_basic()
   double ckValue = 0.0;
   for (int i = 0; i < n; i++)
   {
-    double d;
+    double* d;
     key[3] = (char)(48 + i);
     hashtable_get(h, key, d);
-    lu_assert_dbl_eq(d, ckValue);
+    lu_assert_dbl_eq(*d, ckValue);
     ckValue += 1.0;
   }
 
   //Remove keys / values
   for (int i = 0; i < n; i++)
   {
-    double d;
     key[3] = (char)(48 + i);
     hashtable_delete(h, key);
   }
@@ -88,8 +87,8 @@ void t_hashtable_getnull_modify()
   int n = 10;
 
   HashTable* h = hashtable_new(StructTest1, 4);
-  StructTest1* st1 = (StructTest1*) malloc(n * sizeof (StructTest1));
-  char* key = "key_";
+  StructTest1* st1 = (StructTest1*) malloc(n * sizeof(StructTest1));
+  char key[] = "key_";
   for (int i = 0; i < n; i++)
   {
     key[3] = (char)(48 + i);
@@ -101,11 +100,12 @@ void t_hashtable_getnull_modify()
   {
     //another way to access elements
     key[3] = (char)(48 + i);
-    StructTest1 st1Cell;
+    StructTest1* st1Cell;
     hashtable_get(h, key, st1Cell);
-    lu_assert_int_eq(st1Cell.a, st1[i].a);
-    lu_assert_dbl_eq(st1Cell.b, st1[i].b);
+    lu_assert_int_eq(st1Cell->a, st1[i].a);
+    lu_assert_dbl_eq(st1Cell->b, st1[i].b);
   }
+  safe_free(st1);
 
   // Get null:
   StructTest1* stmp;
@@ -114,21 +114,20 @@ void t_hashtable_getnull_modify()
   hashtable_get(h, "key32", stmp);
   lu_assert(stmp == NULL);
   // Modify:
-  StructTest1* stMod;
-  stMod->a = -17;
-  stMod->b = 3.0;
+  StructTest1 stMod;
+  stMod.a = -17;
+  stMod.b = 3.0;
   hashtable_set(h, "key1", stMod);
   hashtable_get(h, "key1", stmp);
-  lu_assert_int_eq(stmp->a, stMod->a);
-  lu_assert_dbl_eq(stmp->b, stMod->b);
-  stMod->a = -7;
-  stMod->b = 3.5;
+  lu_assert_int_eq(stmp->a, stMod.a);
+  lu_assert_dbl_eq(stmp->b, stMod.b);
+  stMod.a = -7;
+  stMod.b = 3.5;
   hashtable_set(h, "key5", stMod);
   hashtable_get(h, "key5", stmp);
-  lu_assert_int_eq(stmp->a, stMod->a);
-  lu_assert_dbl_eq(stmp->b, stMod->b);
+  lu_assert_int_eq(stmp->a, stMod.a);
+  lu_assert_dbl_eq(stmp->b, stMod.b);
 
-  safe_free(st1);
   hashtable_destroy(h);
 }
 
@@ -137,7 +136,7 @@ void t_hashtable_copy()
   int n = 10;
 
   HashTable* h = hashtable_new(int, 8);
-  char* key = "key_";
+  char key[] = "key_";
   for (int i = 0; i < n; i++)
   {
     key[3] = (char)(48 + i);
@@ -146,13 +145,13 @@ void t_hashtable_copy()
   HashTable* hc = hashtable_copy(h);
 
   lu_assert_int_eq(h->size, hc->size);
-  int a, b;
+  int *a, *b;
   for (int i = 0; i < n; i++)
   {
     key[3] = (char)(48 + i);
     hashtable_get(h, key, a);
     hashtable_get(hc, key, b);
-    lu_assert_int_eq(a, b);
+    lu_assert_int_eq(*a, *b);
   }
   hashtable_destroy(h);
   hashtable_destroy(hc);
-- 
2.44.0