Skip to content

Commit

Permalink
Fixed maps and added random number generator proto (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe authored Oct 24, 2023
1 parent 6ba029b commit 7d222c3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
26 changes: 26 additions & 0 deletions include/fuse/random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @file random.h
* @brief Function prototypes for random numbers.
*
* The first call to the generator creates a seed for subsequent calls.
* On the Pico, the documentation states that "methods may be safely called from either core
* or from an IRQ, but be careful in the latter case as the calls may block for a number of
* microseconds waiting on more entropy"
*/
#ifndef FUSE_RANDOM_H
#define FUSE_RANDOM_H
#include <stdint.h>

/** @brief Return a random unsigned 32 bit number
*
* @return An unsigned 32 bit number
*/
uint32_t rand_u32();


/** @brief Return a random unsigned 64 bit number
*
* @return An unsigned 64 bit number
*/
uint64_t rand_u64();

#endif /* FUSE_RANDOM_H */
27 changes: 17 additions & 10 deletions src/fuse/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void *fuse_map_get(fuse_map_t *self, void *key)
* @param value The value. If the value is NULL, then the key is deleted from the map.
* @return Returns false if the map is full.
*/
bool fuse_map_set(fuse_map_t *self, void *key, void* value)
bool fuse_map_set(fuse_map_t *self, void *key, void *value)
{
assert(self);
assert(key);
Expand All @@ -121,28 +121,35 @@ bool fuse_map_set(fuse_map_t *self, void *key, void* value)
// Walk through the nodes in the map, starting at the index
while (self->nodes[index].key != 0)
{
// If the key already exists
if (self->nodes[index].key == key || self->nodes[index].value == 0)
// If the key already exists or the value has been deleted, then set the value
// and return success. If the value is NULL then effectively the key is deleted,
// or else it is set.
if (self->nodes[index].key == key)
{
self->nodes[index].value = value;
self->count += value ? 1 : -1;
if (self->nodes[index].value != value)
{
self->nodes[index].value = value;
self->count += value ? 1 : -1;
}
return true;
}

// Move to the next node, wrapping around if necessary
// If the index is the same as the first index, then the table is full
// so return false, if the value is not null.
index = (index + 1) % self->size;
if (index == first)
{
// Full table, cannot set key
return false;
return value ? false : true;
}
}

// If the value is not null, then add the key to the map
if (value)
self->nodes[index].key = key;
if (self->nodes[index].value != value)
{
self->nodes[index].key = key;
self->nodes[index].value = value;
self->count++;
self->count += value ? 1 : -1;
}

// Key not found
Expand Down
1 change: 1 addition & 0 deletions tests/map/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ int main()
assert(TEST_001() == 0);
assert(TEST_002(10) == 0);
assert(TEST_002(100) == 0);
assert(TEST_002(1000) == 0);

// Return success
return 0;
Expand Down

0 comments on commit 7d222c3

Please sign in to comment.