Skip to content

Commit

Permalink
Merge pull request #38 from ZSShen/dev
Browse files Browse the repository at this point in the history
Merge demo code
  • Loading branch information
ZSShen committed Jan 13, 2019
2 parents 8001ce2 + 43f5e07 commit c633e61
Show file tree
Hide file tree
Showing 9 changed files with 1,144 additions and 2 deletions.
109 changes: 109 additions & 0 deletions demo/demo_hash_map.c
Expand Up @@ -136,9 +136,118 @@ void ManipulateTexts()
HashMapDeinit(map);
}

void ManipulateNumericsCppStyle()
{
/* We should initialize the container before any operations. */
HashMap* map = HashMapInit();

/* Insert numerics into the map. */
map->put(map, (void*)1, (void*)999);
map->put(map, (void*)2, (void*)99);
map->put(map, (void*)3, (void*)9);

/* Retrieve the value with the designated key. */
int val = (int)(intptr_t)map->get(map, (void*)1);
assert(val == 999);

/* Iterate through the map. */
Pair* ptr_pair;
map->first(map);
while ((ptr_pair = map->next(map)) != NULL) {
int key = (int)(intptr_t)ptr_pair->key;
int val = (int)(intptr_t)ptr_pair->value;
}

/* Remove the key value pair with the designated key. */
map->remove(map, (void*)2);

/* Check the map keys. */
assert(map->contain(map, (void*)1) == true);
assert(map->contain(map, (void*)2) == false);
assert(map->contain(map, (void*)3) == true);

/* Check the pair count in the map. */
unsigned size = map->size(map);
assert(size == 2);

/* We should deinitialize the container after all the relevant operations. */
HashMapDeinit(map);
}

void ManipulateTextsCppStyle()
{
char* names[3] = {"Alice\0", "Bob\0", "Chris\0"};

/* We should initialize the container before any operations. */
HashMap* map = HashMapInit();

/* Set the custom hash value generator and key comparison functions. */
HashMapSetHash(map, HashKey);
HashMapSetCompare(map, CompareKey);

/* If we plan to delegate the resource clean task to the container, set the
custom clean functions. */
HashMapSetCleanKey(map, CleanKey);
HashMapSetCleanValue(map, CleanValue);

/* Insert complex data payload into the map. */
char* key = strdup(names[0]);
Employ* employ = (Employ*)malloc(sizeof(Employ));
employ->id = 1;
employ->year = 25;
employ->level = 100;
map->put(map, (void*)key, (void*)employ);

key = strdup(names[1]);
employ = (Employ*)malloc(sizeof(Employ));
employ->id = 2;
employ->year = 25;
employ->level = 90;
map->put(map, (void*)key, (void*)employ);

key = strdup(names[2]);
employ = (Employ*)malloc(sizeof(Employ));
employ->id = 3;
employ->year = 25;
employ->level = 80;
map->put(map, (void*)key, (void*)employ);

/* Retrieve the value with the designated key. */
employ = (Employ*)map->get(map, (void*)names[0]);
assert(employ != NULL);
assert(employ->id == 1);
assert(employ->year == 25);
assert(employ->level == 100);

/* Iterate through the map. */
Pair* ptr_pair;
HashMapFirst(map);
while ((ptr_pair = map->next(map)) != NULL) {
char* name = (char*)ptr_pair->key;
employ = (Employ*)ptr_pair->value;
}

/* Remove the key value pair with the designated key. */
map->remove(map, (void*)names[1]);

/* Check the map keys. */
assert(map->contain(map, (void*)names[0]) == true);
assert(map->contain(map, (void*)names[1]) == false);
assert(map->contain(map, (void*)names[2]) == true);

/* Check the pair count in the map. */
unsigned size = map->size(map);
assert(size == 2);

/* We should deinitialize the container after all the relevant operations. */
HashMapDeinit(map);
}

int main()
{
ManipulateNumerics();
ManipulateTexts();
ManipulateNumericsCppStyle();
ManipulateTextsCppStyle();
return 0;
}
183 changes: 183 additions & 0 deletions demo/demo_hash_set.c
Expand Up @@ -198,9 +198,192 @@ void ManipulateTexts()
HashSetDeinit(set_rhs);
}

void ManipulateNumericsCppStyle()
{
/* We should initialize the container before any operations. */
HashSet* set_lhs = HashSetInit();
HashSet* set_rhs = HashSetInit();

/* Insert numerics into the sets. */
set_lhs->add(set_lhs, (void*)1);
set_lhs->add(set_lhs, (void*)2);
set_lhs->add(set_lhs, (void*)3);
set_lhs->add(set_lhs, (void*)4);

set_rhs->add(set_rhs, (void*)3);
set_rhs->add(set_rhs, (void*)4);
set_rhs->add(set_rhs, (void*)5);
set_rhs->add(set_rhs, (void*)6);

/* Remove some elements from the sets. */
set_lhs->remove(set_lhs, (void*)1);
set_rhs->remove(set_rhs, (void*)6);

/* Query for element existence. */
assert(set_lhs->find(set_lhs, (void*)1) == false);
assert(set_lhs->find(set_lhs, (void*)2) == true);
assert(set_rhs->find(set_rhs, (void*)5) == true);
assert(set_rhs->find(set_rhs, (void*)6) == false);

/* Iterate through the set. */
void* element;
set_lhs->first(set_lhs);
while ((element = set_lhs->next(set_lhs)) != NULL) {
// Consume the element.
}

/* Check the element count in the set. */
assert(set_lhs->size(set_lhs) == 3);
assert(set_lhs->size(set_rhs) == 3);

/* Perform set union operation. */
HashSet* merge = HashSetUnion(set_lhs, set_rhs);
assert(merge->size(merge) == 4);
assert(merge->find(merge, (void*)1) == false);
assert(merge->find(merge, (void*)2) == true);
assert(merge->find(merge, (void*)3) == true);
assert(merge->find(merge, (void*)4) == true);
assert(merge->find(merge, (void*)5) == true);
assert(merge->find(merge, (void*)6) == false);

/* Perform set intersection operation. */
HashSet* inter = HashSetIntersect(set_lhs, set_rhs);
assert(inter->size(inter) == 2);
assert(inter->find(inter, (void*)1) == false);
assert(inter->find(inter, (void*)2) == false);
assert(inter->find(inter, (void*)3) == true);
assert(inter->find(inter, (void*)4) == true);
assert(inter->find(inter, (void*)5) == false);
assert(inter->find(inter, (void*)6) == false);

/* Perform set difference operation. */
HashSet* only_lhs = HashSetDifference(set_lhs, set_rhs);
assert(only_lhs->size(only_lhs) == 1);
assert(only_lhs->find(only_lhs, (void*)1) == false);
assert(only_lhs->find(only_lhs, (void*)2) == true);
assert(only_lhs->find(only_lhs, (void*)3) == false);
assert(only_lhs->find(only_lhs, (void*)4) == false);
assert(only_lhs->find(only_lhs, (void*)5) == false);
assert(only_lhs->find(only_lhs, (void*)6) == false);

HashSet* only_rhs = HashSetDifference(set_rhs, set_lhs);
assert(only_rhs->size(only_rhs) == 1);
assert(only_rhs->find(only_rhs, (void*)1) == false);
assert(only_rhs->find(only_rhs, (void*)2) == false);
assert(only_rhs->find(only_rhs, (void*)3) == false);
assert(only_rhs->find(only_rhs, (void*)4) == false);
assert(only_rhs->find(only_rhs, (void*)5) == true);
assert(only_rhs->find(only_rhs, (void*)6) == false);

/* We should deinitialize the container after all the relevant operations. */
HashSetDeinit(only_lhs);
HashSetDeinit(only_rhs);
HashSetDeinit(inter);
HashSetDeinit(merge);
HashSetDeinit(set_lhs);
HashSetDeinit(set_rhs);
}

void ManipulateTextsCppStyle()
{
char* names[6] = {"Alice\0", "Bob\0", "Carol\0", "Dave\0", "Oscar\0", "Will\0"};

/* We should initialize the container before any operations. */
HashSet* set_lhs = HashSetInit();
HashSetSetHash(set_lhs, HashKey);
HashSetSetCompare(set_lhs, CompareKey);
HashSetSetCleanKey(set_lhs, CleanKey);

HashSet* set_rhs = HashSetInit();
HashSetSetHash(set_rhs, HashKey);
HashSetSetCompare(set_rhs, CompareKey);
HashSetSetCleanKey(set_rhs, CleanKey);

/* Insert numerics into the sets. */
set_lhs->add(set_lhs, strdup(names[0]));
set_lhs->add(set_lhs, strdup(names[1]));
set_lhs->add(set_lhs, strdup(names[2]));
set_lhs->add(set_lhs, strdup(names[3]));

set_rhs->add(set_rhs, strdup(names[2]));
set_rhs->add(set_rhs, strdup(names[3]));
set_rhs->add(set_rhs, strdup(names[4]));
set_rhs->add(set_rhs, strdup(names[5]));

/* Remove some elements from the sets. */
set_lhs->remove(set_lhs, names[0]);
set_rhs->remove(set_rhs, names[5]);

/* Query for element existence. */
assert(set_lhs->find(set_lhs, names[0]) == false);
assert(set_lhs->find(set_lhs, names[1]) == true);
assert(set_rhs->find(set_rhs, names[4]) == true);
assert(set_rhs->find(set_rhs, names[5]) == false);

/* Iterate through the set. */
void* element;
set_lhs->first(set_lhs);
while ((element = set_rhs->next(set_lhs)) != NULL) {
// Consume the element.
}

/* Check the element count in the set. */
assert(set_lhs->size(set_lhs) == 3);
assert(set_rhs->size(set_rhs) == 3);

/* Perform set union operation. */
HashSet* merge = HashSetUnion(set_lhs, set_rhs);
assert(merge->size(merge) == 4);
assert(merge->find(merge, names[0]) == false);
assert(merge->find(merge, names[1]) == true);
assert(merge->find(merge, names[2]) == true);
assert(merge->find(merge, names[3]) == true);
assert(merge->find(merge, names[4]) == true);
assert(merge->find(merge, names[5]) == false);

/* Perform set intersection operation. */
HashSet* inter = HashSetIntersect(set_lhs, set_rhs);
assert(inter->size(inter) == 2);
assert(inter->find(inter, names[0]) == false);
assert(inter->find(inter, names[1]) == false);
assert(inter->find(inter, names[2]) == true);
assert(inter->find(inter, names[3]) == true);
assert(inter->find(inter, names[4]) == false);
assert(inter->find(inter, names[5]) == false);

/* Perform set difference operation. */
HashSet* only_lhs = HashSetDifference(set_lhs, set_rhs);
assert(only_lhs->size(only_lhs) == 1);
assert(only_lhs->find(only_lhs, names[0]) == false);
assert(only_lhs->find(only_lhs, names[1]) == true);
assert(only_lhs->find(only_lhs, names[2]) == false);
assert(only_lhs->find(only_lhs, names[3]) == false);
assert(only_lhs->find(only_lhs, names[4]) == false);
assert(only_lhs->find(only_lhs, names[5]) == false);

HashSet* only_rhs = HashSetDifference(set_rhs, set_lhs);
assert(only_rhs->size(only_rhs) == 1);
assert(only_rhs->find(only_rhs, names[0]) == false);
assert(only_rhs->find(only_rhs, names[1]) == false);
assert(only_rhs->find(only_rhs, names[2]) == false);
assert(only_rhs->find(only_rhs, names[3]) == false);
assert(only_rhs->find(only_rhs, names[4]) == true);
assert(only_rhs->find(only_rhs, names[5]) == false);

/* We should deinitialize the container after all the relevant operations. */
HashSetDeinit(only_lhs);
HashSetDeinit(only_rhs);
HashSetDeinit(inter);
HashSetDeinit(merge);
HashSetDeinit(set_lhs);
HashSetDeinit(set_rhs);
}

int main()
{
ManipulateNumerics();
ManipulateTexts();
ManipulateNumericsCppStyle();
ManipulateTextsCppStyle();
return 0;
}

0 comments on commit c633e61

Please sign in to comment.