Tables

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <stdint.h> #include <stdbool.h> #include <stdlib.h> #include <assert.h> typedef enum { QUERY_UNKNOWN, QUERY_SELECT, QUERY_WHERE, QUERY_ANY, QUERY_COUNT, } Query_Tag; typedef struct { size_t col_count; size_t *col_strides; size_t row_count; size_t row_stride; void *data; } Table_Header; typedef Table_Header *Table; typedef struct { Query_Tag tag; union { void *(*select)(void *); void *(*where)(void *); bool (*any)(void *); intptr_t (*count)(); }; } Query_Cond; typedef struct { size_t count; Query_Cond *e; } Query_Header; typedef Query_Header *Query; Table table_init(size_t row_count, size_t col_count) { void *data = calloc(row_count * col_count * 8, 1); assert(data); size_t *col_strides = (size_t *)calloc(col_count * 8, 1); assert(col_strides); for(size_t i = 0; i < col_count; ++i) { col_strides[i] = i * 8; } Table table = (Table)calloc(sizeof(Table_Header), 1); *table = (Table_Header){ .col_count = col_count, .col_strides = col_strides, .row_count = row_count, .row_stride = col_count * 8, .data = data, }; return table; } void *get_cell(Table table, size_t r, size_t c) { assert(r < table->row_count); assert(c < table->col_count); size_t row_i = r * table->row_stride; size_t col_i = table->col_strides[c]; uint8_t *cell = (uint8_t *)table->data + row_i + col_i; return cell; } void *query(Table table, Query query) { if (table == 0 || query == 0) { return 0; } for(size_t i = 0; i < query->count; ++i) { switch(query->e[i].tag) { case QUERY_UNKNOWN:break; case QUERY_SELECT:break; case QUERY_WHERE:break; case QUERY_ANY:break; case QUERY_COUNT:break; default:break; } } return 0; } int main(int argc, char **argv) { Table table = table_init(4, 20); size_t *cell0 = (size_t *)get_cell(table, 0, 0); assert(table->data == cell0); size_t *cell1 = (size_t *)get_cell(table, 3, 15); *cell1 = 15; size_t *cell2 = (size_t *)get_cell(table, 3, 15); printf("%zu\n", *cell2); void *row = query(table, 0); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines