node_t* huffman(frequency_t *__frequency, uint8_t bound) { int i; /* index */ char *aux = (char*) malloc (sizeof(char) * (MAXSIZE + 1)); /* intermediare sample */ node_t *x, *y; /* min frequencies from queue */ node_t *z; /* inserted node */ queue_t *queue = queue_create(); /* queue consists of the leaf elements of the tree */ /* * In this step, we insert in the queue every frequency element. The sample element * is /SAMPLE/\0 format. Is our standard to store samples in the tree. In the next * step it will be usefull to create intermediate nodes in the tree (sum of frequencies * in the huffman tree). */ for (i = 0; i <= bound; i++) { if (__frequency[i] > 0) { memset(aux, '\0', sizeof(char) * (MAXSIZE + 1)); sprintf(aux, "/%d/", i); z = create_node_tree(); z->sample = (char*) malloc (sizeof(char) * MAX_LENGHT_SAMPLE); memset(z->sample, '\0', sizeof(char) * MAX_LENGHT_SAMPLE); strncpy(z->sample, aux, strlen(aux)); z->frequency = __frequency[i]; insert_node_queue(&queue, z); } } /* * construct the huffman tree bottom up. Get two min frequency elements from queue, * concatenate to create a sub-root element and points to the two min frequency. * Insert the node (sub-root tree node) in the queue and repeate the process until * the last element of the queue. */ while (queue->count > 1) { memset(aux, '\0', sizeof(char) * (MAXSIZE + 1)); x = get_last_element_from_queue(&queue); y = get_last_element_from_queue(&queue); strcpy(aux, x->sample); strcat(aux, y->sample); z = create_node_tree(); z->sample = (char*) malloc (sizeof(char) * (strlen(aux) + 1)); memset(z->sample, '\0', sizeof(char) * (strlen(aux) + 1)); strncpy(z->sample, aux, strlen(aux)); z->frequency = x->frequency + y->frequency; z->left = x; z->right = y; insert_node_queue(&queue, z); } /*free(aux);*/ /* * last element of a queue is the last element inserted. I.e., the root element of * the huffman tree */ return get_last_element_from_queue(&queue); }
void node_grok_config(struct cfg_comp *config) { uint i; int node_i = 0; static merlin_node *table = NULL; if (!config) return; /* * We won't have more nodes than there are compounds, so we * happily waste a bit to make up for the other valid compounds * so we can keep nodes linear in memory */ if (table) free(table); table = calloc(config->nested, sizeof(merlin_node)); for (i = 0; i < config->nested; i++) { struct cfg_comp *c = config->nest[i]; merlin_node *node; if (!prefixcmp(c->name, "module") || !prefixcmp(c->name, "test")) continue; if (!prefixcmp(c->name, "daemon")) continue; node = &table[node_i++]; memset(node, 0, sizeof(*node)); node->sock = -1; node->name = next_word((char *)c->name); if (!prefixcmp(c->name, "poller") || !prefixcmp(c->name, "slave")) { node->type = MODE_POLLER; node->flags = MERLIN_NODE_DEFAULT_POLLER_FLAGS; grok_node(c, node); } else if (!prefixcmp(c->name, "peer")) { node->type = MODE_PEER; node->flags = MERLIN_NODE_DEFAULT_PEER_FLAGS; grok_node(c, node); } else if (!prefixcmp(c->name, "noc") || !prefixcmp(c->name, "master")) { node->type = MODE_NOC; node->flags = MERLIN_NODE_DEFAULT_MASTER_FLAGS; grok_node(c, node); } else cfg_error(c, NULL, "Unknown compound type\n"); if (node->name) node->name = strdup(node->name); else node->name = strdup(inet_ntoa(node->sain.sin_addr)); node->sock = -1; memset(&node->info, 0, sizeof(node->info)); } create_node_tree(table, node_i); }