示例#1
0
        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();
            }
        }
示例#2
0
        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));
        }
示例#3
0
        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);
        }
示例#4
0
        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;
            }
        }
示例#5
0
 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
 }
示例#6
0
 public void Store(Obj[] objs, uint size)
 {
     Miscellanea.Assert(cols.Length == size);
     for (int i = 0; i < size; i++)
     {
         cols[i].Add(objs[i]);
     }
 }
示例#7
0
 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);
 }
示例#8
0
 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);
 }
示例#9
0
 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>();
     }
 }
示例#10
0
        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];
            }
        }
示例#11
0
        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;
        }
示例#12
0
 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;
     }
 }
示例#13
0
 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;
 }
示例#14
0
        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;
            }
        }
示例#15
0
 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]);
 }
示例#16
0
 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());
     }
 }
示例#17
0
 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());
     }
 }
示例#18
0
 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());
     }
 }
示例#19
0
        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));
        }
示例#20
0
        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);
        }
示例#21
0
        public void Finish()
        {
            var it = deleteList.GetEnumerator();

            while (it.MoveNext())
            {
                var tuple = it.Current;
                if (tuple.field1 != 0xFFFFFFFF)
                {
                    Miscellanea.Assert(table.store1.LookupSurrogate(tuple.field1) != null);
                    Miscellanea.Assert(table.store2.LookupSurrogate(tuple.field2) != null);
                    table.store1.Release(tuple.field1);
                    table.store2.Release(tuple.field2);
                }
            }
            Reset();
        }
示例#22
0
        public void Set(Obj value)
        {
            Clear();
            Miscellanea.Assert(insertList.Count == 0);
            SeqOrSetIter it = value.GetSeqOrSetIter();

            while (!it.Done())
            {
                Obj val  = it.Get();
                int surr = store.LookupValueEx(val);
                if (surr == -1)
                {
                    surr = store.Insert(val);
                }
                insertList.Add((uint)surr);
                it.Next();
            }
        }
示例#23
0
        public virtual void Insert(Obj value, uint hashcode, int slotIdx)
        {
            Miscellanea.Assert(slots != null && slotIdx < slots.Length);
            Miscellanea.Assert(slots[slotIdx] == null);
            Miscellanea.Assert(hashcode == value.Hashcode());

            slots[slotIdx]     = value;
            hashcodes[slotIdx] = hashcode;

            //## DOES IT MAKE ANY DIFFERENCE HERE TO USE int INSTEAD OF uint?
            uint hashtableIdx = hashcode % (uint)slots.Length;
            int  head         = hashtable[hashtableIdx];

            hashtable[hashtableIdx] = slotIdx;
            buckets[slotIdx]        = head;

            count++;
        }
示例#24
0
        public void Delete(uint surr)
        {
            Miscellanea.Assert(surr < 64 * bitmap.Length);

            uint widx = surr / 64;

            if (widx < bitmap.Length)
            {
                ulong mask = bitmap[widx];
                int   bidx = (int)surr % 64;
                if (((mask >> bidx) & 1) == 1)
                {
                    bitmap[widx] = mask & ~(1UL << bidx);
                    count--;
                }
            }
            // Miscellanea.Assert(count == LiveCount());
        }
示例#25
0
        static SymbTable()
        {
            int len = defaultSymbols.Length;

            for (int i = 0; i < len; i++)
            {
                string str = defaultSymbols[i];
                symbTable.Add(str);
                symbMap.Add(str, i);
                symbObjs.Add(new SymbObj(i));
            }

            for (int i = 0; i < Generated.EmbeddedSymbols.Length; i++)
            {
                int idx = SymbTable.StrToIdx(Generated.EmbeddedSymbols[i]);
                Miscellanea.Assert(idx == i);
            }
        }
示例#26
0
        ////////////////////////////////////////////////////////////////////////////

        void DeleteAt(uint index, uint hashcode)
        {
            Tuple tuple = tuples[index];

            Miscellanea.Assert(tuple.field2OrEmptyMarker != Tuple.Empty);

            // Removing the tuple
            tuples[index].field1OrNext        = firstFree;
            tuples[index].field2OrEmptyMarker = Tuple.Empty;
            Miscellanea.Assert(tuples[index].field1OrNext == firstFree);
            Miscellanea.Assert(tuples[index].field2OrEmptyMarker == Tuple.Empty);
            firstFree = index;
            count--;

            // Updating the indexes
            index123.Delete(index, hashcode);
            index12.Delete(index, Miscellanea.Hashcode(tuple.field1OrNext, tuple.field2OrEmptyMarker));
            if (!index13.IsBlank())
            {
                index13.Delete(index, Miscellanea.Hashcode(tuple.field1OrNext, tuple.field3));
            }
            if (!index23.IsBlank())
            {
                index23.Delete(index, Miscellanea.Hashcode(tuple.field2OrEmptyMarker, tuple.field3));
            }
            if (!index1.IsBlank())
            {
                index1.Delete(index, Miscellanea.Hashcode(tuple.field1OrNext));
            }
            if (!index2.IsBlank())
            {
                index2.Delete(index, Miscellanea.Hashcode(tuple.field2OrEmptyMarker));
            }
            if (!index3.IsBlank())
            {
                index3.Delete(index, Miscellanea.Hashcode(tuple.field3));
            }

            // Updating the reference count in the value stores
            store1.Release(tuple.field1OrNext);
            store2.Release(tuple.field2OrEmptyMarker);
            store3.Release(tuple.field3);
        }
示例#27
0
        public Obj Copy(bool flipped)
        {
            int count = table1.count;

            if (count == 0)
            {
                return(EmptyRelObj.Singleton());
            }

            Obj[] objs1 = new Obj[count];
            Obj[] objs2 = new Obj[count];

            int next = 0;

            for (uint i = 0; i < table1.column.Length; i++)
            {
                uint code = table1.column[i];
                if (code != OverflowTable.EmptyMarker)
                {
                    Obj val1 = store1.GetValue(i);
                    if (code >> 29 == 0)
                    {
                        objs1[next]   = val1;
                        objs2[next++] = store2.GetValue(code);
                    }
                    else
                    {
                        OverflowTable.Iter it = table1.overflowTable.GetIter(code);
                        while (!it.Done())
                        {
                            uint surr2 = it.Get();
                            objs1[next]   = val1;
                            objs2[next++] = store2.GetValue(surr2);
                            it.Next();
                        }
                    }
                }
            }
            Miscellanea.Assert(next == count);

            return(Builder.CreateBinRel(flipped ? objs2 : objs1, flipped ? objs1 : objs2, count)); //## THIS COULD BE MADE MORE EFFICIENT
        }
示例#28
0
        public int LookupValue(Obj value)
        {
            if (count == 0)
            {
                return(-1);
            }
            uint hashcode = value.Hashcode();
            int  idx      = hashtable[hashcode % hashtable.Length];

            while (idx != -1)
            {
                Miscellanea.Assert(slots[idx] != null);
                if (hashcodes[idx] == hashcode && value.IsEq(slots[idx]))
                {
                    return(idx);
                }
                idx = buckets[idx];
            }
            return(-1);
        }
示例#29
0
        ////////////////////////////////////////////////////////////////////////////////

        static long ReadNat(byte[] text, uint length, ref long offset)
        {
            long startOffset = offset;
            long endOffset   = startOffset;
            long value       = 0;
            byte ch;

            while (endOffset < length && Char.IsDigit((char)(ch = text[endOffset])))
            {
                value = 10 * value + (ch - '0');
                endOffset++;
            }
            Miscellanea.Assert(endOffset > startOffset);
            long count = endOffset - startOffset;

            if (count > 19)
            {
                offset = -startOffset - 1;
                return(-1);
            }
            else if (count == 19)
            {
                const string MAX = "9223372036854775807";
                for (int i = 0; i < 19; i++)
                {
                    ch = text[startOffset + i];
                    byte maxCh = (byte)MAX[i];
                    if (ch > maxCh)
                    {
                        offset = -startOffset - 1;
                        return(-1);
                    }
                    else if (ch < maxCh)
                    {
                        break;
                    }
                }
            }
            offset = endOffset;
            return(value);
        }
示例#30
0
        public TernaryTable(ValueStore store1, ValueStore store2, ValueStore store3)
        {
            this.store1 = store1;
            this.store2 = store2;
            this.store3 = store3;

            for (uint i = 0; i < MinSize; i++)
            {
                tuples[i].field1OrNext        = i + 1;
                tuples[i].field2OrEmptyMarker = Tuple.Empty;
            }

            for (uint i = 0; i < MinSize; i++)
            {
                Miscellanea.Assert(tuples[i].field1OrNext == i + 1);
                Miscellanea.Assert(tuples[i].field2OrEmptyMarker == Tuple.Empty);
            }

            index123.Init(MinSize);
            index12.Init(MinSize);
        }