Beispiel #1
0
        public ImmutableCompactList <T> Add(T item)
        {
            if (this.data == null)
            {
                if (item != null)
                {
                    return(new ImmutableCompactList <T>(item));
                }
                else
                {
                    return(new ImmutableCompactList <T>(SmallCollection <T> .NullItem));
                }
            }
            else if (this.data is SmallCollection <T> shortList)
            {
                return(new ImmutableCompactList <T>(shortList.Add(item, FullListCreator)));
            }
            else if (this.data is T[] list)
            {
                // TODO: Switch to ImmutableList after some other threshold.
                // This can become very slow if a lot of items are added.
                Array.Resize(ref list, list.Length + 1);
                list[list.Length - 1] = item;
                return(new ImmutableCompactList <T>(list));
            }
            else
            {
                Debug.Assert(this.data is T);

                return(new ImmutableCompactList <T>(SmallCollection <T> .Create((T)this.data, item)));
            }
        }
        public bool Add(T item)
        {
            if (this.data == null)
            {
                if (item != null)
                {
                    this.data = item;
                }
                else
                {
                    this.data = SmallCollection <T> .NullItem;
                }

                return(true);
            }

            if (this.data is SmallCollection <T> shortList)
            {
                if (shortList.AddToSet(item, FullSetCreator, out object newSet))
                {
                    this.data = newSet;
                    return(true);
                }

                return(false);
            }

            if (this.data is HashSet <T> set)
            {
                return(set.Add(item));
            }

            Debug.Assert(this.data is T);

            T singleItem = (T)this.data;

            if (!Comparer.Equals(item, singleItem))
            {
                this.data = SmallCollection <T> .Create(singleItem, item);

                return(true);
            }

            return(false);
        }
 public CompactSet(T item1, T item2)
 {
     if (!Comparer.Equals(item1, item2))
     {
         this.data = SmallCollection <T> .Create(item1, item2);
     }
     else
     {
         if (item1 != null)
         {
             this.data = item1;
         }
         else
         {
             this.data = SmallCollection <T> .NullItem;
         }
     }
 }
        public bool Remove(T item)
        {
            if (this.data == null)
            {
                return(false);
            }


            if (this.data is SmallCollection <T> smallSet)
            {
                if (smallSet.Remove(item, out object?newSet))
                {
                    this.data = newSet;
                    return(true);
                }

                return(false);
            }

            if (this.data is HashSet <T> set)
            {
                if (set.Remove(item))
                {
                    if (set.Count <= SmallCollection <T> .MaxSize)
                    {
                        this.data = SmallCollection <T> .Create(set);
                    }

                    return(true);
                }
            }

            if (Comparer.Equals((T)this.data, item))
            {
                this.data = null;
                return(true);
            }

            return(false);
        }
Beispiel #5
0
        internal static object?CreateListData(IEnumerable <T> enumerable)
        {
            if (!(enumerable is ICollection <T> collection))
            {
                // Copy items into a temporary collection, to avoid
                // enumerating the enumerator twice.
                collection = new List <T>(enumerable);
            }

            int totalCount = collection.Count;

            if (totalCount <= SmallCollection <T> .MaxSize)
            {
                return(SmallCollection <T> .Create(collection));
            }
            else
            {
                T[] array = new T[totalCount];
                collection.CopyTo(array, 0);
                return(array);
            }
        }