mcSTdatasetCreate — Create a sparse-table dataset
#include <memcom.h> int mcSTdatasetCreate(int handle, const char* name);
mcSTdatasetCreate
creates the
sparse-table dataset name
on the database
identified by handle
. Initially, the table
contains zero rows and columns.
handle
Database handle (input).
name
Name of a new dataset defining the sparse-table (input).
If completed successfully, the function returns 0. Otherwise, a negative value indicating the MemCom error number is returned.
/* * Example code how to write a sparse-table dataset. * * The important part is the create_elements_dataset() function. The * rest is just there to make to code work. The code does not contain * any error checking for memory allocation. */ #include <assert.h> #include <stdlib.h> #include <memcom.h> /* * A simple structure for Finite Elements. The structs are linked * together, making for a simple list data structure. You can use * any other sequential data structure instead. */ struct element { int eid; /* Element identifier. */ int etype; /* Element type identifier. */ int pid; /* Property identifier. */ int num_nodes; /* Number of nodes for this element. */ int* nodes; /* Connectivity, variable size. */ struct element* next; /* Pointer to next element or NULL if none. */ }; /* * Creates and writes a sparse-table dataset. The sequential (row-by-row) * fashion ensures optimal performance. */ void create_elements_dataset(int handle, const char* datasetname, const struct element* first_element) { const struct element* element; mcInt64 dsid; /* * Create the sparse-table dataset. We don't have * to know the number of rows nor the number of columns at the * time of its creation. */ mcSTdatasetCreate(handle, datasetname); /* Open the sparse-table dataset. */ dsid = mcSTdatasetOpen(handle, datasetname); /* * Append columns. The column indices are in the order in * which the columns are appended: 1, 2, 3... */ mcSTcolAppend(dsid, "EID", "I", 1); mcSTcolAppend(dsid, "ETYPE", "I", 1); mcSTcolAppend(dsid, "PID", "I", 1); mcSTcolAppend(dsid, "NODES", "I", 0); /* variable-sized. */ /* Loop over the linked list of elements. */ for (element = first_element; element != NULL; element = element->next) { mcInt64 row_index; /* Append a single empty row. */ row_index = mcSTrowAppend(dsid, 1); /* Define cell data for the new row. */ mcSTcellSet(dsid, row_index, 1, 1, &element->eid); mcSTcellSet(dsid, row_index, 2, 1, &element->etype); mcSTcellSet(dsid, row_index, 3, 1, &element->pid); mcSTcellSet(dsid, row_index, 4, element->num_nodes, element->nodes); } mcSTdatasetClose(dsid); } /* * Initializes the linked list of element structures. The element * connectivities are quite meaningless; this is just an example. * One could use C macros for the management of the linked list or, * (in C++) use the STL std::list to make the code less verbose. */ struct element* initialize_elements(mcInt64 num_nodes) { mcInt64 i; int j; struct element* first_element; struct element* last_element; struct element* element; int ne; first_element = last_element = NULL; ne = 0; /* Create some point elements. */ for (i = 0; i < num_nodes; ++i, last_element = element) { element = malloc(sizeof(struct element)); element->eid = ++ne; element->etype = 1; element->pid = 1; element->num_nodes = 1; element->nodes = malloc(sizeof(int) * element->num_nodes); element->nodes[0] = 1 + i; /* Append to linked list. */ element->next = NULL; if (last_element == NULL) first_element = element; else last_element->next = element; } /* Create some rod (line) elements. */ for (i = 0; i < num_nodes - 1; ++i, last_element = element) { element = malloc(sizeof(struct element)); element->eid = ++ne; element->etype = 2; element->pid = 2; element->num_nodes = 2; element->nodes = malloc(sizeof(int) * element->num_nodes); for (j = 0; j < element->num_nodes; j++) element->nodes[j] = 1 + i + j; /* Append to linked list. */ element->next = NULL; if (last_element == NULL) first_element = element; else last_element->next = element; } return first_element; } /* * Clear the linked list of elements. */ void clear_elements(struct element* first_element) { struct element* element; for (element = first_element; element != NULL; ) { struct element* e; e = element; element = element->next; free(e->nodes); free(e); } } int main() { const char* databasename = "st-example.mc"; const char* datasetname = "ETAB.1"; const int num_nodes = 100000; int handle; struct element* elements; /* Open a new MemCom database. */ handle = mcDBopenFile(databasename, "n"); /* Create in-memory element data. */ elements = initialize_elements(num_nodes); /* Create the sparse-table dataset. */ create_elements_dataset(handle, datasetname, elements); /* Delete in-memory element data. */ clear_elements(elements); /* Close database. All in-memory data will be flushed. */ mcDBcloseFile(handle); return 0; }