using System; using System.Collections.Generic; using System.Text; namespace ScrewTurn.Wiki.SearchEngine { /// /// Implements a sorted set of integers which can be accessed by index. /// /// Instance members are not thread-safe. public class SortedBasicWordInfoSet : IEnumerable { private List items; /// /// Initializes a new instance of the class. /// public SortedBasicWordInfoSet() : this(10) { } /// /// Initializes a new instance of the class. /// /// The initial capacity. /// If is null. public SortedBasicWordInfoSet(int capacity) { if(capacity <= 0) throw new ArgumentOutOfRangeException("Invalid capacity", "capacity"); items = new List(capacity); } /// /// Gets the count if the items in the set. /// public int Count { get { return items.Count; } } /// /// Gets the capacity of the set. /// public int Capacity { get { return items.Capacity; } } /// /// Adds a new item to the set. /// /// The item to add. /// true if the item is added, false otherwise. /// Adding an item is O(log n) if the item is already in the set, /// O(n) otherwise, where n is the number of items in the set. public bool Add(BasicWordInfo item) { int idx = items.BinarySearch(item); if(idx < 0) { // Item does not exist, insert it to the right position to avoid explicit sorting // Sort (quick sort) is O(nlogn) on average, O(n^2) worst, Insert is O(n) items.Insert(~idx, item); return true; } else return false; } /// /// Determines whether the set contains an item. /// /// The item to look for. /// true if the set contains the specified item, false otherwise. /// The operation is O(log n), where n is the number of items in the set. public bool Contains(BasicWordInfo item) { if(items.Count == 0) return false; return items.BinarySearch(item) >= 0; } /// /// Removes an item from the set. /// /// The item to remove. /// true if the item is removed, false otherwise. /// The operation is O(n), where n is the number of items in the set. public bool Remove(BasicWordInfo item) { if(items.Count == 0) return false; // Remove and RemoveAt are both O(n) return items.Remove(item); } /// /// Clears the set. /// /// The operation is O(1). public void Clear() { items.Clear(); } /// /// Returns an enumerator that iterates through the set. /// /// The enumerator IEnumerator IEnumerable.GetEnumerator() { return items.GetEnumerator(); } /// /// Returns an enumerator that iterates through the set. /// /// The enumerator System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); } /// /// Gets an item from the set at a specific index. /// /// The zero-based index of the element to get. /// The item at the specified index. /// If is outside the bounds of the collection. public BasicWordInfo this[int index] { get { if(index < 0 || index > items.Count - 1) throw new IndexOutOfRangeException("Index should be greater than or equal to zero and less than the number of items in the set"); return items[index]; } } /// /// Gets a string representation of the current instance. /// /// The string representation. public override string ToString() { StringBuilder sb = new StringBuilder(50); for(int i = 0; i < items.Count; i++) { sb.AppendFormat("{0}", items[i]); if(i != items.Count - 1) sb.Append(", "); } return sb.ToString(); } } }