/** Write code for code fragements and @a NonTerminal's to file

	Note: for LLgen style code generation, each @a NonTerminal is written to
	the output file corresponding to the input file in which it was declared.
*/
static void generateCode(void) {
	int i;
	Declaration *declaration;
	FileListItem *output;
	
	for (i = 0; i < listSize(declarations); i++) {
		declaration = (Declaration *) listIndex(declarations, i);
		switch (declaration->subtype) {
			case CODE:
				output = findOutput(declaration->uCode->fileName);
				outputCode(output->output, declaration->uCode, false);
				break;
			case DIRECTIVE:
				/* output = findOutput(declaration->uDirective->token[0]->fileName); */
				break;
			case NONTERMINAL:
				output = findOutput(declaration->uNonTerminal->token->fileName);
				/* Generate the declarations for all generated NonTerminal
				   functions right before the first one (for each file), so we
				   always declare them before use. */
				if (output->firstNonTerminal) {
					output->firstNonTerminal = false;
					generatePrototypes(output->output);
				}
				generateNonTerminalCode(output->output, declaration->uNonTerminal);
				break;
			default:
				PANIC();
		}
	}
}
示例#2
0
void EzwEncoder::dominantPass(cv::Mat& mat, int32_t threshold) {
	initDominantPassQueue(mat, threshold);

	do {
		// get elm from queue and output it
		auto elm = dpQueue.front();
		dpQueue.pop_front();
		outputCode(elm.code);

		// we don't need to code zerotree children cos they are zero
		if (elm.code != Element::Code::ZeroTreeRoot) {
			// handle elm children
			auto minx = elm.x * 2;
			auto miny = elm.y * 2;
			auto maxx = minx + 1;
			auto maxy = miny + 1;
			if (maxx <= (size_t)mat.cols && maxy <= (size_t)mat.rows) {						// last level doesn't have children
				for (auto y = miny; y < maxy + 1; ++y) {
					for (auto x = minx; x < maxx + 1; ++x) {
						auto child = codeElement(mat, x, y, threshold);
						dpQueue.push_back(child);
					}
				}
			}
		}

	} while(!dpQueue.empty());
}
示例#3
0
文件: lzw.c 项目: krishpop/LZW
// prints the full string that is encoded with code by walking down trie
char outputCode(Trie *st, int code, bool print) {
    int pre = (*st)[code].prefix;
    (*st)[code].appearances++;
    char baseK;
    if(pre==EMPTY) {
        baseK = (char)(*st)[code].suffix;
        if(print) {
            putchar(baseK);
        }
    } else {
        baseK = outputCode(st, pre, print);
        if(print) {
            putchar((*st)[code].suffix);
        }
    }
    return baseK;
}
void
GenerateForm
	(
	istream&			input,
	const JString&		formName,
	const JString&		tagName,
	const JString&		enclName,
	const JString&		codePath,
	const JString&		stringPath,
	const JString&		codeSuffix,
	const JString&		headerSuffix,
	JPtrArray<JString>*	backupList
	)
{
	const JString codeFileName    = codePath + formName + codeSuffix;
	const JString codeFileBakName = codeFileName + kBackupSuffix;

	const JString headerFileName    = codePath + formName + headerSuffix;
	const JString headerFileBakName = headerFileName + kBackupSuffix;

	if (!JFileExists(codeFileName))
		{
		cerr << codeFileName << " not found" << endl;
		return;
		}
	if (!JFileExists(headerFileName))
		{
		cerr << headerFileName << " not found" << endl;
		return;
		}

	cout << "Generating: " << formName << ", " << tagName << endl;

	const JBoolean shouldBackup = ShouldBackupForm(formName, backupList);

	// copy code file contents before start delimiter

	JString tempCodeFileName;
	JError err = JCreateTempFile(codePath, NULL, &tempCodeFileName);
	if (!err.OK())
		{
		cerr << "Unable to create temporary file in " << codePath << endl;
		cerr << "  (" << err.GetMessage() << ')' << endl;
		return;
		}

	ifstream origCode(codeFileName);
	ofstream outputCode(tempCodeFileName);
	if (!outputCode.good())
		{
		cerr << "Unable to open temporary file in " << codePath << endl;
		remove(tempCodeFileName);
		return;
		}
	if (!CopyBeforeCodeDelimiter(tagName, origCode, outputCode))
		{
		cerr << "No starting delimiter in " << codeFileName << endl;
		outputCode.close();
		remove(tempCodeFileName);
		return;
		}

	// generate code for each object in the form

	JPtrArray<JString> objTypes(JPtrArrayT::kDeleteAll),
					   objNames(JPtrArrayT::kDeleteAll);
	GenerateCode(input, outputCode, stringPath, formName, tagName, enclName,
				 &objTypes, &objNames);

	// copy code file contents after end delimiter

	JBoolean done = CopyAfterCodeDelimiter(tagName, origCode, outputCode);
	origCode.close();
	outputCode.close();

	if (!done)
		{
		cerr << "No ending delimiter in " << codeFileName << endl;
		remove(tempCodeFileName);
		return;
		}
	else if (shouldBackup && rename(codeFileName, codeFileBakName) != 0)
		{
		cerr << "Unable to rename original " << codeFileName << endl;
		remove(tempCodeFileName);
		return;
		}
	JEditVCS(codeFileName);
	rename(tempCodeFileName, codeFileName);

	// copy header file contents before start delimiter

	JString tempHeaderFileName;
	err = JCreateTempFile(codePath, NULL, &tempHeaderFileName);
	if (!err.OK())
		{
		cerr << "Unable to create temporary file in " << codePath << endl;
		cerr << "  (" << err.GetMessage() << ')' << endl;
		return;
		}

	ifstream origHeader(headerFileName);
	ofstream outputHeader(tempHeaderFileName);
	if (!outputHeader.good())
		{
		cerr << "Unable to open temporary file in " << codePath << endl;
		remove(tempHeaderFileName);
		return;
		}
	if (!CopyBeforeCodeDelimiter(tagName, origHeader, outputHeader))
		{
		cerr << "No starting delimiter in " << headerFileName << endl;
		outputHeader.close();
		remove(tempHeaderFileName);
		return;
		}

	// generate instance variable for each object in the form

	GenerateHeader(outputHeader, objTypes, objNames);

	// copy header file contents after end delimiter

	done = CopyAfterCodeDelimiter(tagName, origHeader, outputHeader);
	origHeader.close();
	outputHeader.close();

	if (!done)
		{
		cerr << "No ending delimiter in " << headerFileName << endl;
		remove(tempHeaderFileName);
		return;
		}

	// check if header file actually changed

	JString origHeaderText, newHeaderText;
	JReadFile(headerFileName, &origHeaderText);
	JReadFile(tempHeaderFileName, &newHeaderText);
	if (newHeaderText != origHeaderText)
		{
		if (shouldBackup && rename(headerFileName, headerFileBakName) != 0)
			{
			cerr << "Unable to rename original " << headerFileName << endl;
			remove(tempHeaderFileName);
			return;
			}
		JEditVCS(headerFileName);
		rename(tempHeaderFileName, headerFileName);
		}
	else
		{
		remove(tempHeaderFileName);
		}
}
示例#5
0
void lzw_encode(LZWEncoderData *ld, uint8_t *input, int input_length){
  outputCode(ld, ld->clear_code_number);
  for(uint8_t end_index=1; end_index<=input_length; end_index++){
    #ifdef DEBUG
    printf("\nStarting Search from %d to %d: ", ld->start_index, end_index);
    for(int input_byte_index=0; input_byte_index<input_length; input_byte_index++){
      if(input_byte_index==ld->start_index)printf("\e[1;34m");
      printf("%d ",input[input_byte_index]);
      if(input_byte_index+1==end_index)printf("\e[0m");
    }
    printf("\e[0m\n");
    for(int j=0; j<end_index-ld->start_index; j++) printf("%d", input[ld->start_index+j]);
    printf("\n");
    #endif

    LinkedListItem *item = ld->dictionary_branch->first;
    LinkedListItem *nextitem;
    for(int subselect_index=ld->resolved_indexes; subselect_index<end_index-ld->start_index; subselect_index++){
      #ifdef DEBUG
      printf("Checking symbol #%d (%d)\n", ld->start_index+subselect_index, input[ld->start_index+subselect_index]);
      #endif
      uint8_t found = 0;
      do{
        if(item==NULL){
	  #ifdef DEBUG
	  printf("NO CHILDREN FOUND\n");
	  #endif
	  break;
	}
	LZWTreeEntry *treeentry = (LZWTreeEntry*)item->data;
        #ifdef DEBUG
	printf("checking against level %d, rule id: %d value: %d\n", treeentry->level, treeentry->code_number, treeentry->data);
	#endif
	if(treeentry->data==input[ld->start_index+subselect_index]){
	  #ifdef DEBUG
	  printf("FOUND level %d rule %d; id %d\n", treeentry->level, treeentry->data, treeentry->code_number);
	  #endif
	  found=1;
	  ld->resolved_indexes++;
	  ld->dictionary_branch=(LinkedList *)treeentry->children;
	  ld->last_matched_rule = treeentry;
	  break;
	}
	nextitem = item->next;
	item=nextitem;
      }while(item!=0);

      if(found!=1){
	#ifdef DEBUG
	printf("\e[1;33mADDING RULE id: %d; ", ld->next_rule_id);
	for(int rule_data_index=0; rule_data_index<end_index-ld->start_index; rule_data_index++) printf("%d", input[ld->start_index+rule_data_index]);
	printf("\e[0m\n");
	#endif
	outputCode(ld, ld->last_matched_rule->code_number);
	LinkedListItem *new_rule_entry = addNewLinkedListItem(ld->dictionary_branch);
	LZWTreeEntry *treeentry = malloc(sizeof(LZWTreeEntry));
	new_rule_entry->data = (void*)treeentry;
	treeentry->children = malloc(sizeof(LinkedList));
	memset(treeentry->children, 0, sizeof(LinkedList));
	treeentry->data = input[ld->start_index+(end_index-ld->start_index)-1];
	treeentry->code_number = ld->next_rule_id;
	treeentry->level = ld->last_matched_rule->level+1;
       	ld->dictionary_branch = &ld->dictionary;
	ld->start_index=end_index-1;
	end_index--;
	ld->resolved_indexes = 0;
	ld->last_matched_rule=NULL;
	
	ld->next_rule_id++;
	int nextLZWmin = (int)ceil(log2(ld->next_rule_id));
	if(nextLZWmin>12){
	  outputCode(ld, ld->clear_code_number);
	  freeRuleDictionary(&ld->dictionary);
	  ld->next_rule_id = reset_rules(ld);
	  ld->clear_code_number = ld->next_rule_id;
	  ld->end_code_number = ld->next_rule_id+1;
	  ld->next_rule_id+=2;
	  ld->LZWmin = (int)ceil(log2(ld->next_rule_id));
	}else
	  ld->LZWmin = nextLZWmin;
	  
	break;
      }
    }
  }
示例#6
0
int main () {
    //int XOR[4][3] = {{0,0,0,},{0,1,1,},{1,0,1,},{1,1,0,}};	// XOR data
    clock_t t;             // auxiliary variable for compute running time
    float sec;             // auxiliary variable for compute seconds
    double error1,error2;
    int sumData = 0, tPoints = 0;
    char trash[50000];
                           // --------------------
    srand(time(NULL));             // seed for random numbers
    initWeights(-0.1,0.1); // initialize weights
    FILE * trainingSet = fopen("trainingData.txt","r");
    FILE * con1 = fopen("con1.txt","w");
    FILE * con2 = fopen("con2.txt","w");
    while (fgets (trash, 50000 , trainingSet) != NULL) tPoints++;
    t = clock();           // start timer
    
    //printf("\nEpoch:  Output  Des.out. (Error)\n"); // --------------------
    //printf("--------------------------------\n");   // --------------------
    int epoch;                                        // --------------------
    for (epoch = 0; epoch <= epochs; epoch++) { // for every 
        error1 = 0, error2 = 0;
        rewind(trainingSet);
        
        for (int p = 0; p < tPoints; p++) {			// for every pattern
            
            for (int img = 1; img <= IN; img++) {
                fscanf(trainingSet,"%lf",&y(img));
                y(img) = inputCode(y(img));
            }
            fscanf(trainingSet,"%lf %lf",&dv(0),&dv(1));
            dv(0) = outputCode(dv(0));
            dv(1) = outputCode(dv(1));
                                                    // --------------------
            forwardRun();                           // train
            backwardRun();                          // train
            weightsUpdate();                        // train

            double J1=fabs(dv(0) - y(FO));      // compute the error
            double J2=fabs(dv(1) - y(LO));      // compute the error

            error1 += J1;
            error2 += J2;
            sumData++;
                                                    // --------------------
            if (epoch % 100==0) {				    // every 20000 ep. print error
                if (p == 0 && epoch != 0) {
                    printf("\n");
                    printf("\n%f %f\n",error1,error2);
                }          // --------------------
                    		            // --------------------
                //forwardRun();                       // runs network
                //if (p % 20 == 0) 
                printf("%5d: %f %f (%.6f) ::: %f %f (%.6f)\n",epoch,y(FO),dv(0),J1,y(LO),dv(1),J2);
            }
        }

        fprintf(con1,"%f ",error1 / tPoints);
        fprintf(con2,"%f ",error2 / tPoints);

        if ((error1 / tPoints < 0.005) && (error2 / tPoints < 0.005)) {
            printf("%5d: %f %f\n", epoch, error1 / sumData, error2 / sumData);
            break;
        }
    }
    fprintf(con1,"\n%d ",epoch);
    fprintf(con2,"\n%d ",epoch);

    fclose(con1);
    fclose(con2);
    fclose(trainingSet);
    t = clock() - t;                 // stop timer
    sec = ((float)t)/CLOCKS_PER_SEC; // conversion to seconds
    
    //printf("--------------------------------\n%.3f sec\n\n",sec);
    weightsToFileHelper();
    //printf("%.3f ",sec);
    printf("%d",tPoints);
    return 0;
}
示例#7
0
文件: lzw.c 项目: krishpop/LZW
// decodes using lzw algorithm
void decode() {
    int code, newcode;
    int maxbits = getFlags(2);
    int maxcodes = (1 << maxbits);

    int p = getFlags(1);
    int e = getFlags(1);

    Trie st;
    createT(&st, e);

    int bitCount = (e) ? 2 : 9;
    int codeCount = (e) ? 3 : 259;

    int oldc = EMPTY;
    bool kwk = false;
    char baseK;
    int pruneCount=0, kwkcount=0;

    while((newcode=code=getBits(bitCount))!=EOFILE) {

        // under these conditions, a valid prune can occur
        if(p && code==EMPTY) {
            pruneCount++;

            Trie newst;
            createT(&newst, e);
            int oldCodeCount=codeCount;
            codeCount=pruneT(&st, &newst, e, oldCodeCount);
            destroyT(&st, oldCodeCount);
            st=newst;
            bitCount=codeLength(codeCount);
            oldc=EMPTY;
            continue;
        }

        if(newcode>=codeCount+1) {
            ERROR("code impossible to decode\n");
        }
        // read an escaped character
        else if(e && code==ESC) {
            if(tableFilled(codeCount+1)&&bitCount!=codeLength(codeCount+1)) {
                if(bitCount<maxbits) {
                    bitCount++;
                }
            }
            code=getBits(8);
            baseK=(char)code;
            putchar(baseK);
            if(codeCount<maxcodes) {
                if(tableFilled(codeCount+1)) {
                    expandT(&st, (codeCount)*2);
                }
                addT(&st, oldc, code, codeCount);
                codeCount++;
                // then we need to add the char k as it's own code
                if(oldc!=EMPTY) {
                    if(tableFilled(codeCount+1)) {
                        expandT(&st, (codeCount)*2);
                    }
                    addT(&st, EMPTY, code, codeCount);
                    codeCount++;
                    if(tableFilled(codeCount)&&bitCount!=codeLength(codeCount)) {
                        if(bitCount<maxbits) {
                            bitCount++;
                        }
                    }
                }
            }
            oldc=EMPTY;
        }

        else { // no escape character called, would read c and k normally
            if(newcode==codeCount) {
                kwk=true;
                code=oldc; // omega, need to print k after
            }
            baseK=outputCode(&st, code, true);
            if(kwk) {
                putchar(baseK);
                kwkcount++;
            }
            // oldc is empty on the first read, and when the e-flag is present
            // oldc is zero when the last character read was escaped
            if(oldc!=EMPTY) {
                if(codeCount<maxcodes) {
                    if(tableFilled(codeCount+1)) {
                        expandT(&st, (codeCount)*2);
                    }
                    addT(&st, oldc, (int)baseK, codeCount);
                    codeCount++;
                    if(kwk) {
                        // we added kwk after seeing it once already in the prev
                        // scan through so we should increase its number of apps
                        st[newcode].appearances++;
                        // this scenario means we have kkk, without w in between
                        // so
                        if(st[st[oldc].prefix].prefix==EMPTY&&kwkcount==1) {
                            st[oldc].appearances--;
                        }
                        kwk=false;
                    }
                    if(tableFilled(codeCount+1)) {
                        if(bitCount<maxbits&&bitCount!=codeLength(codeCount+1)) {
                            bitCount++;
                        }
                    }
                }
            } else if(e) {
                // if e-flag & last char was excaped, increase bit count if
                // table is filled now
                if(tableFilled(codeCount+1)) {
                    if(bitCount<maxbits) {
                        bitCount++;
                    }
                }
            }
            oldc = newcode;
        }
    }
    destroyT(&st, codeCount);
}