Fourth update on iterators - HASHSET
This commit is contained in:
1 parent
d3e7d64d2a
commit
ae5241a6fb
9 files changed
+387
-143
No files matched your search
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* iterator.c
|
||||
*
|
||||
* Creation Date: 15/05/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "hashset.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
// Int hash function
|
||||
size_t inthash(int t)
|
||||
{
|
||||
size_t a = t;
|
||||
a += ~(a << 15);
|
||||
a ^= (a >> 10);
|
||||
a += (a << 3);
|
||||
a ^= (a >> 6);
|
||||
a += ~(a << 11);
|
||||
a ^= (a >> 16);
|
||||
return a;
|
||||
}
|
||||
|
||||
// Int compare function
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
// Creates a hashset of integers
|
||||
HASHSET_GENERATE(hs, hashset, /* FMOD */, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// Create a new hashset with an initial capacity of 100 and load factor of 0.6
|
||||
hashset *my_hashset = hs_new(100, 0.6, intcmp, inthash);
|
||||
|
||||
// Add elements to the hashset
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
hs_insert(my_hashset, i);
|
||||
}
|
||||
|
||||
// Iterate from the start (index 0) to the end (index count - 1) of the hashset
|
||||
for (hashset_iter it = my_hashset->it_start(my_hashset); // Initialize iterator 'it' to the start of the hashset
|
||||
!hs_iter_end(&it); // Conditional stop (until it is not at the end)
|
||||
hs_iter_next(&it)) // Iterator goes one position forward
|
||||
{
|
||||
// Access the current element
|
||||
int e = hs_iter_value(&it);
|
||||
|
||||
// Access the current index (index is relative to all the elements in the iteration)
|
||||
size_t i = hs_iter_index(&it);
|
||||
|
||||
// Print hashset
|
||||
|
||||
// Index is useful in situations like this
|
||||
// If it is the first element
|
||||
if (i == 0)
|
||||
printf("[ %d, ", e);
|
||||
// If it is the last element
|
||||
else if (i == hs_count(my_hashset) - 1)
|
||||
printf("%d ]\n", e);
|
||||
// For elements at the middle
|
||||
else
|
||||
printf("%d, ", e);
|
||||
}
|
||||
|
||||
// Index can also be used to iterate over a certain portion of a collection
|
||||
size_t limit = hs_count(my_hashset) / 2;
|
||||
for (hashset_iter it = my_hashset->it_start(my_hashset); !hs_iter_end(&it); hs_iter_next(&it))
|
||||
{
|
||||
// Access the current element
|
||||
int e = hs_iter_value(&it);
|
||||
|
||||
// Access the current index (index is relative to all the elements in the iteration)
|
||||
size_t i = hs_iter_index(&it);
|
||||
|
||||
// Print hashset up until limit
|
||||
|
||||
// If it is the first element
|
||||
if (i == 0)
|
||||
printf("[ %d, ", e);
|
||||
// If it is the last element
|
||||
else if (i == limit - 1) // -1 because index is 0 based and we want half of the hashset
|
||||
{
|
||||
printf("%d ]\n", e);
|
||||
break;
|
||||
}
|
||||
// For elements at the middle
|
||||
else
|
||||
printf("%d, ", e);
|
||||
}
|
||||
|
||||
// Going backwards
|
||||
for (hashset_iter it = my_hashset->it_end(my_hashset); // Initialize iterator 'it' to the end of the hashset
|
||||
!hs_iter_start(&it); // Conditional stop (until it is not at the start)
|
||||
hs_iter_prev(&it)) // Iterator goes one position backwards
|
||||
{
|
||||
// Access the current element
|
||||
int e = hs_iter_value(&it);
|
||||
|
||||
// Access the current index (index is relative to all the elements in the iteration)
|
||||
size_t i = hs_iter_index(&it);
|
||||
|
||||
// Print hashset
|
||||
// Note how the conditions for the first and last elements are simply swapped
|
||||
// Because iterating backwards make the first element be the index count - 1
|
||||
// and the last one be the index 0
|
||||
|
||||
// If it is the first element
|
||||
if (i == hs_count(my_hashset) - 1)
|
||||
printf("[ %d, ", e);
|
||||
// If it is the last element
|
||||
else if (i == 0)
|
||||
printf("%d ]\n", e);
|
||||
// For elements at the middle
|
||||
else
|
||||
printf("%d, ", e);
|
||||
}
|
||||
|
||||
// You can also use iterators in a while loop
|
||||
// But to do so you must declare the iterator struct outside the loop first
|
||||
hashset_iter it;
|
||||
|
||||
// You have three options:
|
||||
// - Initialize it using the functions provided by the hashset [ my_hashset->it_start() and my_hashset->it_end() ]
|
||||
// - Initialize it using the init() function (when the iterator is already allocated)
|
||||
// - Initialize it using the new() function (allocates on the heap, pointers only)
|
||||
|
||||
it = my_hashset->it_start(my_hashset);
|
||||
it = my_hashset->it_end(my_hashset);
|
||||
|
||||
// This is equivalent to it_start()
|
||||
hs_iter_init(&it, my_hashset);
|
||||
|
||||
// For pointers
|
||||
// Also equivalent to it_start()
|
||||
hashset_iter *iter = hs_iter_new(my_hashset);
|
||||
|
||||
// The actual while loop
|
||||
while (!hs_iter_end(iter))
|
||||
{
|
||||
// Accessing elements
|
||||
int e = hs_iter_value(iter);
|
||||
size_t i = hs_iter_index(iter);
|
||||
|
||||
// Print index and element
|
||||
printf("hashset[%2" PRIuMAX "] = %2d\n", i, e);
|
||||
|
||||
// Move iterator forwards
|
||||
hs_iter_next(iter);
|
||||
}
|
||||
|
||||
// Send the iterator to the start of the hashset
|
||||
hs_iter_to_start(iter);
|
||||
|
||||
// Send the iterator to the end of the hashset
|
||||
hs_iter_to_end(iter);
|
||||
|
||||
// Backwards loop
|
||||
while (!hs_iter_start(iter))
|
||||
{
|
||||
// Accessing elements
|
||||
int e = hs_iter_value(iter);
|
||||
size_t i = hs_iter_index(iter);
|
||||
|
||||
// Print index and element
|
||||
printf("hashset[%2" PRIuMAX "] = %2d\n", i, e);
|
||||
|
||||
// Move iterator backwards
|
||||
hs_iter_prev(iter);
|
||||
}
|
||||
|
||||
// Certain iterators also allow you to modify the contents it is iterating over
|
||||
// Exceptions are collections that depend of the current state of the element
|
||||
// like tree-based collections and heap (collections that have some kind of ordering).
|
||||
// You also can't change the keys of an associative collection
|
||||
|
||||
// In the case of a hashset or any set, this is not possible because the
|
||||
// values of a set are its keys. Changing the elements could cause the set
|
||||
// to be in an invalid state.
|
||||
|
||||
// Dispose the iterator called by hs_iter_new()
|
||||
hs_iter_free(iter);
|
||||
// Dispose the hashset created
|
||||
hs_free(my_hashset);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -22,13 +22,13 @@ HASHSET_GENERATE(set, set, /* static */, int)
|
||||
|
||||
void print_set(set *s)
|
||||
{
|
||||
size_t index;
|
||||
int result;
|
||||
set_iter iter;
|
||||
set_iter_init(&iter, s);
|
||||
for (set_iter_tostart(&iter); !set_iter_end(&iter);)
|
||||
for (set_iter_to_start(&iter); !set_iter_end(&iter); set_iter_next(&iter))
|
||||
{
|
||||
set_iter_next(&iter, &result, &index);
|
||||
int result = set_iter_value(&iter);
|
||||
size_t index = set_iter_index(&iter);
|
||||
|
||||
if (index == 0)
|
||||
printf("[ %2d, ", result);
|
||||
else if (index == s->count - 1)
|
||||
|
||||
Reference in new issue
Block a user