#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;
}
(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);
}
* 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).
*/
#define buffertop_tryadd(bufferTop, item, value) \
{ \
- typeof((item)) tmp = item; \
+ typeof(item) tmp = item; \
_buffertop_tryadd(bufferTop, &tmp, value); \
}
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;
}
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);
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)
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;
#include <string.h>
#include "cgds/safe_alloc.h"
#include "cgds/types.h"
-#include "cgds/Vector.h"
/**
* @brief Cell of a dictionary.
* 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).
* @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); \
}
/**
*/
#define hashtable_set(hashTable, key, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_hashtable_set(hashTable, key, &tmp); \
}
#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)
{
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){
// 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)
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);
}
* 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).
*/
#define heap_insert(heap, item, value) \
{ \
- typeof((item)) tmp = item; \
+ typeof(item) tmp = item; \
_heap_insert(heap, &tmp, value); \
}
#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; \
#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; \
* 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).
*/
#define list_set(list, listCell, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_list_set(list, listCell, &tmp); \
}
*/
#define list_insert_before(list, listCell, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_list_insert_before(list, listCell, &tmp); \
}
*/
#define list_insert_after(list, listCell, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_list_insert_after(list, listCell, &tmp); \
}
*/
#define list_insert_front(list, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_list_insert_front(list, &tmp); \
}
*/
#define list_insert_back(list, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_list_insert_back(list, &tmp); \
}
#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)
* 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).
* 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).
*/
#define queue_push(queue, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_queue_push(queue, &tmp); \
}
* 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).
*/
#define stack_push(stack, data) \
{ \
- typeof((data)) tmp = data; \
- _stack_push(stack,&tmp); \
+ typeof(data) tmp = data; \
+ _stack_push(stack, &tmp); \
}
/**
* 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).
*/
#define tree_set_root(tree, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_tree_set_root(tree, &tmp); \
}
*/
#define tree_set(tree, treeNode, data) \
{ \
- typeof((data)) tmp = data; \
- _tree_set(tree,treeNode,&tmp); \
+ typeof(data) tmp = data; \
+ _tree_set(tree, treeNode, &tmp); \
}
/**
*/
#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); \
}
/**
*/
#define tree_add_sibling(tree, treeNode, data) \
{ \
- typeof((data)) tmp = data; \
+ typeof(data) tmp = data; \
_tree_add_sibling(tree, treeNode, &tmp); \
}
* 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).
*/
#define vector_push(vector, data) \
{ \
- typeof((data)) tmp = data; \
- _vector_push(vector,&tmp); \
+ typeof(data) tmp = data; \
+ _vector_push(vector, &tmp); \
}
/**
*/
#define vector_set(vector, index, data) \
{ \
- typeof((data)) tmp = data; \
- _vector_set(vector,index,&tmp); \
+ typeof(data) tmp = data; \
+ _vector_set(vector, index, &tmp); \
}
/**
*/
#define vectorI_set(vectorI, data) \
{ \
- typeof((data)) tmp = data; \
- _vectorI_set(vectorI,&tmp); \
+ typeof(data) tmp = data; \
+ _vectorI_set(vectorI, &tmp); \
}
/**
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);
}
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);
{
//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;
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);
}
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);
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);