lztrie buildLZTrie(byte *text, uint **ids, byte s) { trie T; uint n; uint *parent; byte *letters; lztrie LZT; // first creates a full trie T #ifdef INDEXREPORT ticks= sysconf(_SC_CLK_TCK); times(&time); t1 = time.tms_utime; printf (" Building LZTrie...\n"); fflush(stdout); printf (" Building normal trie...\n"); fflush(stdout); #endif T = createTrie(); do { text = insertTrie(T,text); } while (text[-1]!=s); // now compresses it #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" Representing with parentheses, letters and ids...\n"); fflush(stdout); #endif n = T->nid; parent = malloc (((2*n+W-1)/W)*sizeof(uint)); letters = malloc (n*sizeof(byte)); *ids = malloc (n*sizeof(uint)); representTrie (T,parent,letters,*ids); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" Freing trie...\n"); fflush(stdout); #endif destroyTrie(T); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" Creating compressed trie...\n"); fflush(stdout); #endif LZT = createLZTrie (parent,letters,*ids,n); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf (" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf (" End of LZTrie\n"); fflush(stdout); #endif return LZT; }
ACListe createACListe(){ ACListe acListe = (ACListe) malloc (sizeof(ACListe)); acListe->trie = createTrie(maxNode); acListe->suppleant = (int*) malloc(maxNode * sizeof(int)); int i; for(i=0;i<maxNode;i++){ acListe->suppleant[i] = -1; } return acListe; }
Trie preAC(unsigned char **mots, int nb_max) { /* Création de l'etat q0 et du trie */ Trie trie = createTrie(nb_max * 60); /* Taille max d'un mot = 60 */ int i; /* Inserer dans le trie */ for (i = 0; i < nb_max; i++) {insertTrie(trie, mots[i]);} /* Completer la racine */ completer_trie(trie); /* Completer avec les supps*/ complete(trie); /* return */ return trie; }
AcTrie initAcTrie () { AcTrie acTrie; //Le nouveau trie //Allocation de la structure de l'ac-trie acTrie = (AcTrie) malloc (sizeof (struct _ac_trie)); //Création du trie interne acTrie->trie = createTrie (TRIE_SIZE, 256); //Inititalisation du tableau des sortie. acTrie->sortie = (char *) malloc (TRIE_SIZE * sizeof (char)); memset (acTrie->sortie, 0, TRIE_SIZE); //Initialisation des suppléants (aucun suppléants) acTrie->sup = (int *) malloc (TRIE_SIZE * sizeof (int)); memset (acTrie->sup, -1, TRIE_SIZE); return acTrie; }
Trie trieFromFile(FILE *inputFile) { Trie fileTrie = createTrie(); char word[MAX_WORD_LENGTH]; while(fscanf(inputFile, " %s ",word) != EOF) { int wordLength = strlen(word); int i; for(i=0;i<wordLength;i++) { word[i] = toupper(word[i]); assert(FIRST_LETTER <= word[i]); assert(word[i] <= LAST_LETTER); } addWord(fileTrie, word); } return fileTrie; }
int main(int argc, char* argv[]) { freopen("CON", "w", stdout); freopen("CON", "r", stdin); freopen("CON", "w", stderr); /*************************** *** Dico & Grid creation *** ***************************/ // Trie for dictionnary Trie *t = (Trie*)malloc(sizeof(Trie)); // Trie for grid Trie *tGrid = (Trie*)malloc(sizeof(Trie)); // Grid Cell grid[N][N]; // Create Trie with words from file createFullTrie(LOCATION_DICO, t); // Create Trie for grid createTrie(tGrid); // Create grid createFullGrid(LOCATION_GRID, grid); /**************************** ********** Display ********** ****************************/ const unsigned int SCREEN_WIDTH = 480; const unsigned int SCREEN_HEIGHT = 650; TTF_Init(); SDL_Surface* screen = NULL; // screen principal char continu = 1; int state = 0; // array for coord int coord[17][2] = {{0,0}}; int c = 0; int *cpt = &c; int scoreTotal = 0; int *pScore = malloc(sizeof(int)); pScore = &scoreTotal; if (init() != 0) { fprintf(stderr,"SDL init failed\n"); return -1; } screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,32,SDL_SWSURFACE | SDL_DOUBLEBUF); SDL_WM_SetCaption("RUZZLE : Etude de Cas", NULL); if (screen == NULL) { fprintf(stderr,"Open Window Failed\n"); close(); return -2; } PrincipalWindow *principal = principal_window_create(); GridWindow *gride = NULL; ScoreWindow *score = NULL; SDL_Event event; // Boucle principale while (continu) { switch (state) { case 0: principal_window_draw(principal,screen); if (principal_window_load_window_grid(principal,event) != 0) { principal_window_destroy(principal); gride = grid_window_create(pScore, grid); grid_window_draw(gride,screen, event); letter_display(LOCATION_GRID, gride, screen); state = 1; } break; case 1: if(grid_window_draw_on_clic(gride, screen, event, t, tGrid, grid, coord, cpt, pScore) == 1){ continu = 0; } if (grid_window_update(gride) != 0) { grid_window_destroy(gride); score = score_window_create(); state = 2; } break; case 2: score_window_draw(score, screen); if (score_window_load(score, event) != 0) { score_window_destroy(score); destroyTrie(tGrid); principal = principal_window_create(); state = 0; } break; } //to close application on cross clic if (event.type == SDL_QUIT) { //leave the principal loop continu = 0; } SDL_PollEvent(&event); SDL_Delay(10); SDL_Flip(screen); } close(); grid_window_destroy(gride); destroyTrie(tGrid); destroyTrie(t); return 0; }
revtrie buildRevTrie(lztrie T, uint maxdepth, uint **ids) { byte *str; uint n,rn,depth,j; trieNode i; trie RT; uint *parent, *emptybmap, *inv_ids, nbits, pos; revtrie CRT; unsigned long long aux; // first create a full trie RT #ifdef INDEXREPORT times(&time); t1 = time.tms_utime; printf (" Building RevTrie...\n"); fflush(stdout); printf (" Creating full trie...\n"); fflush(stdout); #endif str = malloc(maxdepth*sizeof(byte)); RT = createTrie(); i = ROOT; depth = 0; for (j=1;j<T->n;j++) { i = nextLZTrie(T,i,&depth); str[maxdepth-depth] = letterLZTrie(T,i); pos = leftrankLZTrie(T, i); insertstringTrie(RT,str+maxdepth-depth,depth,rthLZTrie(T,pos)); } free(str); // now compresses it #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Representing with parentheses and ids...\n"); fflush(stdout); #endif n = T->n; rn = RT->nid; aux = (2*(unsigned long long)rn+W-1)/W; parent = malloc(aux*sizeof(uint)); // 2*rn bits emptybmap = calloc(((rn+W-1)/W),sizeof(uint)); // rn bits aux = (((unsigned long long)n)*bits(n-1)+W-1)/W; *ids = malloc(aux*sizeof(uint)); // the rids array has n entries // (only for the non-empty nodes) inv_ids = malloc(aux*sizeof(uint)); representTrie(RT,parent,NULL,*ids,emptybmap,bits(n-1)); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Freeing trie...\n"); fflush(stdout); #endif destroyTrie(RT); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" Creating compressed trie...\n"); fflush(stdout); #endif nbits = bits(n-1); for (i = 0; i < n; i++) bitput(inv_ids,bitget(*ids, i*nbits, nbits)*nbits,nbits,i); free(*ids); CRT = createRevTrie(parent,inv_ids,T,emptybmap,rn); #ifdef INDEXREPORT times(&time); t2 = time.tms_utime; printf(" User time: %f secs\n",(t2-t1)/(float)ticks); fflush(stdout); t1 = t2; printf(" End of RevTrie...\n"); fflush(stdout); #endif return CRT; }
bool Dictionary::insertTrie(string word, string definition) { int wordSize = word.length(); int* digit = new int[wordSize]; string levels ="", lowerWord = ""; Trie temp; lastAccessed = root; //This loop creates a string of lowercase characters. for(int i = 0; i < wordSize; i++) { lowerWord += tolower(word[i]); } //This loop finds the corresponding number for each digit. From [a-z] to [1-26]. for(int i = 0; i < wordSize; i++) { int asciiCode = 97; for(int j = 0; j < ALPHABET_SIZE; j++) { if(asciiCode == static_cast<int>(lowerWord[i])) { digit[i] = j; break; } else { asciiCode++; } } } //This loop determines whether a trie needs to be created. for(int k = 0; k < (wordSize - 1); k++) { levels = ""; creationIndex = digit[k]; temp = lastAccessed; lastAccessed = lastAccessed->next[creationIndex]; for(int l = 0; l < (k + 1); l++) { levels += word[l]; } if(lastAccessed == nullptr) { lastAccessed = temp; lastAccessed = createTrie(); lastAccessed->word = levels; } } //This is where i determine if the insertion was succesful creationIndex = digit[wordSize - 1]; temp = lastAccessed; lastAccessed = lastAccessed->next[creationIndex]; if(lastAccessed == nullptr) { lastAccessed = temp; lastAccessed = createTrie(); lastAccessed->word = word; lastAccessed->definition = definition; lastAccessed = root; delete[] digit; return true; } //If the size of the definition is greater than 0. Then we have a duplicate. if(lastAccessed->definition.length() == 0) { lastAccessed->definition = definition; lastAccessed = root; delete digit; return true; } else { lastAccessed = root; delete digit; return false; } }
bool ChrsGrid::init(ValueMap level_info, int col, int row) { Node::init(); m_letter_info = level_info;//获取关卡配置信息 //设置棋盘的锚点和大小,锚点为中心 this->setContentSize(Size(col*GRID_WIDTH, row*GRID_WIDTH)); this->setAnchorPoint(Vec2(0.5, 0.5)); //绘制一个矩形,用以测试棋盘范围 auto drawnode = DrawNode::create(); drawnode->drawRect(Vec2(0, 0), Vec2(this->getContentSize().width, this->getContentSize().height), Color4F::RED); //addChild(drawnode); //棋盘底图 auto gridbg = ui::Scale9Sprite::create("grid_bg.png"); gridbg->setContentSize(Size(col * GRID_WIDTH + 10, row * GRID_WIDTH + 10)); gridbg->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2); addChild(gridbg, 0, 1000); //得到单词集合 m_Letters = level_info.at("letter").asValueVector(); //初始化汉字集合 initChrBox(); //生成布局 //根据行列初始化一个空的汉字盒子大小 m_row = row; m_col = col; m_canCrush = false; m_SelectedChrs.clear(); m_NewChrs.clear(); m_ChrsBox.resize(m_col); for (auto &vec : m_ChrsBox) { vec.resize(m_row); } //生成汉字字典树 createTrie(&chr_root, &m_Letters); //1.根据布局大小创建出汉字阵列 //2.布局坐标以左下角为原点,x右y上为正方向 for (int x = 0; x < m_col; x++) { for (int y = 0; y < m_row; y++) { m_ChrsBox[x][y] = createAChr(x, y); } } //判断是否是死图 while (isDeadMap()) { //这里稍后做一个更新的算法 for (int x = 0; x < m_col; x++) { for (int y = 0; y < m_row; y++) { m_ChrsBox[x][y]->removeFromParent(); m_ChrsBox[x][y] = createAChr(x, y); } } } //加入触摸监听 auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(ChrsGrid::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(ChrsGrid::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(ChrsGrid::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(ChrsGrid::onTouchCancelled, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //开始提示倒计时 resetCountdown(); schedule(schedule_selector(ChrsGrid::onCountdownCallBack), 1); log("ChrsGrid init!"); return true; }