You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by bp...@apache.org on 2015/10/03 11:40:29 UTC
celix git commit: CELIX-262: Fix minor issues in hashMap/linkedList
Repository: celix
Updated Branches:
refs/heads/develop a29205ab4 -> 5498452e2
CELIX-262: Fix minor issues in hashMap/linkedList
Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/5498452e
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/5498452e
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/5498452e
Branch: refs/heads/develop
Commit: 5498452e2786f2b034058819c6e83f758d75bedf
Parents: a29205a
Author: Bjoern Petri <bp...@apache.org>
Authored: Sat Oct 3 11:39:50 2015 +0200
Committer: Bjoern Petri <bp...@apache.org>
Committed: Sat Oct 3 11:39:50 2015 +0200
----------------------------------------------------------------------
utils/private/src/hash_map.c | 57 ++++++++++++++++++++++-----------
utils/private/src/linked_list.c | 2 +-
utils/public/include/hash_map.h | 4 ++-
utils/public/include/linked_list.h | 2 +-
4 files changed, 43 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/celix/blob/5498452e/utils/private/src/hash_map.c
----------------------------------------------------------------------
diff --git a/utils/private/src/hash_map.c b/utils/private/src/hash_map.c
index dc082a4..177cf0d 100644
--- a/utils/private/src/hash_map.c
+++ b/utils/private/src/hash_map.c
@@ -122,7 +122,7 @@ void * hashMap_get(hash_map_pt map, void * key) {
}
hash = hashMap_hash(map->hashKey(key));
- hash_map_entry_pt entry = NULL;
+ hash_map_entry_pt entry = NULL;
for (entry = map->table[hashMap_indexFor(hash, map->tablelength)]; entry != NULL; entry = entry->next) {
if (entry->hash == hash && (entry->key == key || map->equalsKey(key, entry->key))) {
return entry->value;
@@ -166,7 +166,7 @@ void * hashMap_put(hash_map_pt map, void * key, void * value) {
hash = hashMap_hash(map->hashKey(key));
i = hashMap_indexFor(hash, map->tablelength);
- hash_map_entry_pt entry;
+ hash_map_entry_pt entry;
for (entry = map->table[i]; entry != NULL; entry = entry->next) {
if (entry->hash == hash && (entry->key == key || map->equalsKey(key, entry->key))) {
void * oldValue = entry->value;
@@ -214,7 +214,7 @@ void * hashMap_remove(hash_map_pt map, void * key) {
entry->key = NULL;
entry->value = NULL;
free(entry);
- }
+ }
return value;
}
@@ -247,12 +247,12 @@ hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry
unsigned int hash;
hash_map_entry_pt prev;
hash_map_entry_pt e;
- int i;
+ int i;
if (entry == NULL) {
return NULL;
}
hash = (entry->key == NULL) ? 0 : hashMap_hash(map->hashKey(entry->key));
- i = hashMap_indexFor(hash, map->tablelength);
+ i = hashMap_indexFor(hash, map->tablelength);
prev = map->table[i];
e = prev;
@@ -267,7 +267,7 @@ hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry
prev->next = next;
}
return e;
- }
+ }
prev = e;
e = next;
}
@@ -435,6 +435,11 @@ hash_map_key_set_pt hashMapKeySet_create(hash_map_pt map) {
return keySet;
}
+void hashMapKeySet_destroy(hash_map_key_set_pt keySet){
+ keySet->map = NULL;
+ free(keySet);
+}
+
int hashMapKeySet_size(hash_map_key_set_pt keySet) {
return keySet->map->size;
}
@@ -485,17 +490,17 @@ bool hashMapValues_contains(hash_map_values_pt values, void * value) {
void hashMapValues_toArray(hash_map_values_pt values, void* *array[], unsigned int *size) {
hash_map_iterator_pt it;
int i;
- int vsize = hashMapValues_size(values);
- *size = vsize;
- *array = malloc(vsize * sizeof(*array));
- it = hashMapValues_iterator(values);
- for (i = 0; i < vsize; i++) {
- if (!hashMapIterator_hasNext(it)) {
- return;
- }
- (*array)[i] = hashMapIterator_nextValue(it);
- }
- hashMapIterator_destroy(it);
+ int vsize = hashMapValues_size(values);
+ *size = vsize;
+ *array = malloc(vsize * sizeof(*array));
+ it = hashMapValues_iterator(values);
+ for (i = 0; i < vsize; i++) {
+ if (!hashMapIterator_hasNext(it)) {
+ return;
+ }
+ (*array)[i] = hashMapIterator_nextValue(it);
+ }
+ hashMapIterator_destroy(it);
}
bool hashMapValues_remove(hash_map_values_pt values, void * value) {
@@ -504,6 +509,7 @@ bool hashMapValues_remove(hash_map_values_pt values, void * value) {
while (hashMapIterator_hasNext(iterator)) {
if (hashMapIterator_nextValue(iterator) == NULL) {
hashMapIterator_remove(iterator);
+ hashMapIterator_destroy(iterator);
return true;
}
}
@@ -511,10 +517,12 @@ bool hashMapValues_remove(hash_map_values_pt values, void * value) {
while (hashMapIterator_hasNext(iterator)) {
if (values->map->equalsValue(value, hashMapIterator_nextValue(iterator))) {
hashMapIterator_remove(iterator);
+ hashMapIterator_destroy(iterator);
return true;
}
}
}
+ hashMapIterator_destroy(iterator);
return false;
}
@@ -533,6 +541,11 @@ hash_map_entry_set_pt hashMapEntrySet_create(hash_map_pt map) {
return entrySet;
}
+void hashMapEntrySet_destroy(hash_map_entry_set_pt entrySet){
+ entrySet->map = NULL;
+ free(entrySet);
+}
+
int hashMapEntrySet_size(hash_map_entry_set_pt entrySet) {
return entrySet->map->size;
}
@@ -541,8 +554,14 @@ bool hashMapEntrySet_contains(hash_map_entry_set_pt entrySet, hash_map_entry_pt
return hashMap_containsValue(entrySet->map, entry);
}
-bool hashMapEntrySet_remove(hash_map_values_pt entrySet, hash_map_entry_pt entry) {
- return hashMap_removeMapping(entrySet->map, entry) != NULL;
+bool hashMapEntrySet_remove(hash_map_entry_set_pt entrySet, hash_map_entry_pt entry) {
+ hash_map_entry_pt temp = hashMap_removeMapping(entrySet->map, entry);
+ if (temp != NULL) {
+ free(temp);
+ return true;
+ } else {
+ return false;
+ }
}
void hashMapEntrySet_clear(hash_map_entry_set_pt entrySet) {
http://git-wip-us.apache.org/repos/asf/celix/blob/5498452e/utils/private/src/linked_list.c
----------------------------------------------------------------------
diff --git a/utils/private/src/linked_list.c b/utils/private/src/linked_list.c
index f949007..235c3e7 100644
--- a/utils/private/src/linked_list.c
+++ b/utils/private/src/linked_list.c
@@ -115,7 +115,7 @@ void linkedList_addLast(linked_list_pt list, void * element) {
}
bool linkedList_contains(linked_list_pt list, void * element) {
- return linkedList_indexOf(list, element) != 0;
+ return linkedList_indexOf(list, element) != -1;
}
int linkedList_size(linked_list_pt list) {
http://git-wip-us.apache.org/repos/asf/celix/blob/5498452e/utils/public/include/hash_map.h
----------------------------------------------------------------------
diff --git a/utils/public/include/hash_map.h b/utils/public/include/hash_map.h
index 5565cb7..f4f0acd 100644
--- a/utils/public/include/hash_map.h
+++ b/utils/public/include/hash_map.h
@@ -61,6 +61,7 @@ UTILS_EXPORT void * hashMapIterator_nextKey(hash_map_iterator_pt iterator);
UTILS_EXPORT hash_map_entry_pt hashMapIterator_nextEntry(hash_map_iterator_pt iterator);
UTILS_EXPORT hash_map_key_set_pt hashMapKeySet_create(hash_map_pt map);
+UTILS_EXPORT void hashMapKeySet_destroy(hash_map_key_set_pt keySet);
UTILS_EXPORT int hashMapKeySet_size(hash_map_key_set_pt keySet);
UTILS_EXPORT bool hashMapKeySet_contains(hash_map_key_set_pt keySet, void * key);
UTILS_EXPORT bool hashMapKeySet_remove(hash_map_key_set_pt keySet, void * key);
@@ -78,9 +79,10 @@ UTILS_EXPORT void hashMapValues_clear(hash_map_values_pt values);
UTILS_EXPORT bool hashMapValues_isEmpty(hash_map_values_pt values);
UTILS_EXPORT hash_map_entry_set_pt hashMapEntrySet_create(hash_map_pt map);
+UTILS_EXPORT void hashMapEntrySet_destroy(hash_map_entry_set_pt entrySet);
UTILS_EXPORT int hashMapEntrySet_size(hash_map_entry_set_pt entrySet);
UTILS_EXPORT bool hashMapEntrySet_contains(hash_map_entry_set_pt entrySet, hash_map_entry_pt entry);
-UTILS_EXPORT bool hashMapEntrySet_remove(hash_map_values_pt entrySet, hash_map_entry_pt entry);
+UTILS_EXPORT bool hashMapEntrySet_remove(hash_map_entry_set_pt entrySet, hash_map_entry_pt entry);
UTILS_EXPORT void hashMapEntrySet_clear(hash_map_entry_set_pt entrySet);
UTILS_EXPORT bool hashMapEntrySet_isEmpty(hash_map_entry_set_pt entrySet);
http://git-wip-us.apache.org/repos/asf/celix/blob/5498452e/utils/public/include/linked_list.h
----------------------------------------------------------------------
diff --git a/utils/public/include/linked_list.h b/utils/public/include/linked_list.h
index 22caabb..6321c5a 100644
--- a/utils/public/include/linked_list.h
+++ b/utils/public/include/linked_list.h
@@ -49,7 +49,7 @@ UTILS_EXPORT int linkedList_size(linked_list_pt list);
UTILS_EXPORT bool linkedList_isEmpty(linked_list_pt list);
UTILS_EXPORT bool linkedList_addElement(linked_list_pt list, void * element);
UTILS_EXPORT bool linkedList_removeElement(linked_list_pt list, void * element);
-UTILS_EXPORT void linkedlist_clear(linked_list_pt list);
+UTILS_EXPORT void linkedList_clear(linked_list_pt list);
UTILS_EXPORT void * linkedList_get(linked_list_pt list, int index);
UTILS_EXPORT void * linkedList_set(linked_list_pt list, int index, void * element);
UTILS_EXPORT void linkedList_addIndex(linked_list_pt list, int index, void * element);