Update comments and simplify
This commit is contained in:
1 parent
2b44892aad
commit
e917ac5663
8 files changed
+50
-32
No files matched your search
+2
-1
@@ -82,7 +82,7 @@ int main(void)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 7:
|
case 6:
|
||||||
dict_map_clear(mp);
|
dict_map_clear(mp);
|
||||||
string_clear (str);
|
string_clear (str);
|
||||||
exit(0);
|
exit(0);
|
||||||
@@ -90,6 +90,7 @@ int main(void)
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
printf("Wrong Choice\n");
|
printf("Wrong Choice\n");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+7
-3
@@ -26,11 +26,14 @@ int main(int argc, const char *argv[])
|
|||||||
M_LET(positions, map_pos_t) {
|
M_LET(positions, map_pos_t) {
|
||||||
// Open the file
|
// Open the file
|
||||||
FILE *f = fopen(argv[1], "rt");
|
FILE *f = fopen(argv[1], "rt");
|
||||||
if (!f) return 1;
|
if (!f) {
|
||||||
|
fprintf(stderr, "ERROR: Cannot open %s.\n", argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// While it reads some word from the file
|
// While it reads some word from the file
|
||||||
while (string_fget_word(word, " \t\n\r\"(),=", f)) {
|
while (string_fget_word(word, " \t\n\r\"(),=", f)) {
|
||||||
// Get the reference to this word in the dictionnary (or create it if needed)
|
// Get the reference to the array of this word in the dictionnary (or create it if needed)
|
||||||
array_size_t *array = map_pos_get_at(positions, word);
|
array_size_t *array = map_pos_get_at(positions, word);
|
||||||
// Push the new offset to this word in the associated array
|
// Push the new offset to this word in the associated array
|
||||||
array_size_push_back(*array, (size_t)ftell(f) - string_size(word));
|
array_size_push_back(*array, (size_t)ftell(f) - string_size(word));
|
||||||
@@ -42,8 +45,9 @@ int main(int argc, const char *argv[])
|
|||||||
// Print the words and where they are:
|
// Print the words and where they are:
|
||||||
for M_EACH(pair, positions, map_pos_t) {
|
for M_EACH(pair, positions, map_pos_t) {
|
||||||
string_fputs(stdout, pair->key);
|
string_fputs(stdout, pair->key);
|
||||||
for M_EACH(pos, pair->value, array_size_t)
|
for M_EACH(pos, pair->value, array_size_t) {
|
||||||
printf(" %zu", *pos);
|
printf(" %zu", *pos);
|
||||||
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
} // All created objects are destroyed beyond this point
|
} // All created objects are destroyed beyond this point
|
||||||
|
|||||||
+17
-8
@@ -24,22 +24,25 @@
|
|||||||
|
|
||||||
/* Define the needed data structure : */
|
/* Define the needed data structure : */
|
||||||
|
|
||||||
// Define a dictionnary of constant string --> integer
|
// Define a dictionnary of constant string --> integer and register it.
|
||||||
DICT_DEF2(dict_mph, const char *, M_CSTR_OPLIST, uint32_t, M_DEFAULT_OPLIST)
|
DICT_DEF2(dict_mph, const char *, M_CSTR_OPLIST, uint32_t, M_DEFAULT_OPLIST)
|
||||||
#define M_OPL_dict_mph_t() DICT_OPLIST(dict_mph, M_CSTR_OPLIST, M_DEFAULT_OPLIST)
|
#define M_OPL_dict_mph_t() DICT_OPLIST(dict_mph, M_CSTR_OPLIST, M_DEFAULT_OPLIST)
|
||||||
|
|
||||||
// Defin an array of integer
|
// Define an array of integer to store the value and register it.
|
||||||
ARRAY_DEF(array_value, uint32_t) // Start from 1. 0 is 'None'
|
ARRAY_DEF(array_value, uint32_t) // Start from 1. 0 is 'None'
|
||||||
#define M_OPL_array_value_t() ARRAY_OPLIST(array_value)
|
#define M_OPL_array_value_t() ARRAY_OPLIST(array_value)
|
||||||
|
|
||||||
// Defin an array of integer
|
// Define an array of integer to store the seed and register it.
|
||||||
ARRAY_DEF(array_seed, int32_t)
|
ARRAY_DEF(array_seed, int32_t)
|
||||||
#define M_OPL_array_seed_t() ARRAY_OPLIST(array_seed)
|
#define M_OPL_array_seed_t() ARRAY_OPLIST(array_seed)
|
||||||
|
// Define algorithms on array_seed
|
||||||
ALGO_DEF(array_seed, array_seed_t)
|
ALGO_DEF(array_seed, array_seed_t)
|
||||||
|
|
||||||
// Define an array of constant string that are ordered by their size.
|
// Define an array of constant string that are ordered by their size.
|
||||||
ARRAY_DEF(array_cstr, const char *, M_CSTR_OPLIST)
|
ARRAY_DEF(array_cstr, const char *, M_CSTR_OPLIST)
|
||||||
|
// Register it globally but with a custom CMP operator
|
||||||
static inline int array_cstr_cmp(const array_cstr_t a, const array_cstr_t b) {
|
static inline int array_cstr_cmp(const array_cstr_t a, const array_cstr_t b) {
|
||||||
|
// We compare only the size
|
||||||
size_t sa = array_cstr_size(a);
|
size_t sa = array_cstr_size(a);
|
||||||
size_t sb = array_cstr_size(b);
|
size_t sb = array_cstr_size(b);
|
||||||
return (sa > sb) ? -1 : (sa < sb);
|
return (sa > sb) ? -1 : (sa < sb);
|
||||||
@@ -191,7 +194,11 @@ static void
|
|||||||
dict_read_from_file(dict_mph_t dict, array_string_t arr, const char filename[])
|
dict_read_from_file(dict_mph_t dict, array_string_t arr, const char filename[])
|
||||||
{
|
{
|
||||||
FILE *f = fopen(filename, "rt");
|
FILE *f = fopen(filename, "rt");
|
||||||
if (!f) abort();
|
if (!f) {
|
||||||
|
fprintf(stderr, "ERROR: Cannot open %s\n", filename);
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t line = 1;
|
uint32_t line = 1;
|
||||||
M_LET(str, string_t) {
|
M_LET(str, string_t) {
|
||||||
while (!feof(f) && !ferror(f)) {
|
while (!feof(f) && !ferror(f)) {
|
||||||
@@ -234,12 +241,14 @@ test(array_seed_t seed, array_value_t value, dict_mph_t dict)
|
|||||||
|
|
||||||
int main(int argc, const char *argv[])
|
int main(int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
|
// Create arr as array_string_t, dict as dist_mph_t and seed as array_seed_t
|
||||||
M_LET(arr, array_string_t) M_LET(dict, dict_mph_t) M_LET(seed, array_seed_t)
|
M_LET(arr, array_string_t) M_LET(dict, dict_mph_t) M_LET(seed, array_seed_t)
|
||||||
|
// and value as array_value_t
|
||||||
M_LET(value, array_value_t) {
|
M_LET(value, array_value_t) {
|
||||||
const char *filename = (argc > 1) ? argv[1] :"/usr/share/dict/words";
|
const char *filename = (argc > 1) ? argv[1] :"/usr/share/dict/words";
|
||||||
dict_read_from_file(dict, arr, filename);
|
dict_read_from_file(dict, arr, filename);
|
||||||
CreateMinimalPerfectHash(seed, value, dict);
|
CreateMinimalPerfectHash(seed, value, dict);
|
||||||
test(seed, value, dict);
|
test(seed, value, dict);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "m-array.h"
|
#include "m-array.h"
|
||||||
#include "m-string.h"
|
#include "m-string.h"
|
||||||
|
|
||||||
|
// Define an array of integer
|
||||||
ARRAY_DEF(array_int, int)
|
ARRAY_DEF(array_int, int)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -25,7 +26,7 @@ static void tape_devance(tape_t tape) { if (tape->pos > 0) tape->pos--; }
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
string_t code;
|
string_t code;
|
||||||
array_int_t bracket_map; // Should use map when possible.
|
array_int_t bracket_map;
|
||||||
} program_t[1];
|
} program_t[1];
|
||||||
|
|
||||||
static void program_init(program_t p, string_t text) {
|
static void program_init(program_t p, string_t text) {
|
||||||
@@ -98,7 +99,11 @@ static void program_run(program_t p){
|
|||||||
|
|
||||||
static void read_file(string_t str, const string_t filename) {
|
static void read_file(string_t str, const string_t filename) {
|
||||||
FILE *f = fopen(string_get_cstr(filename), "rt");
|
FILE *f = fopen(string_get_cstr(filename), "rt");
|
||||||
if (!f) abort();
|
if (!f) {
|
||||||
|
fprintf(stderr, "ERROR: Cannot open %s\n", string_get_cstr(filename));
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
// Read the full FILE in the string
|
||||||
string_fgets (str, f, STRING_READ_FILE);
|
string_fgets (str, f, STRING_READ_FILE);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ TUPLE_DEF2(mlib_points, (first_ref, mlib_point_ptr, M_PTR_OPLIST),
|
|||||||
|
|
||||||
static void let2(void)
|
static void let2(void)
|
||||||
{
|
{
|
||||||
|
// Initialize p1 as mlib_point_t with (4,4)
|
||||||
|
// and p2 as mlib_point_t with (5,5)
|
||||||
|
// and pair as mlib_point_t with default value
|
||||||
M_LET((p1, 4, 4), (p2, 5, 5), mlib_point_t) {
|
M_LET((p1, 4, 4), (p2, 5, 5), mlib_point_t) {
|
||||||
M_LET(pair, mlib_points_t) {
|
M_LET(pair, mlib_points_t) {
|
||||||
pair->first_ref = p1;
|
pair->first_ref = p1;
|
||||||
@@ -68,6 +71,7 @@ static void let2(void)
|
|||||||
|
|
||||||
static void let(void)
|
static void let(void)
|
||||||
{
|
{
|
||||||
|
// Let's initialize all the declared types
|
||||||
M_LET(s, symbol_t)
|
M_LET(s, symbol_t)
|
||||||
M_LET(l, symbol_list_t)
|
M_LET(l, symbol_list_t)
|
||||||
M_LET(r,record_t)
|
M_LET(r,record_t)
|
||||||
|
|||||||
@@ -2,9 +2,6 @@
|
|||||||
#include "m-tuple.h"
|
#include "m-tuple.h"
|
||||||
#include "m-array.h"
|
#include "m-array.h"
|
||||||
|
|
||||||
// Serial json is not supported for standard types if not C11
|
|
||||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
|
||||||
|
|
||||||
// Let's define a 'person' composed of several fields.
|
// Let's define a 'person' composed of several fields.
|
||||||
TUPLE_DEF2(person,
|
TUPLE_DEF2(person,
|
||||||
(surname, string_t),
|
(surname, string_t),
|
||||||
@@ -50,7 +47,7 @@ int main(void)
|
|||||||
// Let's have base as a base_t
|
// Let's have base as a base_t
|
||||||
M_LET(base, base_t) {
|
M_LET(base, base_t) {
|
||||||
// Read the JSON file and fill-in base
|
// Read the JSON file and fill-in base
|
||||||
read(base, "ex-json01.json");
|
read(base, "ex11-json01.json");
|
||||||
// Print the contents of base:
|
// Print the contents of base:
|
||||||
printf ("List of presents:\n");
|
printf ("List of presents:\n");
|
||||||
for M_EACH(el, base, base_t) {
|
for M_EACH(el, base, base_t) {
|
||||||
@@ -61,10 +58,3 @@ int main(void)
|
|||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
File renamed without changes.
@@ -1,5 +1,3 @@
|
|||||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "m-array.h"
|
#include "m-array.h"
|
||||||
@@ -7,12 +5,15 @@
|
|||||||
#include "m-dict.h"
|
#include "m-dict.h"
|
||||||
#include "m-string.h"
|
#include "m-string.h"
|
||||||
|
|
||||||
|
// Let's define a tuple composed of (offset, value) and register it
|
||||||
TUPLE_DEF2(symbol, (offset, long), (value, long))
|
TUPLE_DEF2(symbol, (offset, long), (value, long))
|
||||||
#define M_OPL_symbol_t() TUPLE_OPLIST(symbol, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST)
|
#define M_OPL_symbol_t() TUPLE_OPLIST(symbol, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST)
|
||||||
|
|
||||||
|
// Let's define an array of such tuple and register it
|
||||||
ARRAY_DEF(array_symbol, symbol_t)
|
ARRAY_DEF(array_symbol, symbol_t)
|
||||||
#define M_OPL_array_symbol_t() ARRAY_OPLIST(array_symbol, M_OPL_symbol_t())
|
#define M_OPL_array_symbol_t() ARRAY_OPLIST(array_symbol, M_OPL_symbol_t())
|
||||||
|
|
||||||
|
// Let's define a dictionnary associating a string_t to such an array and register it
|
||||||
DICT_DEF2(sections, string_t, array_symbol_t)
|
DICT_DEF2(sections, string_t, array_symbol_t)
|
||||||
#define M_OPL_sections_t() DICT_OPLIST(sections, STRING_OPLIST, M_OPL_array_symbol_t())
|
#define M_OPL_sections_t() DICT_OPLIST(sections, STRING_OPLIST, M_OPL_array_symbol_t())
|
||||||
|
|
||||||
@@ -20,13 +21,21 @@ int main(int argc, const char *argv[])
|
|||||||
{
|
{
|
||||||
if (argc < 2) abort();
|
if (argc < 2) abort();
|
||||||
FILE *f = fopen(argv[1], "rt");
|
FILE *f = fopen(argv[1], "rt");
|
||||||
if (!f) abort();
|
if (!f) {
|
||||||
|
fprintf(stderr, "ERROR: Cannot open %s\n", argv[1]);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create & initialize sc as sections_t
|
||||||
M_LET(sc, sections_t) {
|
M_LET(sc, sections_t) {
|
||||||
|
// Read the section from the FILE
|
||||||
sections_in_str(sc, f);
|
sections_in_str(sc, f);
|
||||||
|
// Get the section named .text
|
||||||
array_symbol_t *a = sections_get(sc, STRING_CTE(".text"));
|
array_symbol_t *a = sections_get(sc, STRING_CTE(".text"));
|
||||||
if (a == NULL) {
|
if (a == NULL) {
|
||||||
printf("There is no .text section.");
|
printf("There is no .text section.");
|
||||||
} else {
|
} else {
|
||||||
|
// Print its content
|
||||||
printf("Section .text is :");
|
printf("Section .text is :");
|
||||||
array_symbol_out_str(stdout, *a);
|
array_symbol_out_str(stdout, *a);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@@ -34,7 +43,3 @@ int main(int argc, const char *argv[])
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
int main(void) { return 0; }
|
|
||||||
#endif
|
|
||||||
Reference in new issue
Block a user