/* Generated by Nim Compiler v0.10.2 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <string.h>
typedef struct intset223056 intset223056;
typedef struct ttrunk223052 ttrunk223052;
typedef struct ttrunkseq223054 ttrunkseq223054;
typedef struct TGenericSeq TGenericSeq;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tcell44933 tcell44933;
typedef struct tcellset44945 tcellset44945;
typedef struct tgcheap47016 tgcheap47016;
typedef struct tcellseq44949 tcellseq44949;
typedef struct tpagedesc44941 tpagedesc44941;
typedef struct tmemregion27210 tmemregion27210;
typedef struct tsmallchunk26440 tsmallchunk26440;
typedef struct tllchunk27204 tllchunk27204;
typedef struct tbigchunk26442 tbigchunk26442;
typedef struct tintset26417 tintset26417;
typedef struct ttrunk26413 ttrunk26413;
typedef struct tavlnode27208 tavlnode27208;
typedef struct tgcstat47014 tgcstat47014;
typedef struct tbasechunk26438 tbasechunk26438;
typedef struct tfreecell26430 tfreecell26430;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  intset223056  {
NI Counter;
NI Max;
ttrunk223052* Head;
ttrunkseq223054* Data;
};
typedef N_NIMCALL_PTR(void, TY3089) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3094) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3089 marker;
TY3094 deepcopy;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NI TY26420[16];
struct  ttrunk223052  {
ttrunk223052* Next;
NI Key;
TY26420 Bits;
};
struct  tcell44933  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq44949  {
NI Len;
NI Cap;
tcell44933** D;
};
struct  tcellset44945  {
NI Counter;
NI Max;
tpagedesc44941* Head;
tpagedesc44941** Data;
};
typedef tsmallchunk26440* TY27222[512];
typedef ttrunk26413* ttrunkbuckets26415[256];
struct  tintset26417  {
ttrunkbuckets26415 Data;
};
struct  tmemregion27210  {
NI Minlargeobj;
NI Maxlargeobj;
TY27222 Freesmallchunks;
tllchunk27204* Llmem;
NI Currmem;
NI Maxmem;
NI Freemem;
NI Lastsize;
tbigchunk26442* Freechunkslist;
tintset26417 Chunkstarts;
tavlnode27208* Root;
tavlnode27208* Deleted;
tavlnode27208* Last;
tavlnode27208* Freeavlnodes;
};
struct  tgcstat47014  {
NI Stackscans;
NI Cyclecollections;
NI Maxthreshold;
NI Maxstacksize;
NI Maxstackcells;
NI Cycletablesize;
NI64 Maxpause;
};
struct  tgcheap47016  {
void* Stackbottom;
NI Cyclethreshold;
tcellseq44949 Zct;
tcellseq44949 Decstack;
tcellset44945 Cycleroots;
tcellseq44949 Tempstack;
NI Recgclock;
tmemregion27210 Region;
tgcstat47014 Stat;
};
struct  tpagedesc44941  {
tpagedesc44941* Next;
NI Key;
TY26420 Bits;
};
struct  tbasechunk26438  {
NI Prevsize;
NI Size;
NIM_BOOL Used;
};
struct  tsmallchunk26440  {
  tbasechunk26438 Sup;
tsmallchunk26440* Next;
tsmallchunk26440* Prev;
tfreecell26430* Freelist;
NI Free;
NI Acc;
NF Data;
};
struct  tllchunk27204  {
NI Size;
NI Acc;
tllchunk27204* Next;
};
struct  tbigchunk26442  {
  tbasechunk26438 Sup;
tbigchunk26442* Next;
tbigchunk26442* Prev;
NI Align;
NF Data;
};
struct  ttrunk26413  {
ttrunk26413* Next;
NI Key;
TY26420 Bits;
};
typedef tavlnode27208* TY27214[2];
struct  tavlnode27208  {
TY27214 Link;
NI Key;
NI Upperbound;
NI Level;
};
struct  tfreecell26430  {
tfreecell26430* Next;
NI Zerofield;
};
struct ttrunkseq223054 {
  TGenericSeq Sup;
  ttrunk223052* data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP968)(void* p, NI op);
N_NIMCALL(void, TMP969)(void* p, NI op);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(ttrunk223052*, intsetget_223220)(intset223056 t, NI key);
static N_INLINE(NI, nexttry_223209)(NI h, NI maxhash);
N_NIMCALL(void, incl_223869)(intset223056* s, NI key);
N_NIMCALL(ttrunk223052*, intsetput_223703)(intset223056* t, NI key);
static N_INLINE(NIM_BOOL, mustrehash_223073)(NI length, NI counter);
N_NIMCALL(void, intsetenlarge_223614)(intset223056* t);
N_NIMCALL(void, intsetrawinsert_223256)(intset223056 t, ttrunkseq223054** data, ttrunk223052* desc);
static N_INLINE(void, asgnRef)(void** dest, void* src);
static N_INLINE(void, incref_51025)(tcell44933* c);
static N_INLINE(NIM_BOOL, canbecycleroot_48667)(tcell44933* c);
static N_INLINE(void, rtladdcycleroot_49429)(tcell44933* c);
N_NOINLINE(void, incl_45671)(tcellset44945* s, tcell44933* cell);
static N_INLINE(tcell44933*, usrtocell_48646)(void* usr);
static N_INLINE(void, decref_50604)(tcell44933* c);
static N_INLINE(void, rtladdzct_50204)(tcell44933* c);
N_NOINLINE(void, addzct_48617)(tcellseq44949* s, tcell44933* c);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
TNimType NTI223056; /* IntSet */
extern TNimType NTI108; /* int */
TNimType NTI223052; /* TTrunk */
extern TNimType NTI26420; /* array[0..15, int] */
TNimType NTI223050; /* PTrunk */
TNimType NTI223054; /* TTrunkSeq */
extern tgcheap47016 gch_47044;
N_NIMCALL(void, TMP968)(void* p, NI op) {
	ttrunk223052* a;
	NI LOC1;
	a = (ttrunk223052*)p;
	nimGCvisit((void*)(*a).Next, op);
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 16; LOC1++) {
	}
}
N_NIMCALL(void, TMP969)(void* p, NI op) {
	ttrunkseq223054* a;
	NI LOC1;
	a = (ttrunkseq223054*)p;
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < a->Sup.len; LOC1++) {
	nimGCvisit((void*)a->data[LOC1], op);
	}
}

N_NIMCALL(void, initintset_223950)(intset223056* Result) {
	unsureAsgnRef((void**) (&(*Result).Data), (ttrunkseq223054*) newSeq((&NTI223054), 8));
	(*Result).Max = 7;
	(*Result).Counter = 0;
	unsureAsgnRef((void**) (&(*Result).Head), NIM_NIL);
}

static N_INLINE(NI, nexttry_223209)(NI h, NI maxhash) {
	NI result;
	result = 0;
	result = (NI)((NI32)((NI32)(5 * h) + 1) & maxhash);
	return result;
}

N_NIMCALL(ttrunk223052*, intsetget_223220)(intset223056 t, NI key) {
	ttrunk223052* result;
	NI h;
	result = 0;
	h = (NI)(key & t.Max);
	{
		while (1) {
			if (!!((t.Data->data[h] == NIM_NIL))) goto LA2;
			{
				if (!((*t.Data->data[h]).Key == key)) goto LA5;
				result = t.Data->data[h];
				goto BeforeRet;
			}
			LA5: ;
			h = nexttry_223209(h, t.Max);
		} LA2: ;
	}
	result = NIM_NIL;
	BeforeRet: ;
	return result;
}

static N_INLINE(NIM_BOOL, mustrehash_223073)(NI length, NI counter) {
	NIM_BOOL result;
	NIM_BOOL LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = ((NI32)(length * 2) < (NI32)(counter * 3));
	if (LOC1) goto LA2;
	LOC1 = ((NI32)(length - counter) < 4);
	LA2: ;
	result = LOC1;
	return result;
}

static N_INLINE(NIM_BOOL, canbecycleroot_48667)(tcell44933* c) {
	NIM_BOOL result;
	result = 0;
	result = !((((*(*c).Typ).flags &(1<<((((NU8) 1))&7)))!=0));
	return result;
}

static N_INLINE(void, rtladdcycleroot_49429)(tcell44933* c) {
	{
		if (!!((((NI) ((NI)((*c).Refcount & 3))) == 3))) goto LA3;
		(*c).Refcount = (NI)((NI)((*c).Refcount & -4) | 3);
		incl_45671((&gch_47044.Cycleroots), c);
	}
	LA3: ;
}

static N_INLINE(void, incref_51025)(tcell44933* c) {
	(*c).Refcount = (NI)((NU32)((*c).Refcount) + (NU32)(8));
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = canbecycleroot_48667(c);
		if (!LOC3) goto LA4;
		rtladdcycleroot_49429(c);
	}
	LA4: ;
}

static N_INLINE(tcell44933*, usrtocell_48646)(void* usr) {
	tcell44933* result;
	result = 0;
	result = ((tcell44933*) ((NI)((NU32)(((NI) (usr))) - (NU32)(((NI)sizeof(tcell44933))))));
	return result;
}

static N_INLINE(void, rtladdzct_50204)(tcell44933* c) {
	addzct_48617((&gch_47044.Zct), c);
}

static N_INLINE(void, decref_50604)(tcell44933* c) {
	{
		(*c).Refcount -= 8;
		if (!((NU32)((*c).Refcount) < (NU32)(8))) goto LA3;
		rtladdzct_50204(c);
	}
	goto LA1;
	LA3: ;
	{
		NIM_BOOL LOC6;
		LOC6 = 0;
		LOC6 = canbecycleroot_48667(c);
		if (!LOC6) goto LA7;
		rtladdcycleroot_49429(c);
	}
	goto LA1;
	LA7: ;
	LA1: ;
}

static N_INLINE(void, asgnRef)(void** dest, void* src) {
	{
		tcell44933* LOC5;
		if (!!((src == NIM_NIL))) goto LA3;
		LOC5 = 0;
		LOC5 = usrtocell_48646(src);
		incref_51025(LOC5);
	}
	LA3: ;
	{
		tcell44933* LOC10;
		if (!!(((*dest) == NIM_NIL))) goto LA8;
		LOC10 = 0;
		LOC10 = usrtocell_48646((*dest));
		decref_50604(LOC10);
	}
	LA8: ;
	(*dest) = src;
}

N_NIMCALL(void, intsetrawinsert_223256)(intset223056 t, ttrunkseq223054** data, ttrunk223052* desc) {
	NI h;
	h = (NI)((*desc).Key & t.Max);
	{
		while (1) {
			if (!!(((*data)->data[h] == NIM_NIL))) goto LA2;
			h = nexttry_223209(h, t.Max);
		} LA2: ;
	}
	asgnRef((void**) (&(*data)->data[h]), desc);
}

N_NIMCALL(void, intsetenlarge_223614)(intset223056* t) {
	ttrunkseq223054* n;
	NI oldmax;
	ttrunkseq223054* LOC8;
	n = 0;
	oldmax = (*t).Max;
	(*t).Max = (NI32)((NI32)((NI32)((*t).Max + 1) * 2) - 1);
	n = (ttrunkseq223054*) newSeq((&NTI223054), (NI32)((*t).Max + 1));
	{
		NI i_223648;
		NI res_223686;
		i_223648 = 0;
		res_223686 = 0;
		{
			while (1) {
				if (!(res_223686 <= oldmax)) goto LA3;
				i_223648 = res_223686;
				{
					if (!!(((*t).Data->data[i_223648] == NIM_NIL))) goto LA6;
					intsetrawinsert_223256((*t), (&n), (*t).Data->data[i_223648]);
				}
				LA6: ;
				res_223686 += 1;
			} LA3: ;
		}
	}
	LOC8 = 0;
	LOC8 = (*t).Data;
	unsureAsgnRef((void**) (&(*t).Data), n);
	n = LOC8;
}

N_NIMCALL(ttrunk223052*, intsetput_223703)(intset223056* t, NI key) {
	ttrunk223052* result;
	NI h;
	result = 0;
	h = (NI)(key & (*t).Max);
	{
		while (1) {
			if (!!(((*t).Data->data[h] == NIM_NIL))) goto LA2;
			{
				if (!((*(*t).Data->data[h]).Key == key)) goto LA5;
				result = (*t).Data->data[h];
				goto BeforeRet;
			}
			LA5: ;
			h = nexttry_223209(h, (*t).Max);
		} LA2: ;
	}
	{
		NIM_BOOL LOC9;
		LOC9 = 0;
		LOC9 = mustrehash_223073((NI32)((*t).Max + 1), (*t).Counter);
		if (!LOC9) goto LA10;
		intsetenlarge_223614(t);
	}
	LA10: ;
	(*t).Counter += 1;
	h = (NI)(key & (*t).Max);
	{
		while (1) {
			if (!!(((*t).Data->data[h] == NIM_NIL))) goto LA13;
			h = nexttry_223209(h, (*t).Max);
		} LA13: ;
	}
	result = (ttrunk223052*) newObj((&NTI223050), sizeof(ttrunk223052));
	asgnRef((void**) (&(*result).Next), (*t).Head);
	(*result).Key = key;
	unsureAsgnRef((void**) (&(*t).Head), result);
	asgnRef((void**) (&(*t).Data->data[h]), result);
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, incl_223869)(intset223056* s, NI key) {
	ttrunk223052* t;
	NI u;
	t = intsetput_223703(s, (NI)((NU32)(key) >> (NU32)(9)));
	u = (NI)(key & 511);
	(*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] = (NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] | (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31))))));
}

N_NIMCALL(NIM_BOOL, containsorincl_223916)(intset223056* s, NI key) {
	NIM_BOOL result;
	ttrunk223052* t;
	result = 0;
	t = intsetget_223220((*s), (NI)((NU32)(key) >> (NU32)(9)));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & 511);
		result = !(((NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] & (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31)))))) == 0));
		{
			if (!!(result)) goto LA7;
			(*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] = (NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] | (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31))))));
		}
		LA7: ;
	}
	goto LA1;
	LA3: ;
	{
		incl_223869(s, key);
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(NIM_BOOL, contains_223837)(intset223056 s, NI key) {
	NIM_BOOL result;
	ttrunk223052* t;
	result = 0;
	t = intsetget_223220(s, (NI)((NU32)(key) >> (NU32)(9)));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & 511);
		result = !(((NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] & (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31)))))) == 0));
	}
	goto LA1;
	LA3: ;
	{
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, excl_223884)(intset223056* s, NI key) {
	ttrunk223052* t;
	t = intsetget_223220((*s), (NI)((NU32)(key) >> (NU32)(9)));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & 511);
		(*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] = (NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] & (NI)((NU32) ~((NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31))))))));
	}
	LA3: ;
}

N_NIMCALL(void, assign_223975)(intset223056* dest, intset223056 src) {
	ttrunk223052* it;
	(*dest).Counter = src.Counter;
	(*dest).Max = src.Max;
	unsureAsgnRef((void**) (&(*dest).Data), (ttrunkseq223054*) newSeq((&NTI223054), src.Data->Sup.len));
	it = src.Head;
	{
		while (1) {
			NI h;
			ttrunk223052* n;
			if (!!((it == NIM_NIL))) goto LA2;
			h = (NI)((*it).Key & (*dest).Max);
			{
				while (1) {
					if (!!(((*dest).Data->data[h] == NIM_NIL))) goto LA4;
					h = nexttry_223209(h, (*dest).Max);
				} LA4: ;
			}
			n = 0;
			n = (ttrunk223052*) newObj((&NTI223050), sizeof(ttrunk223052));
			asgnRef((void**) (&(*n).Next), (*dest).Head);
			(*n).Key = (*it).Key;
			memcpy((void*)(*n).Bits, (NIM_CONST void*)(*it).Bits, sizeof((*n).Bits));
			unsureAsgnRef((void**) (&(*dest).Head), n);
			asgnRef((void**) (&(*dest).Data->data[h]), n);
			it = (*it).Next;
		} LA2: ;
	}
}
NIM_EXTERNC N_NOINLINE(void, stdlib_intsetsInit)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_intsetsDatInit)(void) {
static TNimNode* TMP966[4];
static TNimNode* TMP967[3];
static TNimNode TMP725[9];
NTI223056.size = sizeof(intset223056);
NTI223056.kind = 18;
NTI223056.base = 0;
NTI223056.flags = 2;
TMP966[0] = &TMP725[1];
TMP725[1].kind = 1;
TMP725[1].offset = offsetof(intset223056, Counter);
TMP725[1].typ = (&NTI108);
TMP725[1].name = "counter";
TMP966[1] = &TMP725[2];
TMP725[2].kind = 1;
TMP725[2].offset = offsetof(intset223056, Max);
TMP725[2].typ = (&NTI108);
TMP725[2].name = "max";
TMP966[2] = &TMP725[3];
NTI223052.size = sizeof(ttrunk223052);
NTI223052.kind = 18;
NTI223052.base = 0;
TMP967[0] = &TMP725[5];
TMP725[5].kind = 1;
TMP725[5].offset = offsetof(ttrunk223052, Next);
TMP725[5].typ = (&NTI223050);
TMP725[5].name = "next";
TMP967[1] = &TMP725[6];
TMP725[6].kind = 1;
TMP725[6].offset = offsetof(ttrunk223052, Key);
TMP725[6].typ = (&NTI108);
TMP725[6].name = "key";
TMP967[2] = &TMP725[7];
TMP725[7].kind = 1;
TMP725[7].offset = offsetof(ttrunk223052, Bits);
TMP725[7].typ = (&NTI26420);
TMP725[7].name = "bits";
TMP725[4].len = 3; TMP725[4].kind = 2; TMP725[4].sons = &TMP967[0];
NTI223052.node = &TMP725[4];
NTI223050.size = sizeof(ttrunk223052*);
NTI223050.kind = 22;
NTI223050.base = (&NTI223052);
NTI223050.marker = TMP968;
TMP725[3].kind = 1;
TMP725[3].offset = offsetof(intset223056, Head);
TMP725[3].typ = (&NTI223050);
TMP725[3].name = "head";
TMP966[3] = &TMP725[8];
NTI223054.size = sizeof(ttrunkseq223054*);
NTI223054.kind = 24;
NTI223054.base = (&NTI223050);
NTI223054.flags = 2;
NTI223054.marker = TMP969;
TMP725[8].kind = 1;
TMP725[8].offset = offsetof(intset223056, Data);
TMP725[8].typ = (&NTI223054);
TMP725[8].name = "data";
TMP725[0].len = 4; TMP725[0].kind = 2; TMP725[0].sons = &TMP966[0];
NTI223056.node = &TMP725[0];
}

