Add initial stuff

This commit is contained in:
Patrick Pelissier committed 2017-02-18 18:51:04 +01:00
1 parent 2d8bda0fe0
commit 8e9d20ba1a
45 files changed
+8901

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
CC=gcc -std=c99
CFLAGS=-Wall -W -pedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wswitch-default -Wswitch-enum -Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wunreachable-code -Wlogical-op -Wstrict-aliasing=2 -Wredundant-decls -Wold-style-definition -Wno-unused-function
CPPFLAGS=-I..
RM=rm -f
EXE=./a.exe
EXAMPLES=ex01b.c ex01.c ex02.c ex03.c ex04.c ex05.c ex06.c ex07.c
all:
@echo "Nothing to be done."
check: examples
examples:
for f in $(EXAMPLES) ; do $(CC) $(CFLAGS) $(CPPFLAGS) -g $$f -o $(EXE) -pthread ; done
clean:
$(RM) $(EXE) *.s *~ *.o ./a.dat
$(RM) -r coverage *.gcda *.gcno all.info test.tar README.html
+60
View File
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2017, Patrick Pelissier
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* + Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "m-array.h"
ARRAY_DEF (charp, char *)
int main(void)
{
// Create an array list
array_charp_t al;
array_charp_init(al);
// add elements to the array list
array_charp_push_back (al,"C");
array_charp_push_back (al,"A");
array_charp_push_back (al,"E");
array_charp_push_back (al,"B");
array_charp_push_back (al,"D");
array_charp_push_back (al,"F");
// Use iterator to display contents of al
printf("Original contents of al: ");
array_it_charp_t itr;
for (array_charp_it(itr, al) ; !array_charp_end_p (itr); array_charp_next(itr)) {
char *element = *array_charp_ref(itr);
printf ("%s ", element);
}
printf("\n");
// Now, display the list backwards
printf("List backwards: ");
for (array_charp_previous (itr) ; !array_charp_end_p (itr); array_charp_previous(itr)) {
char *element = *array_charp_ref(itr);
printf ("%s ", element);
}
printf("\n");
array_charp_clear(al);
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2017, Patrick Pelissier
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* + Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "m-array.h"
ARRAY_DEF (charp, char *)
int main(void)
{
// Create an array list
M_LET (al, ARRAY_OPLIST(charp)) {
// add elements to the array list
array_charp_push_back (al,"C");
array_charp_push_back (al,"A");
array_charp_push_back (al,"E");
array_charp_push_back (al,"B");
array_charp_push_back (al,"D");
array_charp_push_back (al,"F");
// Display contents of al
printf("Original contents of al: ");
for M_EACH(element, al, ARRAY_OPLIST(charp)) {
printf ("%s ", *element);
}
printf("\n");
}
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
#include <stdlib.h>
#include "m-array.h"
ARRAY_DEF(int, int)
const int n = 100000;
int main(void)
{
array_int_t x;
array_int_init(x);
for (int i = 0; i < n; i++) {
array_int_push_back(x, rand());
}
for (int i = 0; i < n; i++) {
array_int_push_at (x, rand() % n, rand());
}
for (int i = 0; i < n; i++) {
array_int_pop_at (NULL, x, rand() % n);
}
array_int_clear(x);
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "m-dict.h"
#include "m-string.h"
DICT_DEF2(str, string_t, STRING_OPLIST, int, M_DEFAULT_OPLIST)
int main(void)
{
dict_str_t h;
int max = 1;
string_t key;
dict_str_init(h);
string_init (key);
while (!feof(stdin))
{
string_fgets (key, stdin, STRING_READ_PURE_LINE);
int *v = dict_str_get (h, key);
if (v != NULL) {
(*v)++;
max = M_MAX(*v, max);
} else {
dict_str_set_at(h, key, 1);
}
}
string_clear(key);
dict_str_clear(h);
return 0;
}
+164
View File
@@ -0,0 +1,164 @@
#include "m-buffer.h"
#include "m-shared.h"
#include "m-mutex.h"
/**************************************************************/
/* Define a random big data structure */
#define BIGDATA_SIZE 100000
#define BIGDATA_NUM 10
typedef struct bigdata_s {
char bigdata[BIGDATA_SIZE];
char *ptr;
} bigdata_t[1];
static void bigdata_init(bigdata_t d)
{
memset(d->bigdata, 0, BIGDATA_SIZE);
d->ptr = malloc (BIGDATA_SIZE * 10);
}
static void bigdata_clear(bigdata_t d)
{
free(d->ptr);
}
static bigdata_t *bigdata_new (void)
{
bigdata_t *n = malloc (sizeof (bigdata_t));
bigdata_init(*n);
return n;
}
static void bigdata_free(bigdata_t *d)
{
bigdata_clear(*d);
free(d);
}
/**************************************************************/
/* Perform some works on this big data */
static void perform_acquisition(bigdata_t *p)
{
// TODO
}
static void perform_computation2(bigdata_t *p)
{
// TODO
}
static void perform_computation3(bigdata_t *p)
{
// TODO
}
static void perform_computation4(bigdata_t *p)
{
// TODO
}
/**************************************************************/
/* Let's do the thread scheduling */
#define MY_QUEUE_SIZE 10
SHARED_PTR_DEF(bigdata, bigdata_t, (CLEAR(bigdata_clear), FREE(free)))
BUFFER_DEF(bigdata, shared_bigdata_t, MY_QUEUE_SIZE,
BUFFER_QUEUE|BUFFER_PUSH_INIT_POP_MOVE, SHARED_PTR_OPLIST(bigdata))
/* Thread synchros:
* Thread1 ---> Thread 2
* +-> Thread 3 --> Thread 4
*/
buffer_bigdata_t buf_t1tot2;
buffer_bigdata_t buf_t1tot3;
buffer_bigdata_t buf_t3tot4;
bool continue_threading;
M_THREAD_T t1, t2, t3, t4;
static void initbuf(void)
{
buffer_bigdata_init(buf_t1tot2, MY_QUEUE_SIZE);
buffer_bigdata_init(buf_t1tot3, MY_QUEUE_SIZE);
buffer_bigdata_init(buf_t3tot4, MY_QUEUE_SIZE);
}
static void clearbuf(void)
{
buffer_bigdata_clear(buf_t1tot2);
buffer_bigdata_clear(buf_t1tot3);
buffer_bigdata_clear(buf_t3tot4);
}
static void thread1 (void *arg)
{
while (continue_threading) {
shared_bigdata_t ptr;
shared_bigdata_init2 (ptr, bigdata_new());
perform_acquisition(shared_bigdata_ref(ptr));
buffer_bigdata_push(buf_t1tot2, ptr);
buffer_bigdata_push(buf_t1tot3, ptr);
shared_bigdata_clear(ptr);
}
}
static void thread2 (void *arg)
{
while (continue_threading) {
shared_bigdata_t ptr;
shared_bigdata_init (ptr);
buffer_bigdata_pop(&ptr, buf_t1tot2);
perform_computation2(shared_bigdata_ref(ptr));
shared_bigdata_clear(ptr);
}
}
static void thread3 (void *arg)
{
while (continue_threading) {
shared_bigdata_t ptr;
shared_bigdata_init (ptr);
buffer_bigdata_pop(&ptr, buf_t1tot3);
perform_computation3(shared_bigdata_ref(ptr));
buffer_bigdata_push(buf_t3tot4, ptr);
shared_bigdata_clear(ptr);
}
}
static void thread4 (void *arg)
{
while (continue_threading) {
shared_bigdata_t ptr;
shared_bigdata_init (ptr);
buffer_bigdata_pop(&ptr, buf_t3tot4);
perform_computation4(shared_bigdata_ref(ptr));
shared_bigdata_clear(ptr);
}
}
static void init(void)
{
initbuf();
continue_threading = true;
M_THREAD_CREATE(t1, thread1, NULL);
M_THREAD_CREATE(t2, thread2, NULL);
M_THREAD_CREATE(t3, thread3, NULL);
M_THREAD_CREATE(t3, thread4, NULL);
}
static void clear(void)
{
continue_threading = false;
M_THREAD_JOIN(t1);
M_THREAD_JOIN(t2);
M_THREAD_JOIN(t3);
M_THREAD_JOIN(t4);
clearbuf();
}
int main(void)
{
init();
//
clear();
}
+120
View File
@@ -0,0 +1,120 @@
/* Based on:
https://github.com/kostya/benchmarks/blob/master/brainfuck/brainfuck.cpp */
#include <stdio.h>
#include "m-array.h"
#include "m-string.h"
ARRAY_DEF(int, int)
typedef struct {
int pos;
array_int_t tape;
} tape_t[1];
static void tape_init(tape_t tape) {
tape->pos =0;
array_int_init(tape->tape);
array_int_push_back(tape->tape, 0);
}
static void tape_clear(tape_t tape) { array_int_clear(tape->tape); }
static int tape_get(tape_t tape) { return *array_int_cget(tape->tape, tape->pos); }
static void tape_inc(tape_t tape) { (*array_int_get(tape->tape, tape->pos)) ++; }
static void tape_dec(tape_t tape) { (*array_int_get(tape->tape, tape->pos)) --; }
static void tape_advance(tape_t tape) { tape->pos++; if (array_int_size(tape->tape) <= (unsigned int) tape->pos) array_int_push_back(tape->tape, 0); }
static void tape_devance(tape_t tape) { if (tape->pos > 0) tape->pos--; }
typedef struct {
string_t code;
array_int_t bracket_map; // Should use map when possible.
} program_t[1];
static void program_init(program_t p, string_t text) {
string_init (p->code);
array_int_init (p->bracket_map);
array_int_t leftstack;
string_t symbols;
array_int_init(leftstack);
string_init_set_str(symbols, "[]<>+-,.");
int pc = 0;
for (size_t i = 0; i < string_get_length(text); i++) {
char c = string_get_char(text, i);
if (string_search_char(symbols, c) == STRING_FAILURE) continue;
if (c == '[') array_int_push_back (leftstack, pc);
else
if (c == ']' && array_int_size (leftstack) != 0) {
int left;
array_int_pop_back(&left, leftstack);
int right = pc;
array_int_set_at2 (p->bracket_map, left, right);
array_int_set_at2 (p->bracket_map, right, left);
}
pc++;
string_push_back(p->code, c);
}
array_int_clear(leftstack);
string_clear(symbols);
}
static void program_clear(program_t p) {
string_clear(p->code);
array_int_clear(p->bracket_map);
}
static void program_run(program_t p){
tape_t tape;
tape_init(tape);
for(size_t pc = 0; pc < string_get_length(p->code); pc++) {
switch (string_get_char(p->code, pc)) {
case '+':
tape_inc(tape);
break;
case '-':
tape_dec(tape);
break;
case '>':
tape_advance(tape);
break;
case '<':
tape_devance(tape);
break;
case '[':
if (tape_get(tape) == 0) pc = *array_int_cget(p->bracket_map, pc);
break;
case ']':
if (tape_get(tape) != 0) pc = *array_int_cget(p->bracket_map, pc);
break;
case '.':
printf("%c", tape_get(tape));
fflush(stdout);
break;
default:
abort();
break;
}
}
tape_clear(tape);
}
static void read_file(string_t str, const string_t filename) {
FILE *f = fopen(string_get_cstr(filename), "rt");
if (!f) abort();
string_fgets (str, f, STRING_READ_FILE);
fclose(f);
}
int main(int argc, const char*argv[])
{
string_t filename, text;
assert (argc >= 2);
string_init_set_str(filename, argv[1]);
string_init(text);
read_file(text, filename);
program_t p;
program_init(p, text);
program_run(p);
program_clear(p);
string_clear(text);
string_clear(filename);
return 0;
}
+63
View File
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "m-list.h"
#include "m-string.h"
#include "m-algo.h"
LIST_DEF(string, string_t, STRING_OPLIST)
#define ASTR_OPLIST LIST_OPLIST(string, STRING_OPLIST)
ALGO_DEF(astring, ASTR_OPLIST)
int main(void)
{
// Let's create a variable named 'grow' which is a list of string_t
M_LET (grow, ASTR_OPLIST)
M_LET (tmpstr, STRING_OPLIST) { // And let's create a string_t
// add string elements to the array
list_string_push_back (grow, STRING_CTE("AB"));
string_printf(tmpstr, "%d", 12);
list_string_push_back (grow, tmpstr);
list_string_push_back (grow, STRING_CTE("CD"));
// get the chunk as a big string
list_string_get_str (tmpstr, grow, false);
// print out
printf("Number of elements = %lu\n", list_string_size(grow));
printf("Final string = %s\n", string_get_cstr(tmpstr));
// Another way to print the list
for M_EACH (item, grow, ASTR_OPLIST) {
printf ("Item = ");
string_fputs (stdout, *item);
printf ("\n");
}
// Get the min and the max of the list
string_t *p = astring_min(grow);
printf ("Min string is %s\n", string_get_cstr(*p));
p = astring_max(grow);
printf ("Max string is %s\n", string_get_cstr(*p));
// We can even sort the list!
astring_sort(grow);
for M_EACH (item, grow, ASTR_OPLIST) {
printf ("Sort Item = %s\n", string_get_cstr(*item) );
}
// Split a string into a list.
string_set_str(tmpstr, "HELLO;JOHN;HOW;ARE;YOU");
astring_split(grow, tmpstr, ';');
for M_EACH (item, grow, ASTR_OPLIST) {
printf ("Split Item = %s\n", string_get_cstr(*item) );
}
} // Everything is cleared after this point.
return 0;
}
+44
View File
@@ -0,0 +1,44 @@
#if 1 // Non macro version.
#include "m-dict.h"
DICT_DEF2(m32, unsigned int, M_DEFAULT_OPLIST, char, M_DEFAULT_OPLIST)
int main(void) {
dict_m32_t h;
dict_m32_init(h); // h is init.
dict_m32_set_at (h, 5, 10); // h[5] = 10
char *k = dict_m32_get(h, 10); // k == NULL
int is_missing = (k != NULL); // true
assert (is_missing);
dict_m32_remove(h, 5); // h is now empty
dict_it_m32_t it; // iterate over all dictionnary
for (dict_m32_it (it, h) ; !dict_m32_end_p (it); dict_m32_next(it)) {
dict_pair_m32_t *item = dict_m32_ref(it);
(*item)->value = 1; // Set its value to 1
}
dict_m32_clear(h); // h is cleared
return 0;
}
#else // Use of M_LET & M_FOR macros (otherwise equivalent)
#include "m-dict.h"
DICT_DEF2(m32, unsigned int, M_DEFAULT_OPLIST, char, M_DEFAULT_OPLIST)
#define M32_OPLIST DICT_OPLIST(m32, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST)
int main(void) {
M_LET(h, M32_OPLIST) { // h is init
dict_m32_set_at (h, 5, 10); // h[5] = 10
char *k = dict_m32_get(h, 10); // k == NULL
int is_missing = (k != NULL); // true
assert (is_missing);
dict_m32_remove(h, 5); // h is now empty
for M_EACH(item, h, M32_OPLIST) { // traverse each item
(*item)->value = 1; // Set its value to 1
}
} // h is cleared
return 0;
}
#endif