/* Generated by Nim Compiler v0.10.2 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 64
#include "nimbase.h"
typedef struct stringtableobj130012 stringtableobj130012;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct keyvaluepairseq130010 keyvaluepairseq130010;
typedef struct keyvaluepair130008 keyvaluepair130008;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct tcell44935 tcell44935;
typedef struct tcellseq44951 tcellseq44951;
typedef struct tgcheap47016 tgcheap47016;
typedef struct tcellset44947 tcellset44947;
typedef struct tpagedesc44943 tpagedesc44943;
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;
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  TNimObject  {
TNimType* m_type;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct keyvaluepair130008 {
NimStringDesc* Field0;
NimStringDesc* Field1;
};
struct  stringtableobj130012  {
  TNimObject Sup;
NI Counter;
keyvaluepairseq130010* Data;
NU8 Mode;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  tcell44935  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq44951  {
NI Len;
NI Cap;
tcell44935** D;
};
struct  tcellset44947  {
NI Counter;
NI Max;
tpagedesc44943* Head;
tpagedesc44943** 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;
tcellseq44951 Zct;
tcellseq44951 Decstack;
tcellset44947 Cycleroots;
tcellseq44951 Tempstack;
NI Recgclock;
tmemregion27210 Region;
tgcstat47014 Stat;
};
typedef NI TY26420[8];
struct  tpagedesc44943  {
tpagedesc44943* 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 keyvaluepairseq130010 {
  TGenericSeq Sup;
  keyvaluepair130008 data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP42)(void* p, NI op);
N_NIMCALL(void, TMP46)(void* p, NI op);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
static N_INLINE(void, nimGCunrefNoCycle)(void* p);
static N_INLINE(tcell44935*, usrtocell_48646)(void* usr);
static N_INLINE(void, rtladdzct_50204)(tcell44935* c);
N_NOINLINE(void, addzct_48617)(tcellseq44951* s, tcell44935* c);
N_NIMCALL(void*, newSeqRC1)(TNimType* typ, NI len);
N_NIMCALL(NI, rawget_130420)(stringtableobj130012* t, NimStringDesc* key);
N_NIMCALL(NI, myhash_130171)(stringtableobj130012* t, NimStringDesc* key);
N_NIMCALL(NI, hash_127839)(NimStringDesc* x);
N_NIMCALL(NI, hashignorecase_127977)(NimStringDesc* x);
N_NIMCALL(NI, hashignorestyle_127885)(NimStringDesc* x);
N_NIMCALL(NIM_BOOL, mycmp_130182)(stringtableobj130012* t, NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NI, cmp_4517)(NimStringDesc* x, NimStringDesc* y);
N_NIMCALL(NI, nsuCmpIgnoreCase)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NI, nsuCmpIgnoreStyle)(NimStringDesc* a, NimStringDesc* b);
static N_INLINE(NI, nexttry_130409)(NI h, NI maxhash);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(NimStringDesc*, copyStringRC1)(NimStringDesc* src);
N_NIMCALL(NIM_BOOL, mustrehash_130194)(NI length, NI counter);
N_NIMCALL(void, enlarge_130520)(stringtableobj130012* t);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(void, rawinsert_130498)(stringtableobj130012* t, keyvaluepairseq130010** data, NimStringDesc* key, NimStringDesc* val);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
STRING_LITERAL(TMP1358, "", 0);
extern TNimType NTI3211; /* RootObj */
TNimType NTI130012; /* StringTableObj */
extern TNimType NTI108; /* int */
TNimType NTI130008; /* KeyValuePair */
extern TNimType NTI149; /* string */
TNimType NTI130010; /* KeyValuePairSeq */
TNimType NTI130006; /* StringTableMode */
TNimType NTI130014; /* StringTableRef */
extern tgcheap47016 gch_47044;
N_NIMCALL(void, TMP42)(void* p, NI op) {
	keyvaluepairseq130010* a;
	NI LOC1;
	a = (keyvaluepairseq130010*)p;
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < a->Sup.len; LOC1++) {
	nimGCvisit((void*)a->data[LOC1].Field0, op);
	nimGCvisit((void*)a->data[LOC1].Field1, op);
	}
}
N_NIMCALL(void, TMP46)(void* p, NI op) {
	stringtableobj130012* a;
	a = (stringtableobj130012*)p;
	nimGCvisit((void*)(*a).Data, op);
}

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

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

static N_INLINE(void, nimGCunrefNoCycle)(void* p) {
	tcell44935* c;
	c = usrtocell_48646(p);
	{
		(*c).Refcount -= 8;
		if (!((NU64)((*c).Refcount) < (NU64)(8))) goto LA3;
		rtladdzct_50204(c);
	}
	LA3: ;
}

N_NIMCALL(stringtableobj130012*, nstnewStringTable)(NU8 mode) {
	stringtableobj130012* result;
	result = 0;
	result = (stringtableobj130012*) newObj((&NTI130014), sizeof(stringtableobj130012));
	(*result).Sup.m_type = (&NTI130012);
	(*result).Mode = mode;
	(*result).Counter = 0;
	if ((*result).Data) nimGCunrefNoCycle((*result).Data);
	(*result).Data = (keyvaluepairseq130010*) newSeqRC1((&NTI130010), 64);
	return result;
}

N_NIMCALL(NI, myhash_130171)(stringtableobj130012* t, NimStringDesc* key) {
	NI result;
	result = 0;
	switch ((*t).Mode) {
	case ((NU8) 0):
	{
		result = hash_127839(key);
	}
	break;
	case ((NU8) 1):
	{
		result = hashignorecase_127977(key);
	}
	break;
	case ((NU8) 2):
	{
		result = hashignorestyle_127885(key);
	}
	break;
	}
	return result;
}

N_NIMCALL(NIM_BOOL, mycmp_130182)(stringtableobj130012* t, NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	result = 0;
	switch ((*t).Mode) {
	case ((NU8) 0):
	{
		NI LOC2;
		LOC2 = 0;
		LOC2 = cmp_4517(a, b);
		result = (LOC2 == 0);
	}
	break;
	case ((NU8) 1):
	{
		NI LOC4;
		LOC4 = 0;
		LOC4 = nsuCmpIgnoreCase(a, b);
		result = (LOC4 == 0);
	}
	break;
	case ((NU8) 2):
	{
		NI LOC6;
		LOC6 = 0;
		LOC6 = nsuCmpIgnoreStyle(a, b);
		result = (LOC6 == 0);
	}
	break;
	}
	return result;
}

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

N_NIMCALL(NI, rawget_130420)(stringtableobj130012* t, NimStringDesc* key) {
	NI result;
	NI h;
	NI LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = myhash_130171(t, key);
	h = (NI)(LOC1 & ((*t).Data->Sup.len-1));
	{
		while (1) {
			if (!!((*t).Data->data[h].Field0 == 0)) goto LA3;
			{
				NIM_BOOL LOC6;
				LOC6 = 0;
				LOC6 = mycmp_130182(t, (*t).Data->data[h].Field0, key);
				if (!LOC6) goto LA7;
				result = h;
				goto BeforeRet;
			}
			LA7: ;
			h = nexttry_130409(h, ((*t).Data->Sup.len-1));
		} LA3: ;
	}
	result = -1;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NIM_BOOL, nsthasKey)(stringtableobj130012* t, NimStringDesc* key) {
	NIM_BOOL result;
	NI LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rawget_130420(t, key);
	result = (0 <= LOC1);
	return result;
}

N_NIMCALL(NimStringDesc*, nstGet)(stringtableobj130012* t, NimStringDesc* key) {
	NimStringDesc* result;
	NI index;
	result = 0;
	index = rawget_130420(t, key);
	{
		if (!(0 <= index)) goto LA3;
		result = copyString((*t).Data->data[index].Field1);
	}
	goto LA1;
	LA3: ;
	{
		result = copyString(((NimStringDesc*) &TMP1358));
	}
	LA1: ;
	return result;
}

N_NIMCALL(NIM_BOOL, mustrehash_130194)(NI length, NI counter) {
	NIM_BOOL result;
	NIM_BOOL LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = ((NI64)(length * 2) < (NI64)(counter * 3));
	if (LOC1) goto LA2;
	LOC1 = ((NI64)(length - counter) < 4);
	LA2: ;
	result = LOC1;
	return result;
}

N_NIMCALL(void, rawinsert_130498)(stringtableobj130012* t, keyvaluepairseq130010** data, NimStringDesc* key, NimStringDesc* val) {
	NI h;
	NI LOC1;
	NimStringDesc* LOC4;
	NimStringDesc* LOC5;
	LOC1 = 0;
	LOC1 = myhash_130171(t, key);
	h = (NI)(LOC1 & ((*data)->Sup.len-1));
	{
		while (1) {
			if (!!((*data)->data[h].Field0 == 0)) goto LA3;
			h = nexttry_130409(h, ((*data)->Sup.len-1));
		} LA3: ;
	}
	LOC4 = 0;
	LOC4 = (*data)->data[h].Field0; (*data)->data[h].Field0 = copyStringRC1(key);
	if (LOC4) nimGCunrefNoCycle(LOC4);
	LOC5 = 0;
	LOC5 = (*data)->data[h].Field1; (*data)->data[h].Field1 = copyStringRC1(val);
	if (LOC5) nimGCunrefNoCycle(LOC5);
}

static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src) {
	{
		tcell44935* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_48646(src);
		(*c).Refcount += 8;
	}
	LA3: ;
	{
		tcell44935* c;
		if (!!(((*dest) == NIM_NIL))) goto LA7;
		c = usrtocell_48646((*dest));
		{
			(*c).Refcount -= 8;
			if (!((NU64)((*c).Refcount) < (NU64)(8))) goto LA11;
			rtladdzct_50204(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

N_NIMCALL(void, enlarge_130520)(stringtableobj130012* t) {
	keyvaluepairseq130010* n;
	keyvaluepairseq130010* LOC8;
	n = 0;
	n = (keyvaluepairseq130010*) newSeq((&NTI130010), (NI64)((*t).Data->Sup.len * 2));
	{
		NI i_130563;
		NI HEX3Atmp_130582;
		NI res_130585;
		i_130563 = 0;
		HEX3Atmp_130582 = 0;
		HEX3Atmp_130582 = ((*t).Data->Sup.len-1);
		res_130585 = 0;
		{
			while (1) {
				if (!(res_130585 <= HEX3Atmp_130582)) goto LA3;
				i_130563 = res_130585;
				{
					if (!!((*t).Data->data[i_130563].Field0 == 0)) goto LA6;
					rawinsert_130498(t, (&n), (*t).Data->data[i_130563].Field0, (*t).Data->data[i_130563].Field1);
				}
				LA6: ;
				res_130585 += 1;
			} LA3: ;
		}
	}
	LOC8 = 0;
	LOC8 = (*t).Data;
	asgnRefNoCycle((void**) (&(*t).Data), n);
	n = LOC8;
}

N_NIMCALL(void, nstPut)(stringtableobj130012* t, NimStringDesc* key, NimStringDesc* val) {
	NI index;
	index = rawget_130420(t, key);
	{
		NimStringDesc* LOC5;
		if (!(0 <= index)) goto LA3;
		LOC5 = 0;
		LOC5 = (*t).Data->data[index].Field1; (*t).Data->data[index].Field1 = copyStringRC1(val);
		if (LOC5) nimGCunrefNoCycle(LOC5);
	}
	goto LA1;
	LA3: ;
	{
		{
			NIM_BOOL LOC9;
			LOC9 = 0;
			LOC9 = mustrehash_130194((*t).Data->Sup.len, (*t).Counter);
			if (!LOC9) goto LA10;
			enlarge_130520(t);
		}
		LA10: ;
		rawinsert_130498(t, (&(*t).Data), key, val);
		(*t).Counter += 1;
	}
	LA1: ;
}

N_NIMCALL(NI, nstlen)(stringtableobj130012* t) {
	NI result;
	result = 0;
	result = (*t).Counter;
	return result;
}
NIM_EXTERNC N_NOINLINE(void, stdlib_strtabsInit)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_strtabsDatInit)(void) {
static TNimNode* TMP40[3];
static TNimNode* TMP41[2];
static TNimNode* TMP43[3];
NI TMP45;
static char* NIM_CONST TMP44[3] = {
"modeCaseSensitive", 
"modeCaseInsensitive", 
"modeStyleInsensitive"};
static TNimNode TMP25[11];
NTI130012.size = sizeof(stringtableobj130012);
NTI130012.kind = 17;
NTI130012.base = (&NTI3211);
TMP40[0] = &TMP25[1];
TMP25[1].kind = 1;
TMP25[1].offset = offsetof(stringtableobj130012, Counter);
TMP25[1].typ = (&NTI108);
TMP25[1].name = "counter";
TMP40[1] = &TMP25[2];
NTI130008.size = sizeof(keyvaluepair130008);
NTI130008.kind = 18;
NTI130008.base = 0;
NTI130008.flags = 2;
TMP41[0] = &TMP25[4];
TMP25[4].kind = 1;
TMP25[4].offset = offsetof(keyvaluepair130008, Field0);
TMP25[4].typ = (&NTI149);
TMP25[4].name = "Field0";
TMP41[1] = &TMP25[5];
TMP25[5].kind = 1;
TMP25[5].offset = offsetof(keyvaluepair130008, Field1);
TMP25[5].typ = (&NTI149);
TMP25[5].name = "Field1";
TMP25[3].len = 2; TMP25[3].kind = 2; TMP25[3].sons = &TMP41[0];
NTI130008.node = &TMP25[3];
NTI130010.size = sizeof(keyvaluepairseq130010*);
NTI130010.kind = 24;
NTI130010.base = (&NTI130008);
NTI130010.flags = 2;
NTI130010.marker = TMP42;
TMP25[2].kind = 1;
TMP25[2].offset = offsetof(stringtableobj130012, Data);
TMP25[2].typ = (&NTI130010);
TMP25[2].name = "data";
TMP40[2] = &TMP25[6];
NTI130006.size = sizeof(NU8);
NTI130006.kind = 14;
NTI130006.base = 0;
NTI130006.flags = 3;
for (TMP45 = 0; TMP45 < 3; TMP45++) {
TMP25[TMP45+7].kind = 1;
TMP25[TMP45+7].offset = TMP45;
TMP25[TMP45+7].name = TMP44[TMP45];
TMP43[TMP45] = &TMP25[TMP45+7];
}
TMP25[10].len = 3; TMP25[10].kind = 2; TMP25[10].sons = &TMP43[0];
NTI130006.node = &TMP25[10];
TMP25[6].kind = 1;
TMP25[6].offset = offsetof(stringtableobj130012, Mode);
TMP25[6].typ = (&NTI130006);
TMP25[6].name = "mode";
TMP25[0].len = 3; TMP25[0].kind = 2; TMP25[0].sons = &TMP40[0];
NTI130012.node = &TMP25[0];
NTI130014.size = sizeof(stringtableobj130012*);
NTI130014.kind = 22;
NTI130014.base = (&NTI130012);
NTI130014.marker = TMP46;
}

