Ejemplo n.º 1
0
 /// <summary>Removes an index from the map and returns whether things still seem to be consistent.</summary>
 private protected static bool RemoveIndex(AllowNullDictionary <T, CircularList <int> > map, T item, int index)
 {
     if (!map.TryGetValue(item, out CircularList <int> indices) || !RemoveIndex(indices, index))
     {
         return(false);
     }
     if (indices.Count == 0)
     {
         map.Remove(item);
     }
     return(true);
 }
Ejemplo n.º 2
0
 private protected static bool AdjustIndex(AllowNullDictionary <T, CircularList <int> > map, T item, int startIndex, int diff, HashSet <T> adjustedValues)
 {
     if (adjustedValues.Contains(item))
     {
         return(true);
     }
     adjustedValues.Add(item);
     if (!map.TryGetValue(item, out var indices))
     {
         return(false);
     }
     AdjustIndex(indices, startIndex, diff);
     return(true);
 }
Ejemplo n.º 3
0
        private void BuildIndexMap(bool rebuild = true)
        {
            if (itemToIndex.Count > 0)
            {
                itemToIndex = new AllowNullDictionary <T, CircularList <int> >();
            }
            int length = Count;

            for (int i = 0; i < length; i++)
            {
                T item = base.GetItem(i);
                AddIndex(itemToIndex, item, i);
            }

            if (rebuild)
            {
                OnMapRebuilt();
            }
        }
Ejemplo n.º 4
0
        /// <summary>Adds an index to the map and returns whether things still seem to be consistent.</summary>
        private protected static bool AddIndex(AllowNullDictionary <T, CircularList <int> > map, T item, int index)
        {
            if (!map.TryGetValue(item, out CircularList <int> indices))
            {
                indices   = new CircularList <int>(1);
                map[item] = indices;
            }

            if (indices.Count == 0 || index > indices[indices.Count - 1])
            {
                indices.AddLast(index);
                return(true);
            }

            var pos = indices.BinarySearch(index);

            if (pos >= 0)
            {
                return(false);
            }
            indices.Insert(~pos, index);
            return(true);
        }
Ejemplo n.º 5
0
        private void AssertConsistency <T>(FastLookupCollection <T> coll)
        {
            var itemToIndex = new AllowNullDictionary <T, CircularList <int> >();

            for (int i = 0; i < coll.Count; i++)
            {
                T item = coll[i];
                if (!itemToIndex.TryGetValue(item, out CircularList <int> indices))
                {
                    indices           = new CircularList <int>();
                    itemToIndex[item] = indices;
                }

                indices.Add(i);
            }

            var actualItemToIndex = (AllowNullDictionary <T, CircularList <int> >)Reflector.GetField(coll, "itemToIndex");

            AssertItemsEqual(Sorted(itemToIndex), Sorted(actualItemToIndex));

            IEnumerable Sorted(AllowNullDictionary <T, CircularList <int> > dict)
            => new AllowNullDictionary <T, CircularList <int> >(dict.OrderBy(item => item.Key));
        }
Ejemplo n.º 6
0
 private protected static bool ContainsIndex(AllowNullDictionary <T, CircularList <int> > map, T item, int index)
 => map.TryGetValue(item, out var indices) && indices.Contains(index);
Ejemplo n.º 7
0
 private protected static int GetFirstIndex(AllowNullDictionary <T, CircularList <int> > map, T item)
 => map.TryGetValue(item, out CircularList <int> indices) ? indices[0] : -1;