public void Set(Obj value, int idx1, int idx2, int idx3) { Miscellanea.Assert(deleteList.Count == 0 || deleteList.Count == table.count); Miscellanea.Assert(insertList.Count == 0); Clear(); TernRelIter it = value.GetTernRelIter(); while (!it.Done()) { Obj val1 = idx1 == 0 ? it.Get1() : (idx1 == 1 ? it.Get2() : it.Get3()); Obj val2 = idx2 == 0 ? it.Get1() : (idx2 == 1 ? it.Get2() : it.Get3()); Obj val3 = idx3 == 0 ? it.Get1() : (idx3 == 1 ? it.Get2() : it.Get3()); int surr1 = store1.LookupValueEx(val1); if (surr1 == -1) { surr1 = store1.Insert(val1); } int surr2 = store2.LookupValueEx(val2); if (surr2 == -1) { surr2 = store2.Insert(val2); } int surr3 = store3.LookupValueEx(val3); if (surr3 == -1) { surr3 = store3.Insert(val3); } insertList.Add(new TernaryTable.Tuple((uint)surr1, (uint)surr2, (uint)surr3)); it.Next(); } }
public void Delete(uint index, uint hashcode) { uint hashIdx = hashcode % (uint)hashtable.Length; uint head = hashtable[hashIdx]; Miscellanea.Assert(head != Empty); if (head == index) { hashtable[hashIdx] = buckets[index]; buckets[index] = Empty; return; } uint curr = head; for ( ; ;) { uint next = buckets[curr]; Miscellanea.Assert(next != Empty); if (next == index) { buckets[curr] = buckets[next]; buckets[next] = Empty; return; } curr = next; } }
public Obj Copy(int idx1, int idx2, int idx3) { if (count == 0) { return(EmptyRelObj.Singleton()); } Obj[] objs1 = new Obj[count]; Obj[] objs2 = new Obj[count]; Obj[] objs3 = new Obj[count]; int len = tuples.Length; int next = 0; for (uint i = 0; i < len; i++) { Tuple tuple = tuples[i]; if (tuple.field2OrEmptyMarker != Tuple.Empty) { objs1[next] = store1.GetValue(tuple.field1OrNext); objs2[next] = store2.GetValue(tuple.field2OrEmptyMarker); objs3[next] = store3.GetValue(tuple.field3); next++; } } Miscellanea.Assert(next == count); Obj[][] cols = new Obj[3][]; cols[idx1] = objs1; cols[idx2] = objs2; cols[idx3] = objs3; return(Builder.CreateTernRel(cols[0], cols[1], cols[2], count)); }
public static int[] SortedIndexes(Obj[] col1, Obj[] col2, Obj[] col3) { Miscellanea.Assert(col1.Length == col2.Length && col1.Length == col3.Length); int count = col1.Length; int[] idxs = new int[count]; for (int i = 0; i < count; i++) { idxs[i] = i; } Comparison <int> cmp = delegate(int i, int j) { int res = col1[i].CompareTo(col1[j]); if (res != 0) { return(res); } res = col2[i].CompareTo(col2[j]); if (res != 0) { return(res); } return(col3[i].CompareTo(col3[j])); }; Array.Sort(idxs, cmp); return(idxs); }
override public void Insert(Obj value, uint hashcode, int index) { Miscellanea.Assert(firstFreeIdx == index); Miscellanea.Assert(nextFreeIdx[index] != -1); base.Insert(value, hashcode, index); firstFreeIdx = nextFreeIdx[index]; nextFreeIdx[index] = -1; //## UNNECESSARY, BUT USEFUL FOR DEBUGGING }
public void Store(Obj[] objs, uint size) { Miscellanea.Assert(cols.Length == size); for (int i = 0; i < size; i++) { cols[i].Add(objs[i]); } }
override public void Dump() { Console.WriteLine("offset = {0}, length = {1}", offset, length); Console.WriteLine( "master: items.Length = {0}, length = {1}, used = {2}", master.items.Length, master.length, master.used ); Miscellanea.Assert(items == master.items); }
public void Store(Obj obj) { if (cols.Length != 1) { Console.WriteLine("cols.Length = " + cols.Length.ToString()); } Miscellanea.Assert(cols.Length == 1); cols[0].Add(obj); }
public State(uint arity) { Miscellanea.Assert(arity >= 0 & arity <= 3); cols = new List <Obj> [arity]; for (int i = 0; i < arity; i++) { cols[i] = new List <Obj>(); } }
//////////////////////////////////////////////////////////////////////////////// // If the function is successfull, it returns the index of the next token to consume // If it fails, it returns the location/index of the error, negated and decremented by one static long ParseObj(Token[] tokens, long offset, out Obj var) { int length = tokens.Length; if (offset >= length) { var = null; return(-offset - 1); } Token token = tokens[offset]; switch (token.type) { case TokenType.Comma: case TokenType.Colon: case TokenType.Semicolon: case TokenType.Arrow: case TokenType.ClosePar: case TokenType.CloseBracket: var = null; return(-offset - 1); case TokenType.Int: var = IntObj.Get((long)token.value); return(offset + 1); case TokenType.Float: var = new FloatObj((double)token.value); return(offset + 1); case TokenType.Symbol: return(ParseSymbOrTaggedObj(tokens, offset, out var)); case TokenType.OpenPar: if (IsRecord(tokens, offset)) { return(ParseRec(tokens, offset, out var)); } else { return(ParseSeq(tokens, offset, out var)); } case TokenType.OpenBracket: return(ParseUnordColl(tokens, offset, out var)); case TokenType.String: var = Miscellanea.StrToObj((string)token.value); return(offset + 1); default: var = null; throw new InvalidOperationException(); // Unreachable code } }
public Iter GetIter2(long field2) { if (index2.IsBlank()) { BuildIndex2(); } uint hashcode = Miscellanea.Hashcode((uint)field2); return(new Iter(Tuple.Empty, (uint)field2, Tuple.Empty, index2.Head(hashcode), Iter.Type.F2, this)); }
public Iter GetIter3(long field3) { if (index3.IsBlank()) { BuildIndex3(); } uint hashcode = Miscellanea.Hashcode((uint)field3); return(new Iter(Tuple.Empty, Tuple.Empty, (uint)field3, index3.Head(hashcode), Iter.Type.F3, this)); }
public Iter GetIter1(long field1) { if (index1.IsBlank()) { BuildIndex1(); } uint hashcode = Miscellanea.Hashcode((uint)field1); return(new Iter((uint)field1, Tuple.Empty, Tuple.Empty, index1.Head(hashcode), Iter.Type.F1, this)); }
public static void SortUnique(Obj[] col1, Obj[] col2, Obj[] col3, int count, out Obj[] norm_col_1, out Obj[] norm_col_2, out Obj[] norm_col_3) { Miscellanea.Assert(count > 0); int[] idxs = new int[count]; for (int i = 0; i < count; i++) { idxs[i] = i; } Comparison <int> cmp = delegate(int i, int j) { int res = col1[i].CompareTo(col1[j]); if (res != 0) { return(res); } res = col2[i].CompareTo(col2[j]); if (res != 0) { return(res); } return(col3[i].CompareTo(col3[j])); }; Array.Sort(idxs, cmp); int prev = 0; for (int i = 1; i < count; i++) { int j = idxs[i]; int k = idxs[i - 1]; if (!col1[j].IsEq(col1[k]) || !col2[j].IsEq(col2[k]) || !col3[j].IsEq(col3[k])) { if (i != ++prev) { idxs[prev] = idxs[i]; } } } int size = prev + 1; norm_col_1 = new Obj[size]; norm_col_2 = new Obj[size]; norm_col_3 = new Obj[size]; for (int i = 0; i < size; i++) { int j = idxs[i]; norm_col_1[i] = col1[j]; norm_col_2[i] = col2[j]; norm_col_3[i] = col3[j]; } }
public void Init(uint size) { Miscellanea.Assert(hashtable == null & buckets == null); hashtable = new uint[size]; buckets = new uint[size]; for (int i = 0; i < size; i++) { hashtable[i] = Empty; buckets[i] = Empty; } }
public void Insert(uint index, uint hashcode) { Miscellanea.Assert(buckets[index] == Empty); Miscellanea.Assert(index < hashtable.Length); uint hashIdx = hashcode % (uint)hashtable.Length; uint head = hashtable[hashIdx]; hashtable[hashIdx] = index; buckets[index] = head; }
public static Obj StringToObj(string str) { int[] cps = Miscellanea.CodePoints(str); int len = cps.Length; Obj[] objs = new Obj[len]; for (int i = 0; i < len; i++) { objs[i] = IntObj.Get(cps[i]); } return(new TaggedObj(SymbTable.StringSymbId, new MasterSeqObj(objs))); }
public BinRelIter(Obj[] col1, Obj[] col2, int[] idxs, int next, int last) { Miscellanea.Assert(col1.Length == col2.Length); Miscellanea.Assert(idxs == null || col1.Length == idxs.Length); Miscellanea.Assert(next >= 0); Miscellanea.Assert(last >= -1 & last < col1.Length); this.col1 = col1; this.col2 = col2; this.idxs = idxs; this.next = next; this.last = last; }
public void Release(uint index) { int refCount = refCounts[index]; Miscellanea.Assert(refCount > 0); refCounts[index] = refCount - 1; if (refCount == 1) { Delete((int)index); nextFreeIdx[index] = firstFreeIdx; firstFreeIdx = (int)index; } }
public int NextFreeIdx(int index) { Miscellanea.Assert(index == -1 || index >= slots.Length || (slots[index] == null & nextFreeIdx[index] != -1)); if (index == -1) { return(firstFreeIdx); } if (index >= nextFreeIdx.Length) { return(index + 1); } return(nextFreeIdx[index]); }
public static Obj CreateSet(Obj[] objs, long count) { Miscellanea.Assert(objs.Length >= count); if (count != 0) { Obj[] norm_objs = Algs.SortUnique(objs, (int)count); return(new NeSetObj(norm_objs)); } else { return(EmptyRelObj.Singleton()); } }
public static Obj CreateTernRel(Obj[] col1, Obj[] col2, Obj[] col3, long count) { Miscellanea.Assert(count <= col1.Length && count <= col2.Length && count <= col3.Length); if (col1.Length != 0) { Obj[] norm_col_1, norm_col_2, norm_col_3; Algs.SortUnique(col1, col2, col3, (int)count, out norm_col_1, out norm_col_2, out norm_col_3); return(new NeTernRelObj(norm_col_1, norm_col_2, norm_col_3)); } else { return(EmptyRelObj.Singleton()); } }
void BuildIndex3() { uint len = (uint)tuples.Length; index3.Init(len); for (uint i = 0; i < len; i++) { Tuple tuple = tuples[i]; if (tuple.field2OrEmptyMarker != Tuple.Empty) { index3.Insert(i, Miscellanea.Hashcode(tuple.field3)); } } }
public bool Contains12(uint field1, uint field2) { uint hashcode = Miscellanea.Hashcode(field1, field2); for (uint idx = index12.Head(hashcode); idx != Tuple.Empty; idx = index12.Next(idx)) { Tuple tuple = tuples[idx]; if (tuple.field1OrNext == field1 & tuple.field2OrEmptyMarker == field2) { return(true); } } return(false); }
public bool Contains(long field1, long field2, long field3) { uint hashcode = Miscellanea.Hashcode((uint)field1, (uint)field2, (uint)field3); for (uint idx = index123.Head(hashcode); idx != Tuple.Empty; idx = index123.Next(idx)) { Tuple tuple = tuples[idx]; if (tuple.field1OrNext == field1 & tuple.field2OrEmptyMarker == field2 & tuple.field3 == field3) { return(true); } } return(false); }
public void Delete(uint field1, uint field2, uint field3) { uint hashcode = Miscellanea.Hashcode(field1, field2, field3); for (uint idx = index123.Head(hashcode); idx != Tuple.Empty; idx = index123.Next(idx)) { Tuple tuple = tuples[idx]; if (tuple.field1OrNext == field1 & tuple.field2OrEmptyMarker == field2 & tuple.field3 == field3) { DeleteAt(idx, hashcode); return; } } }
public static Obj CreateBinRel(Obj[] col1, Obj[] col2, long count) { Miscellanea.Assert(count <= col1.Length & count <= col2.Length); if (count != 0) { Obj[] norm_col_1, norm_col_2; Algs.SortUnique(col1, col2, (int)count, out norm_col_1, out norm_col_2); return(new NeBinRelObj(norm_col_1, norm_col_2, !Algs.SortedArrayHasDuplicates(norm_col_1))); } else { return(EmptyRelObj.Singleton()); } }
public static Obj CreateSeq(Obj[] objs, long count) { Miscellanea.Assert(objs != null && count <= objs.Length); for (int i = 0; i < count; i++) { Miscellanea.Assert(objs[i] != null); } Obj[] objs_copy = new Obj[count]; for (int i = 0; i < count; i++) { objs_copy[i] = objs[i]; } return(new MasterSeqObj(objs_copy)); }
public int Insert(Obj value) { int capacity = slots != null ? slots.Length : 0; Miscellanea.Assert(count <= capacity); if (count == capacity) { Resize(count + 1); } lastSurrogate = store.NextFreeIdx(lastSurrogate); surrogates[count] = lastSurrogate; Insert(value, count); return(lastSurrogate); }
override public Obj UpdatedAt(long idx, Obj obj) { if (idx < 0 | idx >= length) { Miscellanea.SoftFail("Invalid sequence index"); } int offset = Offset(); Obj[] newItems = new Obj[length]; for (int i = 0; i < length; i++) { newItems[i] = i == idx ? obj : items[offset + i]; } return(new MasterSeqObj(newItems)); }