// 二分法 // 根据给出的Key得到Value // return: // -1 not found public int Search(string strKeyParam, out byte[] data) { data = null; int k; // 区间左 int m; // 区间右 int j = -1; // 区间中 string strKey; int nComp; k = 0; m = (int)this.Count - 1; while (k <= m) { j = (k + m) / 2; // 取得j位置的值 ObjectLineItem item = (ObjectLineItem)this[j]; strKey = item.ObjectItem.Key; nComp = String.Compare(strKey, strKeyParam); if (nComp == 0) { data = item.ObjectItem.Data; break; } if (nComp > 0) { // strKeyParam较小 m = j - 1; } else { k = j + 1; } } if (k > m) { return(-1); // not found } return(j); }
// 获得排序后的非重复事项数 public long GetNoDupCount() { long lResult = 0; this.Sort(); long lCount = this.Count; string strPrevText = ""; for (long i = 0; i < lCount; i++) { ObjectLineItem line_item = (ObjectLineItem)this[i]; string strCurText = line_item.ObjectItem.Key; if (strPrevText != strCurText) { lResult++; strPrevText = strCurText; } } return(lResult); }
// 实现IComparable接口的CompareTo()方法, // 根据ID比较两个对象的大小,以便排序, // 按右对齐方式比较 // obj: An object to compare with this instance // 返回值 A 32-bit signed integer that indicates the relative order of the comparands. The return value has these meanings: // Less than zero: This instance is less than obj. // Zero: This instance is equal to obj. // Greater than zero: This instance is greater than obj. // 异常: ArgumentException,obj is not the same type as this instance. public override int CompareTo(object obj) { ObjectLineItem item = (ObjectLineItem)obj; return(String.Compare(this.m_strLineKey, item.m_strLineKey)); }