Пример #1
0
        /// <summary>
        /// Removes the stored item.
        /// </summary>
        /// <returns>True if the item was removed, false otherwise</returns>
        public bool Remove(T item, UnorderedListMetadata metadata)
        {
            // the metadata says the item is not in the collection
            if (metadata.Index == -1)
            {
                return(false);
            }

            int index = metadata.Index;

            metadata.Index    = -1;
            metadata.StoredIn = null;

            // nothing to remove if we are empty
            if (Length == 0)
            {
                return(false);
            }

            StoredItem replacer = Pop();

            // If the item to remove was the item at the end of the list or there are no items left
            // then the popped item was equal to the item itself, so just return
            if (index == Length || Length == 0)
            {
                return(true);
            }

            // Replace the removed item with the popped item.
            replacer.Metadata.Index    = index;
            replacer.Metadata.StoredIn = this;
            Items[index] = replacer;
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Checks to see if the given item is contained in the UnorderedList. This is O(1).
        /// </summary>
        /// <param name="item">The item to check</param>
        /// <param name="metadata">The item's metadata</param>
        /// <returns>True if the item is contained, false otherwise</returns>
        public bool Contains(T item, UnorderedListMetadata metadata)
        {
            if (metadata.Index < 0)
            {
                return(false);
            }

            if (metadata.Index >= Length)
            {
                return(false);
            }

            return(EqualityComparer <T> .Default.Equals(Items[metadata.Index].Item, item));
        }
Пример #3
0
        /// <summary>
        /// Adds an item to the list. The location of the item in the list is unspecified.
        /// </summary>
        public void Add(T item, UnorderedListMetadata metadata)
        {
            // Make sure the item is not in another list
            if (metadata.Index != -1)
            {
                throw new Exception(String.Format("Item {0} is already stored in another UnorderedList, or has been added twice to the same one", item));
            }

            // Grow the array if necessary
            if (Length == Items.Length)
            {
                Array.Resize(ref Items, Items.Length * 2);
            }

            // Add the item
            metadata.Index    = Length;
            metadata.StoredIn = this;
            Items[Length++]   = new StoredItem()
            {
                Item     = item,
                Metadata = metadata
            };
        }