예제 #1
0
void test4() {
  int i = findArray(n, a, 8.5, 10);
  printf("findArray=%d\n", i);
}
예제 #2
0
파일: lzw.c 프로젝트: g-mainak/LZW
int decode()
{
	int k;
	Hash *h = malloc(sizeof(Hash));
	Stack *kStack = NULL;
	int oldC = EMPTY;
	int newC, C;
	char finalK;
	//freopen("b.txt", "r", stdin);
	int args[NUMARGS];
	scanf("%d %d %d|", args+1, args+2, args+3);
	int size = 1 << (args[1]+1);
	unsigned int time =1;
	initialize(h, size, args[3], time);
	int bits = (args[3])?3:9;
	while((newC = C = getBits(bits)) != EOF)
	{
		if(C==INCR)
		{
			bits++;
			continue;
		}
		if(C == ESCAPE) //Time to add a new sequence
		{
			newC = C = getBits(CHAR_BIT);
			if(h->numElements< ( 1 << (args[1])))
				insert(h, EMPTY, (char)C, 1);
			oldC = EMPTY;
			putchar(C);
			continue;
		}
		if(C == RESET)
		{
			Element *e  = findArray(h, getBits(bits));
			if (e && e->ch)
			{
				putchar(e->ch);
				finalK=e->ch;
			}
			prune(args, &h, time);
			//bitchange
			oldC= EMPTY;
			continue;
		}

		if(findArray(h, C)==EMPTY)
		{
			push(&kStack, finalK);
			C = oldC;
		}
		else
		{
			Element *elem = findArray(h, C);
			elem->time = time++;
		}

		Element *elem = findArray(h, C);
		while (elem && elem->prefix!=EMPTY)
		{
			push(&kStack, elem->ch);
			C = elem->prefix;
			elem = findArray(h, C);
		}

		finalK = elem->ch;
		putchar(finalK);

		while(kStack)
		{
			k = pop(&kStack);
			putchar(k);
		}

		if (oldC != EMPTY && h->numElements < ( 1 << (args[1])))
			insert(h, oldC, finalK, 1);
		oldC = newC;
	}
	//printArray(arr);
	free(h);
	return 0;
}