Move files to submodules

master
teknomunk 1 year ago
parent 709d8e415e
commit a48a288782

@ -1 +1 @@
Subproject commit 67dd64fef873c6f3fa71cd30851ec1114d722ba7
Subproject commit da7cb6bc910db8935dddf69ad9b559fc6db05aa1

@ -1 +1 @@
Subproject commit 5c5b389508bddffd603124a6f6abfdfb934ce598
Subproject commit 6cf050b1f1a6f5062167b64c7ddb80a6cc5c2339

@ -1,26 +0,0 @@
#include "json/json.h"
#include "vector.h"
#include "json_vector.h"
bool vector_append_from_json( struct json_pull_parser* jpp, struct vector_vtable* vtable, bool (*T_new_from_json)( struct json_pull_parser*, void*, void*),
struct vector* v, void* extra )
{
int save = 0;
if( !json_pull_parser_begin_array( jpp, &save ) ) {
return false;
}
char tmp[ vtable->T_sizeof() ];
for(;;) {
if( json_pull_parser_end_array(jpp,&save) ) {
return true;
}
if( !T_new_from_json( jpp, extra, tmp ) ) {
printf( "Unable to read T from json\n" );
return false;
}
vector_push( v, vtable, tmp );
}
}

@ -1,11 +0,0 @@
#pragma once
#include <stdbool.h>
struct json_pull_parser;
struct vector_vtable;
struct vector;
bool vector_append_from_json( struct json_pull_parser* jpp,
struct vector_vtable* vtable, bool (*T_new_from_json)( struct json_pull_parser*, void*, void*), struct vector* v, void* extra
);

@ -1,86 +0,0 @@
#include "vector.h"
#include <stdlib.h>
#include <stdio.h>
struct vector* vector_new( struct vector_vtable* vtable )
{
struct vector* v = (struct vector*)malloc( sizeof(struct vector) );
if( !v ) {
return NULL;
}
v->size = 0;
v->data = vtable->slice_T_new( 16 );
return v;
}
void vector_release( struct vector* v, struct vector_vtable* vtable )
{
vtable->slice_T_release( v->data );
free(v);
}
void vector_push( struct vector* v, struct vector_vtable* vtable, void* item )
{
if( v->size + 1 > v->data.size ) {
struct slice new_data = vtable->slice_T_new( v->data.size + 16 );
char tmp[ vtable->T_sizeof() ];
for( int i = 0; i < v->size; ++i ) {
vtable->slice_T_get_index( v->data, i, tmp );
vtable->slice_T_set_index( new_data, i, tmp );
}
vtable->slice_T_release( v->data );
v->data = new_data;
}
vtable->slice_T_set_index( v->data, v->size, item );
v->size += 1;
}
uint32_t vector_size( struct vector* v, struct vector_vtable* vtable )
{
if( !v ) {
return 0;
}
return v->size;
}
void vector_get_index_unsafe( struct vector* v, struct vector_vtable* vtable, uint32_t idx, void* item )
{
vtable->slice_T_get_index( v->data, idx, item );
}
void vector_bubble_sort( struct vector* v, struct vector_vtable* vtable, bool (*compare)( void* a, void* b ) )
{
uint32_t size = vector_size( v, vtable );
uint32_t T_sizeof = vtable->T_sizeof();
char a[T_sizeof];
char b[T_sizeof];
while( size > 1 ) {
bool any_swaps = false;
for( int j = 0; j < size - 1; ++j ) {
vtable->slice_T_get_index( v->data, j, a );
vtable->slice_T_get_index( v->data, j+1, b );
if( !compare(a,b) ) {
any_swaps = true;
vtable->slice_T_set_index( v->data, j, b );
vtable->slice_T_set_index( v->data, j+1, a );
}
}
// Short circuit
if( !any_swaps ) {
return;
}
size -= 1;
}
}

@ -1,90 +0,0 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
struct slice
{
uint32_t size;
char* data;
};
struct vector_vtable
{
struct slice (*slice_T_new)( uint32_t size );
void (*slice_T_release)( struct slice s );
void (*slice_T_set_index)( struct slice s, uint32_t idx, void* ptr );
void (*slice_T_get_index)( struct slice s, uint32_t idx, void* ptr );
uint32_t (*T_sizeof)();
bool (*T_operator_equal)( void* a, void* b );
};
struct vector
{
uint32_t size;
struct slice data;
};
struct vector* vector_new( struct vector_vtable* vtable );
void vector_release( struct vector* v, struct vector_vtable* vtable );
void vector_push( struct vector* v, struct vector_vtable* vtable, void* item );
uint32_t vector_size( struct vector* v, struct vector_vtable* vtable );
void vector_get_index_unsafe( struct vector* v, struct vector_vtable* vtable, uint32_t idx, void* item );
// Comparison sort functions
// All these functions take a comparitor function that should return true if 'a' should appear after 'b' in the list
void vector_bubble_sort( struct vector* v, struct vector_vtable* vtable, bool (*compare)( void* a, void* b ) );
// Sorted Insertion functions
//void vector_insert_sorted( struct vector* v, struct vector_vtable* vtable, bool (*compare)( void* a, void* b ) );
#define _PASTE( a, b ) _PASTE2( a, b )
#define _PASTE2( a, b ) a ## b
#define VECTOR_FOREACH_WITH_INDEX( vec, vtable, item_type, item_name, index, ignore ) \
uint32_t _PASTE(limit,__LINE__) = vector_size( vec, vtable );\
for( int index = 0; index < _PASTE(limit,__LINE__); ++index ) { \
item_type item_name = 0; \
vector_get_index_unsafe( vec, vtable, index, &item_name );
#define VECTOR_FOREACH( vec, vtable, item_type, item_name, ignore ) \
VECTOR_FOREACH_WITH_INDEX( vec, vtable, item_type, item_name, _PASTE(i,__LINE__), ignore )
#define VECTOR_VTABLE( T, type ) \
static struct slice slice_ ## T ## _new( uint32_t size ) \
{ \
struct slice res = { \
.size = size, \
.data = malloc(sizeof(type) * size), \
}; \
return res; \
} \
static void slice_ ## T ## _release( struct slice s ) \
{ \
free(s.data); \
} \
static void slice_ ## T ## _set_index( struct slice s, uint32_t index, void* ptr ) \
{ \
type* data = (type*)s.data; \
data[index] = *(type*)ptr; \
} \
static void slice_ ## T ## _get_index( struct slice s, uint32_t index, void* ptr ) \
{ \
type* data = (type*)s.data; \
*(type*)ptr = data[index]; \
} \
static uint32_t T ## _sizeof() \
{ \
return sizeof(struct chapter*); \
} \
struct vector_vtable vector_ ## T ## _vtable = { \
.slice_T_new = slice_## T ##_new, \
.slice_T_release = slice_## T ##_release, \
.slice_T_set_index = slice_## T ## _set_index, \
.slice_T_get_index = slice_## T ## _get_index, \
.T_sizeof = T ## _sizeof, \
.T_operator_equal = T ##_operator_equal, \
}
Loading…
Cancel
Save