コード例 #1
0
ファイル: Struct.cpp プロジェクト: DmitriyVolkov/TCHMK2
BigInt operator % (BigInt a, BigInt b)
{
	BigInt residue; 
	if ((a> b) < 0)  
		return a;
	if (b.size == 1) 
	{
		if (b.index[0] == 0) 
		{
			residue.size = 0;
			residue.index = NULL;
			return residue;
		}
		BigInt res = div(a, b.index[0]);
		residue = a - mul(res, b.index[0]);
		return residue;
	}

	residue.size = a.size;
	residue = memory_alloc(residue, residue.size);
	memcpy(residue.index, a.index, residue.size * sizeof(unsigned int));
	unsigned int i;

	for (i = a.size - b.size + 1; i != 0; i--)
	{
		unsigned long long int assume_max = BASE; 
		unsigned long long int assume_min = 0;
		unsigned long long int assume;

		while (assume_max - assume_min > 1)
		{
			assume = (assume_max + assume_min) / 2;
			BigInt temp = mul(b, assume);
			temp = shift(temp, i - 1);   
			if ((temp> residue) > 0)
				assume_max = assume;
			else
				assume_min = assume;
			temp = freeindex(temp);
		}
		BigInt temp = mul(b, assume_min);
		temp = shift(temp, i - 1);
		residue = residue - temp;
		temp = freeindex(temp);
	}
	residue = control(residue);
	return residue;
}
コード例 #2
0
ファイル: Struct.cpp プロジェクト: DmitriyVolkov/TCHMK2
BigInt deg(BigInt a, BigInt b, BigInt c)
{
	BigInt Res;
	if (a.size < c.size)
		Res.size = c.size + c.size;
	else
		Res.size = a.size + a.size;

	Res = memory_alloc(Res, Res.size);
	Res = init(Res, Res.size); 
	Res.index[0] = 1;

	BigInt com;
	com.size = 1;
	com = memory_alloc(com, com.size);
	com.index[0] = 0;

	BigInt value;
	value.size = Res.size;
	value = memory_alloc(value, value.size);
	value = init(value, value.size); 
	memcpy(value.index, a.index, a.size * sizeof(unsigned int));
	BigInt pow;
	pow.size = b.size;
	pow = memory_alloc(pow, pow.size);
	memcpy(pow.index, b.index, pow.size * sizeof(unsigned int));
	while ((pow>com) > 0)
	{
		if ((pow.index[0] & 1) == 1)
		{
			Res = Res * value;
			Res = Res % c;
		}
		value = value * value;
		value = value % c;
		pow = div(pow, 2);
	}
	com = freeindex(com);
	pow = freeindex(pow);
	value = freeindex(value);
	return Res;
}
コード例 #3
0
static void freeindex ( struct flagptr * indexp, int depth )
{
   int i;

   for ( i = 0; i < SET_SIZE + hashheader.nstrchars; i++, indexp++ ) {
      if ( indexp->numents == 0 && indexp->pu.fp != NULL ) {
         freeindex ( indexp->pu.fp, depth + 1 );
         free ( indexp->pu.fp );
         indexp->pu.fp = 0;
      }                         /* endif */
   }                            /* endfor */
}
コード例 #4
0
ファイル: Struct.cpp プロジェクト: DmitriyVolkov/TCHMK2
BigInt ReadText(const char* filename)
{
	BigInt bin, number;
	char ch;
	number.size = 0;
	FILE *in = fopen(filename, "r");
	fseek(in, 0, SEEK_END);
	number.size = ftell(in);
	fseek(in, SEEK_SET, 0);  //переход на начало строки
	number = memory_alloc(number, number.size);
	number = init(number, number.size);

	bin.size = number.size / 9 + 1;
	bin = memory_alloc(bin, bin.size);
	bin = init(bin, bin.size);
	unsigned int a;
	unsigned int flag = 0;
	unsigned int temp;
	unsigned int cur; 
	unsigned int j;
	unsigned int x;
	long long int i = number.size - 1;

	while ((ch = getc(in)) != EOF)
		number.index[i--] = ch - '0';
	fclose(in);

	cur = 1;
	j = 0;
	x = 0;
	while (number.size != 1 || number.index[0] != 0)
	{
		flag = 0;
		for (i = number.size - 1; i >= 0; i--)
		{
			temp = flag * 10 + number.index[i];
			number.index[i] = temp / 2;
			flag = temp - number.index[i] * 2;
		}
		number = control(number);
		bin.index[j] = ((cur << x) * flag) | bin.index[j];
		x++;
		if (x == 32)
		{
			x = 0;
			j++;
		}
	}
	number = freeindex(number);
	bin = control(bin);
	return bin;
}
コード例 #5
0
void lclose ( void )
{
   if ( hashtbl ) {
      free ( hashtbl );
      hashtbl = 0;
   }                            /* endif */
   if ( hashstrings ) {
      free ( hashstrings );
      hashstrings = 0;
   }                            /* endif */
   if ( sflaglist ) {
      free ( sflaglist );
      sflaglist = 0;
   }                            /* endif */
   if ( chartypes ) {
      free ( chartypes );
      chartypes = 0;
   }                            /* endif */
   freeindex ( pflagindex, 0 );
   memset ( pflagindex, 0, sizeof ( pflagindex ) );
   freeindex ( sflagindex, 0 );
   memset ( sflagindex, 0, sizeof ( sflagindex ) );
   inited = 0;
}
コード例 #6
0
ファイル: index.c プロジェクト: 99years/plan9
Index*
initindex(char *name, ISect **sects, int n)
{
	IFile f;
	Index *ix;
	ISect *is;
	u32int last, blocksize, tabsize;
	int i;

	if(n <= 0){
fprint(2, "bad n\n");
		seterr(EOk, "no index sections to initialize index");
		return nil;
	}
	ix = MKZ(Index);
	if(ix == nil){
fprint(2, "no mem\n");
		seterr(EOk, "can't initialize index: out of memory");
		freeindex(ix);
		return nil;
	}

	tabsize = sects[0]->tabsize;
	if(partifile(&f, sects[0]->part, sects[0]->tabbase, tabsize) < 0)
		return nil;
	if(parseindex(&f, ix) < 0){
		freeifile(&f);
		freeindex(ix);
		return nil;
	}
	freeifile(&f);
	if(namecmp(ix->name, name) != 0){
		seterr(ECorrupt, "mismatched index name: found %s expected %s", ix->name, name);
		return nil;
	}
	if(ix->nsects != n){
		seterr(ECorrupt, "mismatched number index sections: found %d expected %d", n, ix->nsects);
		freeindex(ix);
		return nil;
	}
	ix->sects = sects;
	last = 0;
	blocksize = ix->blocksize;
	for(i = 0; i < ix->nsects; i++){
		is = sects[i];
		if(namecmp(ix->name, is->index) != 0
		|| is->blocksize != blocksize
		|| is->tabsize != tabsize
		|| namecmp(is->name, ix->smap[i].name) != 0
		|| is->start != ix->smap[i].start
		|| is->stop != ix->smap[i].stop
		|| last != is->start
		|| is->start > is->stop){
			seterr(ECorrupt, "inconsistent index sections in %s", ix->name);
			freeindex(ix);
			return nil;
		}
		last = is->stop;
	}
	ix->tabsize = tabsize;
	ix->buckets = last;

	if(initindex1(ix) < 0){
		freeindex(ix);
		return nil;
	}

	ix->arenas = MKNZ(Arena*, ix->narenas);
	if(maparenas(ix->amap, ix->arenas, ix->narenas, ix->name) < 0){
		freeindex(ix);
		return nil;
	}

	return ix;
}
コード例 #7
0
ファイル: Struct.cpp プロジェクト: DmitriyVolkov/TCHMK2
BigInt operator /(BigInt a, BigInt b)
{
	BigInt residue;
	if ((a>b) < 0)  
	{
		BigInt Res;
		Res.size = 1;
		Res = memory_alloc(Res, Res.size);
		Res.index[0] = 0;
		return Res;
	}
		if ((a>b) == 0)  
	{
		BigInt Res;
		Res.size = 1;
		Res = memory_alloc(Res, Res.size);
		Res.index[0] = 1;
		return Res;
	}
	if (b.size == 1) 
	{
		if (b.index[0] == 0)
		{
			residue.size = 0;
			residue.index = NULL;
			return residue;
		}
		BigInt Res = div(a, b.index[0]);
		return Res;
	}

	residue.size = a.size;
	residue = memory_alloc(residue, residue.size);
	memcpy(residue.index, a.index, residue.size * sizeof(unsigned int));

	BigInt Res;
	Res.size = a.size - b.size + 1;
	Res = memory_alloc(Res, Res.size);
	Res = init(Res, Res.size); //заполняет массив нулями
	unsigned int i;

	for (i = a.size - b.size + 1; i != 0; i--)
	{
		unsigned long long int assume_max = BASE;
		unsigned long long int assume_min = 0;
		unsigned long long int assume;
		
		while (assume_max - assume_min > 1)
		{
			assume = (assume_max + assume_min) / 2;
			BigInt temp = mul(b, assume);
			temp = shift(temp, i - 1);    // сдвигает число на (i - 1) разрядов влево
			if ((temp> residue) > 0)
				assume_max = assume;
			else
				assume_min = assume;
			temp = freeindex(temp);
		}
		Res.index[i - 1] = assume_min;
	}
	Res = control(Res);

	return Res;
}