using System; using System.Collections.Generic; using System.Text; namespace ScrewTurn.Wiki.SearchEngine { /// /// Contains a collection of MatchInfo objects, sorted by index. /// public class WordInfoCollection : ICollection { /// /// The items of the collection. /// protected List items; /// /// Initializes a new instance of the class. /// public WordInfoCollection() : this(10) { } /// /// Initializes a new instance of the class. /// /// The initial capacity of the collection. /// If is less than or equal to zero. public WordInfoCollection(int capacity) { if(capacity <= 0) throw new ArgumentOutOfRangeException("capacity", "Invalid capacity"); items = new List(capacity); } /// /// Adds an item to the collection. /// /// The item to add. /// If is null. public void Add(WordInfo item) { if(item == null) throw new ArgumentNullException("item"); items.Add(item); // Sort items.Sort(delegate(WordInfo mi1, WordInfo mi2) { return mi1.FirstCharIndex.CompareTo(mi2.FirstCharIndex); }); } /// /// Clears the collection. /// public void Clear() { items.Clear(); } /// /// Determines whether an item is in the collection. /// /// The item to check for. /// true if the item in the collection, false otherwise. public bool Contains(WordInfo item) { if(item == null) return false; return items.Contains(item); } /// /// Determines whether a word is in the collection. /// /// The word. /// true if the word is in the collection, false otherwise. public bool Contains(string word) { if(string.IsNullOrEmpty(word)) return false; foreach(WordInfo w in items) { if(w.Text == word) return true; } return false; } /// /// Determines whether a word occurrence is in the collection. /// /// The word. /// The index of the first character. /// True if the collection contains the occurrence, false otherwise. public bool ContainsOccurrence(string word, int firstCharIndex) { if(string.IsNullOrEmpty(word)) return false; if(firstCharIndex < 0) return false; foreach(WordInfo w in items) { if(w.Text == word && w.FirstCharIndex == firstCharIndex) return true; } return false; } /// /// Copies the collection items to an array. /// /// The destination array. /// The zero-based array index at which the copy begins. /// If is not within the bounds of . public void CopyTo(WordInfo[] array, int arrayIndex) { if(array == null) throw new ArgumentNullException("array"); if(arrayIndex < 0 || arrayIndex > array.Length - 1) throw new ArgumentOutOfRangeException("arrayIndex", "Index should be greater than or equal to zero and less than the number of items in the array"); if(array.Length - arrayIndex < items.Count) throw new ArgumentOutOfRangeException("arrayIndex", "Not enough space for copying the items starting at the specified index"); items.CopyTo(array, arrayIndex); } /// /// Gets the number of items in the collection. /// public int Count { get { return items.Count; } } /// /// Gets the capacity of the collection. /// public int Capacity { get { return items.Capacity; } } /// /// Gets a value indicating whether the collection is read-only. /// public bool IsReadOnly { get { return false; } } /// /// Removes an item from the collection. /// /// The item to remove. /// true if item is removed, false otherwise. /// If is null. public bool Remove(WordInfo item) { if(item == null) throw new ArgumentNullException("item"); return items.Remove(item); } /// /// Returns an enumerator that iterates through the items of the collection. /// /// The enumerator. public IEnumerator GetEnumerator() { return items.GetEnumerator(); } /// /// Returns an enumerator that iterates through the items of the collection. /// /// The enumerator. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); } /// /// Gets an item of the collection. /// /// The index of the item to retrieve. /// The item. /// If is outside the bounds of the collection. public WordInfo 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 collection"); return items[index]; } } /// /// Returns a string representation of the current instance. /// /// The string representation. public override string ToString() { return items.Count.ToString() + " matches"; } } }