using System;
using System.Collections.Generic;
using System.Text;
namespace ScrewTurn.Wiki.SearchEngine {
///
/// Contains basic information about a word in a document.
///
public class BasicWordInfo : IEquatable, IComparable {
///
/// The index in the original document the word starts at.
///
protected ushort firstCharIndex;
///
/// The index of the word in the document.
///
protected ushort wordIndex;
///
/// The location of the word in the document.
///
protected WordLocation location;
///
/// Initializes a new instance of the class.
///
/// The index of the first character of the word in the document.
/// The index of the word in the document.
/// The location of the word in the document.
/// If or are less than zero.
public BasicWordInfo(ushort firstCharIndex, ushort wordIndex, WordLocation location) {
if(firstCharIndex < 0) throw new ArgumentOutOfRangeException("firstCharIndex", "Invalid first char index: must be greater than or equal to zero");
if(wordIndex < 0) throw new ArgumentOutOfRangeException("wordIndex", "Invalid word index: must be greater than or equal to zero");
this.firstCharIndex = firstCharIndex;
this.wordIndex = wordIndex;
this.location = location;
}
///
/// Gets the index of the first character of the word in the document.
///
public ushort FirstCharIndex {
get { return firstCharIndex; }
}
///
/// Gets the index of the word in the document.
///
public ushort WordIndex {
get { return wordIndex; }
}
///
/// Gets the location of the word in the document.
///
public WordLocation Location {
get { return location; }
}
///
/// Determinates whether the current instance is equal to an instance of an object.
///
/// The instance of the object.
/// true if the instances are value-equal, false otherwise.
public override bool Equals(object other) {
if(other is BasicWordInfo) return Equals((BasicWordInfo)other);
else return false;
}
///
/// Determinates whether the current instance is value-equal to another.
///
/// The other instance.
/// true if the instances are value-equal, false otherwise.
public bool Equals(BasicWordInfo other) {
if(object.ReferenceEquals(other, null)) return false;
return other.FirstCharIndex == firstCharIndex && other.WordIndex == wordIndex &&
other.Location == location;
}
///
/// Applies the value-equality operator to two objects.
///
/// The first object.
/// The second object.
/// true if the objects are value-equal, false otherwise.
public static bool operator ==(BasicWordInfo x, BasicWordInfo y) {
if(object.ReferenceEquals(x, null) && !object.ReferenceEquals(y, null) ||
!object.ReferenceEquals(x, null) && object.ReferenceEquals(y, null)) return false;
if(object.ReferenceEquals(x, null) && object.ReferenceEquals(y, null)) return true;
return x.Equals(y);
}
///
/// Applies the value-inequality operator to two objects.
///
/// The first object.
/// The second object.
/// true if the objects are not value-equal, false otherwise.
public static bool operator !=(BasicWordInfo x, BasicWordInfo y) {
return !(x == y);
}
///
/// Gets the hash code of the current instance.
///
/// The hash code.
public override int GetHashCode() {
return location.GetHashCode() + firstCharIndex * 10 + wordIndex * 100000;
}
///
/// Compares the current instance with another instance.
///
/// The other instance.
/// The comparison result.
/// The First Char Index does not partecipate to the comparison.
public int CompareTo(BasicWordInfo other) {
if(other == null) return 1;
int res = location.CompareTo(other.Location) * 2;
return res + wordIndex.CompareTo(other.WordIndex);
}
}
}