From 36d75d561eee6dbec123126eb381067588968153 Mon Sep 17 00:00:00 2001 From: Leonardo Date: Tue, 2 Apr 2019 00:37:58 -0300 Subject: [PATCH] Fixing things everywhere Adding necessary if checks for list and linkedlist Fixing hashset new required size Unified max() and min() functions for treemap. Now you get both key and value. --- src/hashset.h | 2 +- src/linkedlist.h | 5 ++- src/list.h | 3 ++ src/treemap.h | 102 +++++++++++++++++++---------------------------- src/treeset.h | 4 +- 5 files changed, 50 insertions(+), 66 deletions(-) diff --git a/src/hashset.h b/src/hashset.h index 307d4e6..6ffa0c1 100644 --- a/src/hashset.h +++ b/src/hashset.h @@ -364,7 +364,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 193, 389, 769, 1543, 3079, const size_t count = sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]); \ \ if (cmc_hashtable_primes[count - 1] < required) \ - return required * 2; \ + return required; \ \ size_t i = 0; \ while (cmc_hashtable_primes[i] < required) \ diff --git a/src/linkedlist.h b/src/linkedlist.h index 3de748f..4e53def 100644 --- a/src/linkedlist.h +++ b/src/linkedlist.h @@ -47,10 +47,10 @@ \ struct SNAME##_node_s \ { \ + V data; \ struct SNAME##_s *owner; \ struct SNAME##_node_s *next; \ struct SNAME##_node_s *prev; \ - V data; \ }; \ \ struct SNAME##_iter_s \ @@ -234,6 +234,9 @@ if (PFX##_empty(_list_)) \ return false; \ \ + if (index >= _list_->count) \ + return false; \ + \ if (index == 0) \ { \ return PFX##_pop_front(_list_); \ diff --git a/src/list.h b/src/list.h index 2970b1c..789f349 100644 --- a/src/list.h +++ b/src/list.h @@ -216,6 +216,9 @@ if (PFX##_empty(_list_)) \ return false; \ \ + if (index >= _list_->count) \ + return false; \ + \ if (index == 0) \ { \ return PFX##_pop_front(_list_); \ diff --git a/src/treemap.h b/src/treemap.h index 8c75fe2..1719f26 100644 --- a/src/treemap.h +++ b/src/treemap.h @@ -47,12 +47,12 @@ \ struct SNAME##_node_s \ { \ + K key; \ + V value; \ + unsigned char height; \ struct SNAME##_node_s *right; \ struct SNAME##_node_s *left; \ struct SNAME##_node_s *parent; \ - unsigned char height; \ - K key; \ - V value; \ }; \ \ struct SNAME##_iter_s \ @@ -67,34 +67,32 @@ }; \ \ /* HEADER ********************************************************************/ -#define TREEMAP_GENERATE_HEADER(PFX, SNAME, FMOD, K, V) \ - \ - typedef struct SNAME##_s SNAME; \ - typedef struct SNAME##_node_s SNAME##_node; \ - typedef struct SNAME##_iter_s SNAME##_iter; \ - \ - FMOD SNAME *PFX##_new(int (*compare)(K, K)); \ - FMOD void PFX##_free(SNAME *_map_); \ - FMOD bool PFX##_insert(SNAME *_map_, K key, V value); \ - FMOD bool PFX##_remove(SNAME *_map_, K key, V *value); \ - FMOD bool PFX##_insert_if(SNAME *_map_, K key, V value, bool condition); \ - FMOD bool PFX##_remove_if(SNAME *_map_, K key, V *value, bool condition); \ - FMOD K PFX##_max_key(SNAME *_map_); \ - FMOD V PFX##_max_val(SNAME *_map_); \ - FMOD K PFX##_min_key(SNAME *_map_); \ - FMOD V PFX##_min_val(SNAME *_map_); \ - FMOD V PFX##_get(SNAME *_map_, K key); \ - FMOD bool PFX##_empty(SNAME *_map_); \ - FMOD size_t PFX##_count(SNAME *_map_); \ - \ - FMOD void PFX##_iter_new(SNAME##_iter *iter, SNAME *target); \ - FMOD bool PFX##_iter_start(SNAME##_iter *iter); \ - FMOD bool PFX##_iter_end(SNAME##_iter *iter); \ - FMOD void PFX##_iter_tostart(SNAME##_iter *iter); \ - FMOD void PFX##_iter_toend(SNAME##_iter *iter); \ - FMOD bool PFX##_iter_next(SNAME##_iter *iter, K *key, V *result, size_t *index); \ - FMOD bool PFX##_iter_prev(SNAME##_iter *iter, K *key, V *result, size_t *index); \ - \ +#define TREEMAP_GENERATE_HEADER(PFX, SNAME, FMOD, K, V) \ + \ + typedef struct SNAME##_s SNAME; \ + typedef struct SNAME##_node_s SNAME##_node; \ + typedef struct SNAME##_iter_s SNAME##_iter; \ + \ + FMOD SNAME *PFX##_new(int (*compare)(K, K)); \ + FMOD void PFX##_free(SNAME *_map_); \ + FMOD bool PFX##_insert(SNAME *_map_, K key, V value); \ + FMOD bool PFX##_remove(SNAME *_map_, K key, V *value); \ + FMOD bool PFX##_insert_if(SNAME *_map_, K key, V value, bool condition); \ + FMOD bool PFX##_remove_if(SNAME *_map_, K key, V *value, bool condition); \ + FMOD bool PFX##_max(SNAME *_map_, K *key, V *value); \ + FMOD bool PFX##_min(SNAME *_map_, K *key, V *value); \ + FMOD V PFX##_get(SNAME *_map_, K key); \ + FMOD bool PFX##_empty(SNAME *_map_); \ + FMOD size_t PFX##_count(SNAME *_map_); \ + \ + FMOD void PFX##_iter_new(SNAME##_iter *iter, SNAME *target); \ + FMOD bool PFX##_iter_start(SNAME##_iter *iter); \ + FMOD bool PFX##_iter_end(SNAME##_iter *iter); \ + FMOD void PFX##_iter_tostart(SNAME##_iter *iter); \ + FMOD void PFX##_iter_toend(SNAME##_iter *iter); \ + FMOD bool PFX##_iter_next(SNAME##_iter *iter, K *key, V *value, size_t *index); \ + FMOD bool PFX##_iter_prev(SNAME##_iter *iter, K *key, V *value, size_t *index); \ + \ /* SOURCE ********************************************************************/ #define TREEMAP_GENERATE_SOURCE(PFX, SNAME, FMOD, K, V) \ \ @@ -368,56 +366,36 @@ return false; \ } \ \ - FMOD K PFX##_max_key(SNAME *_map_) \ + FMOD bool PFX##_max(SNAME *_map_, K *key, V *value) \ { \ if (PFX##_empty(_map_)) \ - return 0; \ + return false; \ \ SNAME##_node *scan = _map_->root; \ \ while (scan->right != NULL) \ scan = scan->right; \ \ - return scan->key; \ + *key = scan->key; \ + *value = scan->value; \ + \ + return true; \ } \ \ - FMOD V PFX##_max_val(SNAME *_map_) \ + FMOD bool PFX##_min(SNAME *_map_, K *key, V *value) \ { \ if (PFX##_empty(_map_)) \ - return 0; \ - \ - SNAME##_node *scan = _map_->root; \ - \ - while (scan->right != NULL) \ - scan = scan->right; \ - \ - return scan->value; \ - } \ - \ - FMOD K PFX##_min_key(SNAME *_map_) \ - { \ - if (PFX##_empty(_map_)) \ - return 0; \ + return false; \ \ SNAME##_node *scan = _map_->root; \ \ while (scan->left != NULL) \ scan = scan->left; \ \ - return scan->key; \ - } \ + *key = scan->key; \ + *value = scan->value; \ \ - FMOD V PFX##_min_val(SNAME *_map_) \ - { \ - if (PFX##_empty(_map_)) \ - return 0; \ - \ - SNAME##_node *scan = _map_->root; \ - \ - while (scan->left != NULL) \ - scan = scan->left; \ - \ - return scan->value; \ + return true; \ } \ \ FMOD V PFX##_get(SNAME *_map_, K key) \ diff --git a/src/treeset.h b/src/treeset.h index f1e5cdb..be5925a 100644 --- a/src/treeset.h +++ b/src/treeset.h @@ -47,11 +47,11 @@ \ struct SNAME##_node_s \ { \ + V key; \ + unsigned char height; \ struct SNAME##_node_s *right; \ struct SNAME##_node_s *left; \ struct SNAME##_node_s *parent; \ - unsigned char height; \ - V key; \ }; \ \ struct SNAME##_iter_s \