fork download
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace Jtc.CsQuery
  9. {
  10. /// <summary>
  11. /// A sorted dictionary that allows lookup by range.
  12. /// </summary>
  13. interface IRangeSortedDictionary<TValue>: IDictionary<string, TValue>
  14. {
  15. IEnumerable<string> GetRangeKeys(string subKey);
  16. IEnumerable<TValue> GetRange(string subKey);
  17.  
  18. }
  19. /*
  20.   * .class/body/div
  21.   * .class/body/a/span/element
  22.   * #id/body/div/span/element
  23.   *
  24.   * search for .class#id
  25.   * 1) return all .class elements
  26.   * 2) serach #id/body/div
  27.   */
  28. public class RangeSortedDictionary<TValue> : IRangeSortedDictionary<TValue>
  29. {
  30. protected SortedSet<string> Keys = new SortedSet<string>();
  31. protected Dictionary<string,TValue> Index = new Dictionary<string,TValue>();
  32. public IEnumerable<string> GetRangeKeys(string subkey)
  33. {
  34. if (string.IsNullOrEmpty(subkey)) {
  35. yield break;
  36. }
  37. string lastKey = subkey.Substring(0,subkey.Length - 1) + Convert.ToChar(Convert.ToInt32(subkey[subkey.Length - 1]) + 1);
  38.  
  39. foreach (var key in Keys.GetViewBetween(subkey, lastKey))
  40. {
  41. if (key != lastKey)
  42. {
  43. yield return key;
  44. }
  45. }
  46. }
  47.  
  48. public IEnumerable<TValue> GetRange(string subKey)
  49. {
  50. foreach (var key in GetRangeKeys(subKey))
  51. {
  52. yield return Index[key];
  53. }
  54. }
  55. #region IDictionary<string,TValue> Members
  56.  
  57. public void Add(string key, TValue value)
  58. {
  59. if (Keys.Add(key))
  60. {
  61. Index.Add(key, value);
  62. }
  63. }
  64.  
  65. public bool ContainsKey(string key)
  66. {
  67. return Keys.Contains(key);
  68. }
  69.  
  70. ICollection<string> IDictionary<string, TValue>.Keys
  71. {
  72. get { return Keys; }
  73. }
  74.  
  75. public bool Remove(string key)
  76. {
  77. if (Keys.Remove(key))
  78. {
  79. Index.Remove(key);
  80. return true;
  81. }
  82. return false;
  83. }
  84.  
  85. public bool TryGetValue(string key, out TValue value)
  86. {
  87. return Index.TryGetValue(key, out value);
  88. }
  89.  
  90. public ICollection<TValue> Values
  91. {
  92. get { throw new NotImplementedException(); }
  93. }
  94.  
  95. public TValue this[string key]
  96. {
  97. get
  98. {
  99. return Index[key];
  100. }
  101. set
  102. {
  103. Index[key] = value;
  104. }
  105. }
  106.  
  107. #endregion
  108.  
  109. #region ICollection<KeyValuePair<string,TValue>> Members
  110.  
  111. public void Add(KeyValuePair<string, TValue> item)
  112. {
  113. Add(item.Key, item.Value);
  114. }
  115. public void Clear()
  116. {
  117. Keys.Clear();
  118. Index.Clear();
  119. }
  120.  
  121. public bool Contains(KeyValuePair<string, TValue> item)
  122. {
  123. return Index.Contains(item);
  124. }
  125.  
  126. public void CopyTo(KeyValuePair<string, TValue>[] array, int arrayIndex)
  127. {
  128. throw new NotImplementedException();
  129. }
  130.  
  131. public int Count
  132. {
  133. get { return Index.Count; }
  134. }
  135.  
  136. public bool IsReadOnly
  137. {
  138. get { return false; }
  139. }
  140.  
  141. public bool Remove(KeyValuePair<string, TValue> item)
  142. {
  143. return Remove(item.Key);
  144. }
  145.  
  146. #endregion
  147.  
  148. #region IEnumerable<KeyValuePair<string,TValue>> Members
  149.  
  150. public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
  151. {
  152. // Don't use dictionary enumerator - return values in sorted order when enumerating over entire object
  153. foreach (var key in Keys)
  154. {
  155. yield return new KeyValuePair<string, TValue>(key, Index[key]);
  156. }
  157. }
  158.  
  159. #endregion
  160.  
  161. #region IEnumerable Members
  162.  
  163. IEnumerator IEnumerable.GetEnumerator()
  164. {
  165. return GetEnumerator();
  166. }
  167.  
  168. #endregion
  169.  
  170.  
  171. }
  172. }
  173.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(30,19): error CS0246: The type or namespace name `SortedSet' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty