GhtErr ght_node_free(GhtNode *node) { int i; const int deep = 1; assert(node != NULL); if ( node->attributes ) GHT_TRY(ght_attribute_free(node->attributes)); if ( node->children ) GHT_TRY(ght_nodelist_free_deep(node->children)); if ( node->hash ) GHT_TRY(ght_hash_free(node->hash)); ght_free(node); return GHT_OK; }
/* Recursively build a nodelist from a tree of GhtNodes */ GhtErr ght_node_to_nodelist(const GhtNode *node, GhtNodeList *nodelist, GhtAttribute *attr, GhtHash *hash) { static int hash_array_len = GHT_MAX_HASH_LENGTH+1; GhtHash h[hash_array_len]; GhtAttribute *a; /* Add our part of the hash to the incoming part */ memset(h, 0, hash_array_len); strncpy(h, hash, GHT_MAX_HASH_LENGTH); if ( node->hash ) { /* TODO: limit write to GHT_MAX_HASH_LENGTH - strlen(node->hash) */ strcat(h, node->hash); } /* Make a copy of all the incoming attributes */ GHT_TRY(ght_attribute_union(node->attributes, attr, &a)); /* Recurse down to leaf nodes, copying attributes and passing them down */ if ( node->children && node->children->num_nodes > 0 ) { int i; for ( i = 0; i < node->children->num_nodes; i++ ) { GHT_TRY(ght_node_to_nodelist(node->children->nodes[i], nodelist, a, h)); } ght_attribute_free(a); } /* This is a leaf node, create a new node and add to list */ else { GhtNode *n; GHT_TRY(ght_node_new_from_hash(h, &n)); GHT_TRY(ght_node_add_attribute(n, a)); GHT_TRY(ght_nodelist_add_node(nodelist, n)); } return GHT_OK; }
//return ght_node_calculate_z(tree->root); GhtErr ght_node_calculate_z(const GhtNode *node, GhtAttribute *attr, GhtSchema *schema) { static int hash_array_len = GHT_MAX_HASH_LENGTH + 1; GhtHash h[hash_array_len]; GhtAttribute *a; // TODO Vérifier l'access au hash de chaque noeud /* Make a copy of all the incoming attributes */ GHT_TRY(ght_attribute_union(node->attributes, attr, &a)); /* Recurse down to leaf nodes, copying attributes and passing them down */ if ( node->children && node->children->num_nodes > 0 ) { int i; for ( i = 0; i < node->children->num_nodes; i++ ) { GHT_TRY(ght_node_calculate_z(node->children->nodes[i], a, schema )); } double acc = 0; int k; for ( k = 0; k < node->children->num_nodes; k++ ) { acc = node->children->nodes[k]->z_avg + acc; } GHT_TRY(ght_node_set_z_avg( node, acc / node->children->num_nodes )); ght_attribute_free(a); } /* This is a leaf node, create a new node and add to list */ else { // printf (">> Dans une feuille \n"); double valeur; // *** DEBUT *** // TODO On a besoin d'une fonction plus géneral // Pour l'instant on va le tester avec la dimension "elevation" = Z GhtDimension *dim; GhtAttribute found; /* int num_dims; GHT_TRY( ght_schema_get_num_dimensions( schema, &num_dims) ); printf ("> num dimension %i \n ", num_dims); */ //GhtErr ght_schema_get_dimension_by_name(const GhtSchema *schema, const char *name, GhtDimension **dim) GHT_TRY( ght_schema_get_dimension_by_name(schema, "Z", &dim) ); /* const char *name_dim ; ght_dimension_get_name(dim,&name_dim); printf("> Name of dimension: "); puts(name_dim); */ //GhtErr ght_attribute_get_by_dimension(const GhtAttribute *attr, const GhtDimension *dim, GhtAttribute *found) GHT_TRY( ght_attribute_get_by_dimension(a, dim, &found) ); //GhtErr ght_attribute_get_value(const GhtAttribute *attr, double *val) GHT_TRY( ght_attribute_get_value(&found, &valeur) ); //printf ("> VALEUR %g \n ", valeur); GHT_TRY( ght_node_set_z_avg(node, valeur) ); // *** FIN *** /* GhtNode *n; GHT_TRY(ght_node_new_from_hash(h, &n)); GHT_TRY(ght_node_add_attribute(n, a)); GHT_TRY(ght_nodelist_add_node(nodelist, n)); */ /* double d; GHT_TRY(ght_attribute_get_value(attr, &d)); ght_attribute_get_value */ } return GHT_OK; }