Skip to content
Snippets Groups Projects
Commit 3f2c2dab authored by Bruce Cowan's avatar Bruce Cowan :airplane:
Browse files

More directory excitement and a few additions

Array capacity changes display
Some sort of string to double thing
Fixed format string visualisation
parent 69cd8523
No related branches found
No related tags found
No related merge requests found
Showing with 121 additions and 51 deletions
subdir('src')
if glib_dep.found()
subdir('test')
endif
src = files('array.c', 'mem.c')
lib = shared_library('array', src, dependencies: libm)
lib_inc = include_directories('.')
utils_lib = library('utils', 'utils.c') subdir('types')
utils_dep = declare_dependency(link_with: utils_lib, subdir('utils')
include_directories: '.')
array_lib = library('array',
['array.c', 'mem.c'],
dependencies: libm)
array_dep = declare_dependency(link_with: array_lib,
include_directories: '.')
File moved
File moved
#include "hashtable.h" #include "hashtable.h"
#include "mem.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
typedef struct
{
char *key;
char *value;
} KeyValue;
/** /**
* HashTable: * HashTable:
* *
...@@ -32,14 +39,6 @@ hash_table_new (size_t size) ...@@ -32,14 +39,6 @@ hash_table_new (size_t size)
return new; return new;
} }
/**
* strhash:
* @string: the string
*
* Converts a string into a hash value.
*
* Returns: the hash
*/
static unsigned static unsigned
strhash (const char *string) strhash (const char *string)
{ {
...@@ -69,7 +68,11 @@ hash_table_insert (HashTable *table, ...@@ -69,7 +68,11 @@ hash_table_insert (HashTable *table,
{ {
unsigned index = strhash (key) % table->size; unsigned index = strhash (key) % table->size;
SList *list = slist_prepend (table->array[index], key, value); KeyValue *kv = check_malloc (sizeof (KeyValue));
kv->key = strdup (key);
kv->value = strdup (value);
SList *list = slist_prepend (table->array[index], kv);
table->array[index] = list; table->array[index] = list;
} }
...@@ -90,6 +93,9 @@ hash_table_print_all (HashTable *table) ...@@ -90,6 +93,9 @@ hash_table_print_all (HashTable *table)
continue; continue;
for (SList *list = bucket; list; list = list->next) for (SList *list = bucket; list; list = list->next)
printf ("%s:%s\n", list->key, list->value); {
KeyValue *kv = list->data;
printf ("%s:%s\n", kv->key, kv->value);
}
} }
} }
File moved
File moved
File moved
types_src = ['array.c', 'hashtable.c', 'mem.c', 'slist.c']
types_lib = library('types', types_src, dependencies: libm)
types_dep = declare_dependency(link_with: types_lib, include_directories: '.')
File moved
File moved
utils_lib = library('utils', 'utils.c')
utils_dep = declare_dependency(link_with: utils_lib,
include_directories: '.')
File moved
File moved
#include <stdio.h>
#include <array.h>
static void
print_details (Array *arr)
{
size_t len = array_get_length (arr);
size_t capacity = array_get_capacity (arr);
printf ("Array length %zu, Array capacity %zu\n", len, capacity);
}
int
main (void)
{
Array *arr = array_new (NULL);
print_details (arr);
for (int i = 0; i < 10; i++)
{
array_add (arr, NULL);
print_details (arr);
}
for (int i = 0; i < 10; i++)
{
array_remove_fast (arr, 0);
print_details (arr);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define STR "1.2 2.3 3.4 4.5 5.6 6.7 7.8"
#define LEN 7
int
main (void)
{
char *str = STR;
char *rem = str;
double arr[LEN];
for (int i = 0; i < LEN; i++)
{
double val = strtod (rem, &rem);
arr[i] = val;
}
for (int i = 0; i < LEN; i++)
printf ("Value %d is %lf\n", i, arr[i]);
return EXIT_SUCCESS;
}
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
int
main (void)
{
printf ("Print formats\n=============\n");
printf ("Format character for int8_t is %s\n", PRId8);
printf ("Format character for int16_t is %s\n", PRId16);
printf ("Format character for int32_t is %s\n", PRId32);
printf ("Format character for int64_t is %s\n", PRId64);
printf ("Format character for int_fast8_t is %s\n", PRIdFAST8);
printf ("Format character for int_fast16_t is %s\n", PRIdFAST16);
printf ("Format character for int_fast32_t is %s\n", PRIdFAST32);
printf ("Format character for int_fast64_t is %s\n", PRIdFAST64);
printf ("Format character for int_least8_t is %s\n", PRIdLEAST8);
printf ("Format character for int_least16_t is %s\n", PRIdLEAST16);
printf ("Format character for int_least32_t is %s\n", PRIdLEAST32);
printf ("Format character for int_least64_t is %s\n", PRIdLEAST64);
printf ("\nScan formats\n============\n");
printf ("Format character for int8_t is %s\n", SCNd8);
printf ("Format character for int16_t is %s\n", SCNd16);
printf ("Format character for int32_t is %s\n", SCNd32);
printf ("Format character for int64_t is %s\n", SCNd64);
printf ("Format character for int_fast8_t is %s\n", SCNdFAST8);
printf ("Format character for int_fast16_t is %s\n", SCNdFAST16);
printf ("Format character for int_fast32_t is %s\n", SCNdFAST32);
printf ("Format character for int_fast64_t is %s\n", SCNdFAST64);
printf ("Format character for int_least8_t is %s\n", SCNdLEAST8);
printf ("Format character for int_least16_t is %s\n", SCNdLEAST16);
printf ("Format character for int_least32_t is %s\n", SCNdLEAST32);
printf ("Format character for int_least64_t is %s\n", SCNdLEAST64);
return 0;
}
sources = files('hashtable.c',
'slist.c',
'test.c')
executable('hash', sources)
#include "slist.h"
#include <stdlib.h>
#include <string.h>
SList *
slist_prepend (SList *list,
const char *key,
const char *value)
{
SList *new;
new = malloc (sizeof (SList));
new->key = strdup (key);
new->value = strdup (value);
new->next = list;
return new;
}
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