diff options
author | Nicolas Dandrimont <nicolas.dandrimont@gmail.com> | 2007-11-24 22:50:14 +0100 |
---|---|---|
committer | Guillaume Seguin <guillaume@segu.in> | 2007-11-24 23:21:08 +0100 |
commit | ca04edcccfdddfca7b2951ec895c86356b4f53a6 (patch) | |
tree | ad66a72c2b5b5e6754baab1e42f1c3c7c20a8cbf | |
parent | 0db54823cdd8874082f0cddaaadfcceb35f2915e (diff) | |
download | gmathlib-ca04edcccfdddfca7b2951ec895c86356b4f53a6.tar.gz gmathlib-ca04edcccfdddfca7b2951ec895c86356b4f53a6.tar.bz2 |
Introducing a new "array" symbol, placeholder for a new GMathArray object, allowing creation of equation arrays and matrices.
-rw-r--r-- | TEMPLATES | 8 | ||||
-rw-r--r-- | core/gmathlib.c | 43 | ||||
-rw-r--r-- | core/gmathtemplates.c | 1 | ||||
-rw-r--r-- | include/gmathlib.h | 19 |
4 files changed, 66 insertions, 5 deletions
@@ -119,3 +119,11 @@ Template description template: o 0: left parameter o 1: right parameter * Examples: x -> 0, y -> +∞ +- array: + * A placeholder symbol for an array object + * Value: no + * Children: 1: + o 0: a GMathArray object + * Example: no + + diff --git a/core/gmathlib.c b/core/gmathlib.c index 879f999..96bbfa8 100644 --- a/core/gmathlib.c +++ b/core/gmathlib.c @@ -252,7 +252,7 @@ gmathsymbol_new (GMathContext *context, gchar *type) symbol->type = g_strdup (type); symbol->value = NULL; - symbol->children = g_new0 (GMathSymbol*, template->children_count); + symbol->children = (void**)g_new0 (GMathSymbol*, template->children_count); symbol->parent = NULL; @@ -262,17 +262,54 @@ gmathsymbol_new (GMathContext *context, gchar *type) return symbol; } +/* Create a new array, contents are given line after line */ +GMathSymbol* +gmathsymbol_new_array (GMathContext *context, guint hsize, guint vsize, + GMathSymbol **contents) +{ + GMathSymbol *symbol; + GMathArray *array; + + symbol = gmathsymbol_new (context, "array"); + gmathsymbol_set_value (symbol, ""); + + array = g_new (GMathArray, 1); + symbol->children = g_new0 (void*, 1); + + symbol->children[0] = (void*)array; + + array->hsize = hsize; + array->vsize = vsize; + + array->children = contents; + + return symbol; +} + /* Free a symbol and its children */ void gmathsymbol_free (GMathSymbol *symbol) { int i; + int arraysize; + GMathArray* array; if (!symbol) return; - for (i = 0; i < symbol->template->children_count; i++) - gmathsymbol_free (symbol->children[i]); + if(strcmp (symbol->type, "array") == 0) + { + array = (GMathArray*)symbol->children[0]; + arraysize = array->hsize * array->vsize; + for(i = 0; i < arraysize; i++) + gmathsymbol_free (array->children[i]); + g_free (array); + } + else + { + for (i = 0; i < symbol->template->children_count; i++) + gmathsymbol_free (symbol->children[i]); + } g_free (symbol->type); g_free (symbol->value); diff --git a/core/gmathtemplates.c b/core/gmathtemplates.c index 8e48641..c304c87 100644 --- a/core/gmathtemplates.c +++ b/core/gmathtemplates.c @@ -47,4 +47,5 @@ gmath_register_templates (GMathContext *context) gmathcontext_register_template (context, "lim", false, 3); /* lim */ gmathcontext_register_template (context, "infty", false, 0); /* ∞ */ gmathcontext_register_template (context, "approach", false, 2); /* a -> b */ + gmathcontext_register_template (context, "array", false, 1); /* placeholder */ } diff --git a/include/gmathlib.h b/include/gmathlib.h index cd17ece..c7a2578 100644 --- a/include/gmathlib.h +++ b/include/gmathlib.h @@ -41,6 +41,7 @@ typedef struct _GMathModule GMathModule; typedef struct _GMathTemplate GMathTemplate; typedef struct _GMathFormula GMathFormula; typedef struct _GMathSymbol GMathSymbol; +typedef struct _GMathArray GMathArray; typedef GMathFormula* (*GMathInputProc) (GMathContext *context, void *data); typedef size_t (*GMathOutputProc) (GMathFormula *formula, gchar **data); @@ -77,15 +78,25 @@ struct _GMathSymbol gchar *type; /* Type identifier ("atom", "sum" ...) */ gchar *value; /* Value, as string (if any) */ - GMathSymbol **children; /* Child symbols array */ + void **children; /* Child symbols array */ - GMathSymbol *parent; /* Parent symbol - should prevent + void *parent; /* Parent symbol - should prevent double-frees */ GMathTemplate *template; /* Symbol's template */ GMathContext *context; /* Parent context */ }; +struct _GMathArray +{ + guint hsize; /* Horizontal size */ + guint vsize; /* Vertical size */ + + GMathSymbol **children; /* Child symbols array */ + + GMathSymbol *parent; /* Parent symbol */ +}; + /* Available methods */ /* GMathContext methods */ @@ -131,6 +142,10 @@ gmathformula_export (GMathFormula *formula, gchar *module, gchar **data); GMathSymbol* gmathsymbol_new (GMathContext *context, gchar *type); +GMathSymbol* +gmathsymbol_new_array (GMathContext *context, guint hsize, guint vsize, + GMathSymbol **contents); + void gmathsymbol_free (GMathSymbol *symbol); |