Skip to content
Snippets Groups Projects
Commit 2926de72 authored by florian.burgener's avatar florian.burgener
Browse files

Validation exercice 2

parent 1b6ed1ef
No related branches found
No related tags found
No related merge requests found
......@@ -9,13 +9,14 @@
*
*/
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEY_MAXIMUM_LENGTH 4
#define HASH_MAP_LENGTH 10
#define VECTOR_DEFAULT_CAPACITY 100
typedef struct Entry {
char *key;
double value;
......@@ -32,6 +33,92 @@ typedef struct HashMap {
Vector **entries;
} HashMap;
/**
* @brief Hashes a string.
*
* @param key
* @param length
* @return int
*/
int hash(char key[4], int length);
/**
* @brief Initializes an entry.
*
* @param key
* @param value
* @return Entry*
*/
Entry *entry_init(char *key, double value);
/**
* @brief Frees the memory of the entry.
*
* @param e
*/
void entry_destroy(Entry *e);
/**
* @brief Initializes an vector.
*
* @param capacity
* @return Vector*
*/
Vector *vector_init(int capacity);
/**
* @brief Frees the memory of the vector.
*
* @param e
*/
void vector_destroy(Vector *v);
/**
* @brief Adds an element at the end of the vector.
*
* @param v
* @param e
*/
void vector_push(Vector *v, Entry *e);
/**
* @brief Displays the vector.
*
* @param v
*/
void vector_print(Vector *v);
/**
* @brief Initializes an hash map.
*
* @param length
* @return HashMap*
*/
HashMap *hash_map_init(int length);
/**
* @brief Frees the memory of the hash map.
*
* @param dict
*/
void hash_map_destroy(HashMap *dict);
/**
* @brief Inserts the value in the hash map.
*
* @param dict
* @param key
* @param value
*/
void hash_map_insert(HashMap *dict, char *key, double value);
/**
* @brief Displays the hash map.
*
* @param dict
*/
void hash_map_print(HashMap *dict);
int hash(char key[4], int length) {
int index = 0;
for (int i = 0; i < 4; ++i) {
......@@ -42,12 +129,15 @@ int hash(char key[4], int length) {
Entry *entry_init(char *key, double value) {
Entry *e = (Entry *)malloc(sizeof(Entry));
e->key = key;
e->key = (char *)malloc(sizeof(char) * (KEY_MAXIMUM_LENGTH + 1));
e->key[0] = '\0';
strcpy(e->key, key);
e->value = value;
return e;
}
void entry_destroy(Entry *e) {
free(e->key);
free(e);
}
......@@ -89,7 +179,7 @@ HashMap *hash_map_init(int length) {
dict->entries = (Vector **)malloc(sizeof(Vector *) * dict->length);
for (int i = 0; i < length; i += 1) {
dict->entries[i] = vector_init(100);
dict->entries[i] = vector_init(VECTOR_DEFAULT_CAPACITY);
}
return dict;
......@@ -115,7 +205,6 @@ void hash_map_insert(HashMap *dict, char *key, double value) {
}
}
// string copy
Entry *e = entry_init(key, value);
vector_push(v, e);
}
......@@ -129,7 +218,8 @@ void hash_map_print(HashMap *dict) {
}
int main() {
HashMap *dict = hash_map_init(10);
HashMap *dict = hash_map_init(HASH_MAP_LENGTH);
hash_map_insert(dict, "ajku", 1.1);
hash_map_insert(dict, "ajkv", 2.2);
hash_map_insert(dict, "ajkw", 3.1);
......@@ -142,10 +232,11 @@ int main() {
hash_map_insert(dict, "bjkd", 1.9);
hash_map_print(dict);
printf("\n");
hash_map_insert(dict, "ajkt", 0.1);
hash_map_insert(dict, "ajku", 9.1);
hash_map_insert(dict, "ajkx", 2.9);
printf("\n");
hash_map_print(dict);
hash_map_destroy(dict);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment