bool transaction_remove_listener(Transaction *t, char cmd, void *listener) { list_node_t *prev, *victim; assert((cmd >= 'a') && (cmd <= 'z')); prev = NULL; victim = t->listeners[CHAR_TO_INDEX(cmd)]; // find the victim while (victim) { if (victim->value == listener) { break; } prev = victim; victim = victim->next; } if (victim) { // victim found if (prev) { prev->next = victim->next; } else { t->listeners[CHAR_TO_INDEX(cmd)] = victim->next; } free(victim); return true; } return false; }
void transaction_push_listener(Transaction *t, char cmd, void *listener) { list_node_t *n; assert((cmd >= 'a') && (cmd <= 'z')); n = malloc(sizeof(list_node_t)); n->value = listener; n->next = t->listeners[CHAR_TO_INDEX(cmd)]; t->listeners[CHAR_TO_INDEX(cmd)] = n; }
void *transaction_pop_listener(Transaction *t, char cmd) { list_node_t *n; void *value; assert((cmd >= 'a') && (cmd <= 'z')); if (!t->listeners[CHAR_TO_INDEX(cmd)]) { return NULL; } n = t->listeners[CHAR_TO_INDEX(cmd)]; t->listeners[CHAR_TO_INDEX(cmd)] = n->next; value = n->value; free(n); return value; }
void command_count(int argc, char *argv, nodotrie *root){ if (argc == 1){ printf("Word count = %d.\n", root->cant_pref); }else{ char* prefix = argv; int nivel; int leng = strlen(prefix); int index; nodotrie *mRecorrer; mRecorrer = root; for (nivel = 0; nivel < leng; nivel++) { index = CHAR_TO_INDEX(prefix[nivel]); if ( !mRecorrer->hijos[index]){ printf("Word count = 0.\n"); return; }else{ mRecorrer = mRecorrer->hijos[index]; } } printf("Word count = %d.\n", mRecorrer->cant_pref); } }
void print(trie_t *pTrie,char key[]) { int level; int length = strlen(key); int index; trie_node_t *pCrawl; pCrawl = pTrie->root; for( level = 0; level < length; level++ ) { index = CHAR_TO_INDEX(key[level]); pCrawl = pCrawl->children[index]; if(level==0 && pCrawl->print==1) printf("Keywords starting with %c\n",key[0]); if(pCrawl->print==1) { printf("-%c",key[level]); pCrawl->print=0; } else if(pCrawl->print==0) printf(" "); } printf("\n"); }
void insertar(nodotrie *mRoot, char key[]){ int nivel; int leng = strlen(key); int index; nodotrie *mRecorrer; mRecorrer = mRoot; for (nivel = 0; nivel < leng; nivel++) { mRecorrer->cant_pref++; index = CHAR_TO_INDEX( (char)tolower(key[nivel])); if ( !mRecorrer->hijos[index]){ mRecorrer->hijos[index] = getNodo(); mRecorrer->hijos[index]->letter = (char) tolower(key[nivel]); } mRecorrer = mRecorrer->hijos[index]; } mRecorrer->es_palabra = 1; mRecorrer->cant_pref++; }
void trie_del_recursive(nodotrie *node, char *str, int pos) { unsigned char ch = (unsigned char)str[pos]; // current char if(ch) // string not finished { int index = CHAR_TO_INDEX(ch); if(node->hijos[index]) // next character is in trie { if (node->cant_pref>0) { node->cant_pref--; } // if (node->cant_pref==0) { // free_nodes(node->hijos[index], str, pos+1); // return; // } trie_del_recursive(node->hijos[index], str, pos + 1); } } else { if(node->es_palabra) { node->es_palabra = 0; if (node->cant_pref>0) { node->cant_pref--; } } } }
void transaction_clear(Transaction *t) { int i; t->xid = INVALID_XID; t->xmin = INVALID_XID; t->size = 0; t->fixed_size = false; t->votes_for = 0; t->votes_against = 0; t->snapshots_count = 0; for (i = 'a'; i <= 'z'; i++) { t->listeners[CHAR_TO_INDEX(i)] = NULL; } }
void insert(Node* root, String* string, int source_id) { int level; int index; Node *crawl = root; for (level = 0; level < string->length; level++) { index = CHAR_TO_INDEX(string->data[level]); if (!crawl->children[index]) crawl->children[index] = get_node(); crawl = crawl->children[index]; } // mark last node as leaf crawl->is_leaf[source_id] = true; }
void insert(trie_t *pTrie, char key[]) { int level; int length = strlen(key); int index; trie_node_t *pCrawl; pTrie->count++; pCrawl = pTrie->root; for( level = 0; level < length; level++ ) { index = CHAR_TO_INDEX(key[level]); if( !pCrawl->children[index] ) pCrawl->children[index] = getNode(); pCrawl = pCrawl->children[index]; } pCrawl->value = pTrie->count; }
long int trieSearch(Trie trie, char* word) { long int val = -1; int i, c; Node node; if (!trie || !word) return val; node = trie->root; for (i = 0; i < (int)strlen(word); i++) { if (!isalpha(word[i])) return -1; c = CHAR_TO_INDEX(word[i]); if (!node->next[c]) break; /* word not found */ node = node->next[c]; val = node->x; } return val; }
// Returns true if key presents in trie, else false bool search(Node* root, const char* key, int source_id) { int level; int length = strlen(key); int index; Node* crawl = root; for (level = 0; level < length; level++) { index = CHAR_TO_INDEX(key[level]); if (!crawl->children[index]) return false; crawl = crawl->children[index]; } return (crawl != NULL && crawl->is_leaf[source_id]); }
// Returns non zero, if key presents in trie int search(trie_t *pTrie, char key[]) { int level; int length = strlen(key); int index; trie_node_t *pCrawl; pCrawl = pTrie->root; for( level = 0; level < length; level++ ) { index = CHAR_TO_INDEX(key[level]); if( !pCrawl->children[index] ) { return 0; } pCrawl = pCrawl->children[index]; } return (0 != pCrawl && pCrawl->value); }
// Returns true if key presents in tries, else false bool search(struct TrieNode *root, const char *key) { int level; int length = strlen(key); int index; struct TrieNode *pCrawl = root; for (level = 0; level < length; level++) { index = CHAR_TO_INDEX(key[level]); if (!pCrawl->children[index]) return false; pCrawl = pCrawl->children[index]; } return (pCrawl != NULL && pCrawl->isLeaf); }
/** Inserts a word into the trie. */ trie* insert_trie(trie* root, char* word) { if(root==NULL) root=trieCreate(); int length=strlen(word); int level; struct TrieNode*pcrawl=root; for(level=0;level<length;level++) { int index=CHAR_TO_INDEX(word[level]); if(!pcrawl->Child[index]) { pcrawl->Child[index]=getNode(); } pcrawl=pcrawl->Child[index]; } pcrawl->value=1; }
int command_search(int argc, char *argv, nodotrie *root, int print){ if (argc != 2){ if (print == 1) { printf("Error al usar insertar, numero de argumentos incorrectos.\n"); } return 0; } int len = strlen(argv); int nivel; int index; nodotrie *mRecorrer; mRecorrer = root; for (nivel = 0; nivel < len; nivel++) { index = CHAR_TO_INDEX((char)tolower(argv[nivel])); if ( !mRecorrer->hijos[index]){ if (print == 1) { printf("No existe la palabra ingresada.\n"); } return 0; }else{ mRecorrer = mRecorrer->hijos[index]; } } if (mRecorrer->es_palabra) { if (print == 1) { printf("Palabra encontrada: %s\n", argv); } return 1; }else{ if (print == 1) { printf("No existe la palabra ingresada.\n"); } return 0; } }
// If not present, inserts key into tries , If the key is prefix of tries node, just marks leaf node void insert(struct TrieNode *root, const char *key) { int level; int length = strlen(key); int index; struct TrieNode *pCrawl = root; for (level = 0; level < length; level++) { index = CHAR_TO_INDEX(key[level]); if (!pCrawl->children[index]) pCrawl->children[index] = getNode(); pCrawl = pCrawl->children[index]; } // mark last node as leaf pCrawl->isLeaf = true; }
// If not present, inserts key into trie // If the key is prefix of trie node, just marks leaf node void insert(struct trie *pTrie, char *key,int id) { int level; int length = strlen(key); int index; trie_node_t *pCrawl; pTrie->count++; pCrawl = pTrie->root; for( level = 0; level < length; level++) { if(key[level]>=97&&key[level]<=122) index = CHAR_TO_INDEX(key[level]); else index = CHAR_TO_INDEX1(key[level]); if( !pCrawl->children[index] ) { pCrawl->children[index] = getNode(); } pCrawl = pCrawl->children[index]; } struct id *temp; if(pCrawl->mode==NULL) { pCrawl->mode=createnode_id(); temp=pCrawl->mode; temp->id=id; } else { temp=pCrawl->mode; insert_id(temp,id); } // mark last node as leaf pCrawl->value = pTrie->count; }
void insert(struct trie *node,char key []) { int len = strlen(key); int index; struct trieNode *p; node->count++; p = node->root; for(int i = 0 ; i <len; i++) { index = CHAR_TO_INDEX(key[i]); if(!p->children[i]) { p->children[i] = getNode(); } p = p->children[i]; } p->isLeaf = true; }
int insert(Trie trie, char* word, long int value) { int i, c; Node node; if (!trie || !word) return 0; node = trie->root; for (i = 0; i < (int)strlen(word); i++) { if (!isalpha(word[i])) return 0; c = CHAR_TO_INDEX(word[i]); if (!node->next[c]) { node->next[c] = newNode(); if (!node->next[c]) { /* allocation failed */ trieDestroy(trie); return 0; } } if (i == strlen(word) - 1) node->next[c]->x = value; /* mark as leaf */ node = node->next[c]; } return 1; }
void command_prefix(int argc, char *argv, nodotrie *root){ if (argc != 2){ printf("Error al usar insertar, numero de argumentos incorrectos.\n"); return; } int prefix_len = strlen(argv); if (limit<=prefix_len) { printf("El prefijo no puede superar los %d caracteres.\n", limit); return; } char* prefix = argv; int nivel; int index; nodotrie *mRecorrer; mRecorrer = root; for (nivel = 0; nivel < prefix_len; nivel++) { index = CHAR_TO_INDEX(prefix[nivel]); if ( !mRecorrer->hijos[index]){ printf("No hay palabras con el prefijo ingresado.\n"); return; }else{ mRecorrer = mRecorrer->hijos[index]; } } char branch[100]; printTree(mRecorrer, 0, branch, prefix); }
int search(trie * node,char key[]) { int level; int len = strlen(key); int index; trieNode *p; p = node->root; for( level = 0; level < len; level++ ) { index = CHAR_TO_INDEX(key[level]); if( !p->children[index] ) { return 0; } p = p->children[index]; } return (0 != p && p->isLeaf); }