Ejemplo n.º 1
0
// helper function to display code
void HuffmanTree::findCode(BST<CharFreq>::BSTNode* node){
  if(node->left == nullptr && node->right == nullptr){
	cout<<"Charater:"<< node->data.letter<<" Encoding:"<< node->data.encoding<<endl;
  }
  if(node->right!=nullptr){
	findCode(node->right);
  }
  if(node->left != nullptr){
	findCode(node->left);
  }
}
Ejemplo n.º 2
0
/**
 * LZW main compress function
 * @param s LZW state
 * @param inbuf Input buffer
 * @param insize Size of input buffer
 * @return Number of bytes written or -1 on error
 */
int ff_lzw_encode(LZWEncodeState *s, const uint8_t *inbuf, int insize)
{
    int i;

    if(insize * 3 > (s->bufsize - s->output_bytes) * 2)
    {
        return -1;
    }

    if (s->last_code == LZW_PREFIX_EMPTY)
        clearTable(s);

    for (i = 0; i < insize; i++)
    {
        uint8_t c = *inbuf++;
        int code = findCode(s, c, s->last_code);
        if (s->tab[code].hash_prefix == LZW_PREFIX_FREE)
        {
            writeCode(s, s->last_code);
            addCode(s, c, s->last_code, code);
            code = hash(0, c);
        }
        s->last_code = s->tab[code].code;
        if (s->tabsize >= s->maxcode - 1)
        {
            clearTable(s);
        }
    }

    return writtenBytes(s);
}
Ejemplo n.º 3
0
void MonsterAILib::takeNode(script::tixmlCodeNode* node)
{
	if (!node) return; // 处理事件
	script::tixmlCodeNode aiNode = node->getFirstChildNode("ai");
	while (aiNode.isValid())
	{
		script::tixmlCodeNode codePtr = aiNode.getFirstChildNode("event");
		MutiAI *ai = new MutiAI();
		aiNode.getAttr("id",ai->id);
		if (ai->id >= npcAis.size())
		{
			npcAis.resize(ai->id + 1);
		}
		if (!npcAis[ai->id])
		{
			npcAis[ai->id] = ai;	
		}
		else
			delete ai;
		while (codePtr.isValid())
		{
			std::string name = codePtr.getAttr("name");
			std::string code = codePtr.getAttr("code");
			npcAis[ai->id]->addCode(findCode(code.c_str()),&codePtr);
			codePtr = codePtr.getNextNode("event");	
		}	
		aiNode = aiNode.getNextNode("ai");
	}		
}
Ejemplo n.º 4
0
void linkHash(int prefix, int c, int index, Table *stringTable)
{
	int h = hash(prefix, c, stringTable->hashSize);
	Node *chain = stringTable->pairHash[h];
	Node *node = malloc(sizeof(*node));
	node->pair = stringTable->table[index];
	node->index = index;
	node->next = NULL;
	if(chain == NULL) { //this chain hasn't been initialized
		stringTable->pairHash[h] = node;
	} else {
		while(chain->next != NULL)
			chain = chain->next;
		chain->next = node;
	}
//SCAFFOLDING TO SEARCH() IMMEDIATELY AFTER INSERTING
	int dummy = 0;
	int scaffoldIndex = findCode(prefix, c, stringTable, &dummy, &dummy);
	if(scaffoldIndex == -1) {
		fprintf(stderr, "SCAFFOLD CODE: COULD NOT FIND (%d, %c) AFTER INSERTION\n", prefix, c);
		exit(EXIT_FAILURE);
	}
	int scaffoldPrefix = stringTable->table[scaffoldIndex]->prefix;
	int scaffoldChar = stringTable->table[scaffoldIndex]->c;
	if(prefix != scaffoldPrefix || c != scaffoldChar) {
		fprintf(stderr, "SCAFFOLD CODE: COULD NOT FIND (PREF, CHAR) AFTER INSERTION\n");
		exit(EXIT_FAILURE);	
	}
}
Ejemplo n.º 5
0
/**
 * 更改事件响应函数
 **/
int MonsterAILib::bind_event(MutiAIStub* stub,script::tixmlCodeNode * node)
{
	if (stub && stub->ai)
	{
		stub->ai->bindEvent(node->getAttr("event"),findCode(node->getAttr("code")));
	}
	return 0;
}
Ejemplo n.º 6
0
void HuffmanTree::displayCode(ostream &out)
{
	// **************************************************************
	// TODO: print out each letter and its code, you might want to check for special cases (space, newline, etc.)

  findCode(root); 
  
	// **************************************************************
}
BitmapPtr
strikeBitmapIndex(StrikePtr strike, CmapPtr cmap, int index)
{
    int code = findCode(cmap, index);
    if(code < 0)
        return NULL;

    return STRIKE_BITMAP(strike, code);
}
Ejemplo n.º 8
0
void callFromCode(char *code, FILE *stream) {
  int i;
  char *res;

  i = findCode(code);
  if (i >= 0) {
    sprintf(cmdresult, "ok");
    // printf("%s: ", code);
    cmdArray[i](stream);
    fprintf(stdout, "%s\n", cmdresult);
  } else {
    fprintf(stdout, "bad\n");
  }
  fflush(stdout);
}
Ejemplo n.º 9
0
//Lembrar de enviar o dataMSize->stkHeap_size para o numFrames
void createFrame(method_info *method, ClassFile *Class, Frame *frame_ptr, u2 *numFrames) {
    
    u2 i = *numFrames;
    u2 codeIndex = 0;
    codeIndex = findCode(method);
    
    if (i < STKFRAME_MAX - 1) {
        frame_ptr[i].pClass = Class;
        frame_ptr[i].pMethod = method;
        frame_ptr[i].code_length = method->attribute[codeIndex].info.Code_attribute.code_length;
        frame_ptr[i].code = method->attribute[codeIndex].info.Code_attribute.code;
        
        frame_ptr[i].pc = 0;
        frame_ptr[i].sp = 0;
        
        frame_ptr[i].stack_size = method->attribute[codeIndex].info.Code_attribute.max_stack;
        frame_ptr[i].local_size = method->attribute[codeIndex].info.Code_attribute.max_locals;
        frame_ptr[i].stack = malloc(frame_ptr[i].stack_size * sizeof(u4));
        frame_ptr[i].local = malloc(frame_ptr[i].local_size * sizeof(u4));
        
        /*TESTE*/
        //      frame_ptr[i - 1].pClass = Class;
        //          frame_ptr[i -1].pMethod = method;
        //          frame_ptr[i -1].code_length = Class->methods->attribute[codeIndex].info.Code_attribute.code_length;
        //          frame_ptr[i - 1].code = malloc(frame_ptr[i].code_length * sizeof(u1));
        //
        //          frame_ptr[i - 1].pc = 0;
        //          frame_ptr[i - 1].sp = 0;
        //
        //          frame_ptr[i - 1].stack_size = Class->methods->attribute[codeIndex].info.Code_attribute.max_stack;
        //          frame_ptr[i - 1].local_size = Class->methods->attribute[codeIndex].info.Code_attribute.max_locals;
        //          frame_ptr[i - 1].stack = malloc(frame_ptr[i].stack_size * sizeof(u4));
        //          frame_ptr[i - 1].local = malloc(frame_ptr[i].local_size * sizeof(u4));
        //
        
        
        (*numFrames)++;
    } else {
        printf("Frame não pode ser alocado, tamanho máximo atingido");
        exit(1);
    }
}
Ejemplo n.º 10
0
int addStudent(Student * student, int code, char * name) {
    if (findCode(student, code) == 0) {
        Student * p = lastPoint(student);
        if (student->name == "" && student->code == 0 && student->next == 0) {
            p->code = code;
            p->name = name;
            p->next = 0;
        }
        else {
            p->next = malloc(sizeof(Student));
            p->next->code = code;
            p->next->name = name;
            p->next->next = 0;
        }
        return 0;
    }
    else {
        return -1;
    }
}
Ejemplo n.º 11
0
int main(int argc, char **argv)
{
	clock_t begin, end;
	double time_spent;
	begin = clock();
	int NUM_FINDCODE_CALLS = 0;
	int NUM_HASHCELLS_VISITED = 0;
	int c = 0; //char in (pref, char)
	int prefix = EMPTY_CODE;
	int index = EMPTY_CODE;
	int MAXBITS = 15; //default
	char DUMP_TO[PATH_MAX];
	char INIT_FROM[PATH_MAX];
	int PRUNE = -1;
	Table *stringTable = initStringTable(MAXBITS);
//setting params from cmd line argv's STARTING FROM 2 BC ENCODE/DECODE
	for (int i = 2; i < argc; ++i)
	{
		if (strcmp(argv[i], "-m") == 0) {
			//ERROR CHECK FOR IF NEXT ARGV IS NOT INTEGER STRING??? 
			MAXBITS = atoi(argv[i + 1]);
			i++;
		} else if (strcmp(argv[i], "-o") == 0) {
			if(i + 1 == argc) {
				fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
				exit(EXIT_FAILURE);
				//EDGE CASE: IF -O -O HI, THEN DO WE DUMP TO '-O' AND SIGNAL ERROR ON HI? OR DO WE DUMP TO -O, AND OVERRIDE WITH DUMPING TO 'HI'? 
			}
			strncpy(DUMP_TO, argv[i + 1], strlen(argv[i + 1]));
			i++;
		} else if (strcmp(argv[i], "-i") == 0) {
			if(i + 1 == argc) {
				fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
				exit(EXIT_FAILURE);
			}
			strncpy(INIT_FROM, argv[i + 1], strlen(argv[i + 1]));
			i++;
		} else if (strcmp(argv[i], "-p") == 0) {
			if(i + 1 == argc) {
				fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
				exit(EXIT_FAILURE);
			}
			PRUNE = atoi(argv[i + 1]); //test if not an integer; what if prune is 0? w
			i++;
			if(PRUNE){}
		} else {
			fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
			exit(EXIT_FAILURE);
		}
	}
	fprintf(stderr, "argv[0]: %s\n", argv[0]);
	if(strcmp(argv[1], "encode") == 0) {
//ENCODE	
		while((c = getchar()) != EOF) {
			index = findCode(prefix, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED);
			if (index != -1) //(pref, char) found in stringTable
			{
				prefix = index;
				(stringTable->table[prefix]->useCount)++;
				continue;
			} else { //not found in stringTable
				putBits(stringTable->nBits, prefix);
				insertToTable(prefix, c, &stringTable, PRUNE);
				prefix = findCode(0, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED); //start search again at finalChar of newly found sequence
				(stringTable->table[prefix]->useCount)++;
			}
		}
		putBits(stringTable->nBits, prefix);
		flushBits();
		// printTable(stringTable, "encode");
		// fprintf(stderr, "\nNUM_FINDCODE_CALLS: %d\nNUM_HASHCELLS_VISITED: %d\nAVG_SEARCH_RATIO: %d\n", NUM_FINDCODE_CALLS, NUM_HASHCELLS_VISITED, NUM_HASHCELLS_VISITED / NUM_FINDCODE_CALLS);
	} else if(strcmp(argv[1], "decode") == 0) {
//DECODE
		char ENCODE_OR_DECODE[25] = "decode";
		int oldIndex = 0, firstCharOfNewIndex = 0; //used to build stringTable, 1 step behind encode
		int newIndex = 0;
		int cascadingIndex = 0; //used to print a code string recursively
		int kwkwk = 0; //used in kwkwk case
		int prunedThisPass = 0;
		int HIT_THE_LIMIT = 0;

		while( (newIndex = cascadingIndex = getBits(stringTable->nBits)) != EOF) {
				//nBits was incremented just before to accomodate increased nBits in ENCODE that hasn't been automatically detected in DECODE
				//and we decrement it now bc it will be automatically incremented in INSERTTABLE
				//but if == MAXBITS, then we did not artificially increment nBits last round 

			if(stringTable->last + 1 == stringTable->size && !HIT_THE_LIMIT ) {//2nd condition happens when table is already maxSize, in which case no more doubling or pruning happens
				if(!prunedThisPass) //when pruning results in an at-limit table
					stringTable->nBits--;
				if(stringTable->size != 1<<stringTable->nBits){
					fprintf(stderr, "size: %d, nBits: %d\n", stringTable->size, stringTable->nBits);
					exit(EXIT_FAILURE);
				} else {
				}
			}
			if(newIndex > stringTable->last) //newIndex is an unknown code (kwkwk abnormal case)
			{
				kwkwk = 1;
				if(newIndex < stringTable->size)
					stringTable->table[newIndex]->useCount++; //useCount usually incremented in printRecursive function, which will not receive newIndex in this case
				cascadingIndex = oldIndex;
			}
			printRecursive(&cascadingIndex, stringTable);
			if (kwkwk == 1) {
				printf("%c", firstCharOfNewIndex); //output should be oldCode, oldCode, firstCharOfOldCode
				kwkwk = 0;
			}
			if(cascadingIndex >= stringTable->size) {
				fprintf(stderr, "cascadingIndex: %d, size: %d\n", cascadingIndex, stringTable->size);
				exit(EXIT_FAILURE);
			}
			firstCharOfNewIndex = stringTable->table[cascadingIndex]->c; //finalK = char(c)
			if(oldIndex != 0 && !prunedThisPass) { //every time except first time, and after pruning, add new code to table
				insertToTable(oldIndex, firstCharOfNewIndex, &stringTable, PRUNE);
			}
			prunedThisPass = 0;
			oldIndex = newIndex;
			if(stringTable->last + 1 >= stringTable->size) { 
			//decode is one step behind encode; deocde inserts new code every time it reads a code, starting from 2nd code
			//encode adds a code at every code starting from 1st code. CURRENT CODE + firstChar of NEXT CODE was the last code
			//encode tried to add to table and PRUNED or DOUBLE'd
			//=>PRUNING or DOUBLING::
			//if DOUBLING: next code will increment nBits
			//if PRUNING: next code is encoded from a PRUNED table, not CURRENT table (and we don't insert next round)
				if(stringTable->nBits != stringTable->MAXBITS) {
					if(stringTable->nBits == 8){
						exit(EXIT_FAILURE);
					}
					stringTable->nBits++;
				} else { //next insert exceeds table size, and table size is MAXBITS size, so PRUNE
					HIT_THE_LIMIT = 1;
					if(PRUNE != -1) {
						prune(&stringTable, PRUNE, ENCODE_OR_DECODE);
						prunedThisPass = 1;
						if (stringTable->last + 1 == stringTable->size)
						{
							fprintf(stderr, "HERE!!! last: %d, size: %d\n", stringTable->last, stringTable->size);
							char temp[10] = "encode";
							printTable(stringTable, temp);
						}
						if(stringTable->last + 1 != 1<<stringTable->MAXBITS)
							HIT_THE_LIMIT = 0;
					}
				}
			}
		}
		// printTable(stringTable, "decode");
	}
	moses(stringTable);
	end = clock();
	time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
	fprintf(stderr, "time_spent: %f\n", time_spent);
}