Toychain!
A toy blockchain in C
vote.c File Reference

A set of functions for the voting system. More...

#include "lib/vote.h"
#include "lib/types.h"
#include "lib/mmath.h"
#include "lib/dataio.h"
#include "lib/error.h"
#include <stdlib.h>
#include <assert.h>
Include dependency graph for vote.c:

Functions

HashCellcreate_hashcell_raw (Key *key)
 Create a hashcell raw object. Takes ownership of key. More...
 
HashCellcreate_hashcell (const Key *key)
 Create a hashcell object. Copies key. More...
 
void free_hashcell (HashCell *hc)
 Free a HasCell object. More...
 
uint32 hash_function (const Key *key, int size)
 Hash function for Key arrays (HashTable). More...
 
uint32 find_position (const HashTable *t, const Key *k)
 Find the position to place k at in the HasTable t. If the position in the array is taken, find a new one using linear probing. If no position was found, returns the originally found position of hash_function(). More...
 
HashTablecreate_hashtable (const CellKey *keys, size_t size)
 Create a HashTable object containing keys elements. Keys position are found with find_position().. More...
 
void free_hashtable (HashTable *t)
 Free a HashTable object and all the elements of the table. (Correspond to delete_hashtable in the project subject). More...
 
Keycompute_winner (const CellProtected *decl, const CellKey *candidates, const CellKey *voters, int sizeC, int sizeV)
 Simulates an election process and returns the winner. Checks if declarations are valid and update vote numbers for each candidate. More...
 

Detailed Description

A set of functions for the voting system.

Function Documentation

◆ compute_winner()

Key * compute_winner ( const CellProtected decl,
const CellKey candidates,
const CellKey voters,
int  sizeC,
int  sizeV 
)

Simulates an election process and returns the winner. Checks if declarations are valid and update vote numbers for each candidate.

Parameters
decllist of declaration
cadidateslist of candidates
voterslist of voters
sizeCsize of candidates hashtable
sizeVsize of voters hashtable
Returns
Key* copy of the key of the winning candidate

◆ create_hashcell()

HashCell * create_hashcell ( const Key key)

Create a hashcell object. Copies key.

Parameters
key
Returns
HashCell*

◆ create_hashcell_raw()

HashCell * create_hashcell_raw ( Key key)

Create a hashcell raw object. Takes ownership of key.

Parameters
key
Returns
HashCell*

◆ create_hashtable()

HashTable * create_hashtable ( const CellKey keys,
size_t  size 
)

Create a HashTable object containing keys elements. Keys position are found with find_position()..

Parameters
keyskeys to store
sizesize of the table
Returns
HashTable*

◆ find_position()

uint32 find_position ( const HashTable t,
const Key k 
)

Find the position to place k at in the HasTable t. If the position in the array is taken, find a new one using linear probing. If no position was found, returns the originally found position of hash_function().

Parameters
tTable to store in
kKey to store
Returns
uint32 : found position.

◆ free_hashcell()

void free_hashcell ( HashCell hc)

Free a HasCell object.

Parameters
hcObject to free.

◆ free_hashtable()

void free_hashtable ( HashTable t)

Free a HashTable object and all the elements of the table. (Correspond to delete_hashtable in the project subject).

Parameters
tobject to free

◆ hash_function()

uint32 hash_function ( const Key key,
int  size 
)

Hash function for Key arrays (HashTable).

Parameters
key
sizelength of the array
Returns
uint32 : position in the array