Пример #1
0
 private SparseEdgeMap(erl.Oracle.TnsNames.Antlr4.Runtime.Dfa.SparseEdgeMap <T> map, int maxSparseSize)
     : base(map.minIndex, map.maxIndex)
 {
     lock (map)
     {
         if (maxSparseSize < map.values.Count)
         {
             throw new ArgumentException();
         }
         keys   = Arrays.CopyOf(map.keys, maxSparseSize);
         values = new List <T>(maxSparseSize);
         values.AddRange(map.Values);
     }
 }
Пример #2
0
 public override AbstractEdgeMap <T> Remove(int key)
 {
     lock (this)
     {
         int index = System.Array.BinarySearch(keys, 0, Count, key);
         if (index < 0)
         {
             return(this);
         }
         erl.Oracle.TnsNames.Antlr4.Runtime.Dfa.SparseEdgeMap <T> result = new erl.Oracle.TnsNames.Antlr4.Runtime.Dfa.SparseEdgeMap <T>(this, MaxSparseSize);
         System.Array.Copy(result.keys, index + 1, result.keys, index, Count - index - 1);
         result.values.RemoveAt(index);
         return(result);
     }
 }
Пример #3
0
 public override AbstractEdgeMap <T> Put(int key, T value)
 {
     if (key < minIndex || key > maxIndex)
     {
         return(this);
     }
     if (value == null)
     {
         return(Remove(key));
     }
     lock (this)
     {
         int index = System.Array.BinarySearch(keys, 0, Count, key);
         if (index >= 0)
         {
             // replace existing entry
             values[index] = value;
             return(this);
         }
         System.Diagnostics.Debug.Assert(index < 0 && value != null);
         int insertIndex = -index - 1;
         if (Count < MaxSparseSize && insertIndex == Count)
         {
             // stay sparse and add new entry
             keys[insertIndex] = key;
             values.Add(value);
             return(this);
         }
         int desiredSize = Count >= MaxSparseSize ? MaxSparseSize * 2 : MaxSparseSize;
         int space       = maxIndex - minIndex + 1;
         // SparseEdgeMap only uses less memory than ArrayEdgeMap up to half the size of the symbol space
         if (desiredSize >= space / 2)
         {
             ArrayEdgeMap <T> arrayMap = new ArrayEdgeMap <T>(minIndex, maxIndex);
             arrayMap = ((ArrayEdgeMap <T>)arrayMap.PutAll(this));
             arrayMap.Put(key, value);
             return(arrayMap);
         }
         else
         {
             erl.Oracle.TnsNames.Antlr4.Runtime.Dfa.SparseEdgeMap <T> resized = new erl.Oracle.TnsNames.Antlr4.Runtime.Dfa.SparseEdgeMap <T>(this, desiredSize);
             System.Array.Copy(resized.keys, insertIndex, resized.keys, insertIndex + 1, Count - insertIndex);
             resized.keys[insertIndex] = key;
             resized.values.Insert(insertIndex, value);
             return(resized);
         }
     }
 }