PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/src/Boo.Lang.Compiler/Ast/NodeCollection.cs

https://github.com/w4x/boolangstudio
C# | 344 lines | 267 code | 45 blank | 32 comment | 29 complexity | ca48f60058e53673da83584b4a6dca87 MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using Boo.Lang;
  32. namespace Boo.Lang.Compiler.Ast
  33. {
  34. /// <summary>
  35. /// Node collection base class.
  36. /// </summary>
  37. public class NodeCollection<T> : ICollection, ICloneable, IEnumerable<T>
  38. where T : Node
  39. {
  40. protected Node _parent;
  41. protected List _list;
  42. protected NodeCollection()
  43. {
  44. _list = new List();
  45. }
  46. protected NodeCollection(Node parent)
  47. {
  48. _parent = parent;
  49. _list = new List();
  50. }
  51. protected NodeCollection(Node parent, IEnumerable<T> list)
  52. {
  53. if (null == list) throw new ArgumentNullException("list");
  54. _parent = parent;
  55. _list = new List(list);
  56. }
  57. public T this[int index]
  58. {
  59. get { return (T)_list[index]; }
  60. set { _list[index] = value; }
  61. }
  62. public IEnumerator<T> GetEnumerator()
  63. {
  64. foreach (T item in _list) yield return item;
  65. }
  66. public int Count
  67. {
  68. get { return _list.Count; }
  69. }
  70. public bool IsSynchronized
  71. {
  72. get { return false; }
  73. }
  74. public object SyncRoot
  75. {
  76. get { return this; }
  77. }
  78. public void CopyTo(Array array, int index)
  79. {
  80. _list.CopyTo(array, index);
  81. }
  82. IEnumerator IEnumerable.GetEnumerator()
  83. {
  84. return _list.GetEnumerator();
  85. }
  86. public void Clear()
  87. {
  88. _list.Clear();
  89. }
  90. public T[] ToArray()
  91. {
  92. return (T[])_list.ToArray(new T[_list.Count]);
  93. }
  94. public T[] ToReverseArray()
  95. {
  96. T[] array = ToArray();
  97. Array.Reverse(array);
  98. return array;
  99. }
  100. public T[] Select(NodeType type)
  101. {
  102. List result = new List();
  103. foreach (Node node in _list)
  104. {
  105. if (node.NodeType == type)
  106. {
  107. result.Add(node);
  108. }
  109. }
  110. return (T[])result.ToArray(new T[result.Count]);
  111. }
  112. protected IEnumerable InternalPopRange(int begin)
  113. {
  114. return _list.PopRange(begin);
  115. }
  116. public bool ContainsNode(T node)
  117. {
  118. return _list.ContainsReference(node);
  119. }
  120. public bool Contains(Predicate condition)
  121. {
  122. return _list.Contains(condition);
  123. }
  124. public bool ContainsEntity(Boo.Lang.Compiler.TypeSystem.IEntity entity)
  125. {
  126. foreach (Node node in _list)
  127. {
  128. if (entity == node.Entity)
  129. {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. public Node RemoveByEntity(Boo.Lang.Compiler.TypeSystem.IEntity entity)
  136. {
  137. if (null == entity)
  138. {
  139. throw new ArgumentNullException("entity");
  140. }
  141. for (int i = 0; i < _list.Count; ++i)
  142. {
  143. Node node = (Node)_list[i];
  144. if (entity == node.Entity)
  145. {
  146. _list.RemoveAt(i);
  147. return node;
  148. }
  149. }
  150. return null;
  151. }
  152. public object Clone()
  153. {
  154. NodeCollection<T> clone = (NodeCollection<T>)Activator.CreateInstance(GetType());
  155. List cloneList = clone._list;
  156. foreach (Node node in _list)
  157. {
  158. cloneList.Add(node.Clone());
  159. }
  160. return clone;
  161. }
  162. public void ClearTypeSystemBindings()
  163. {
  164. foreach (Node node in _list)
  165. {
  166. node.ClearTypeSystemBindings();
  167. }
  168. }
  169. protected List InnerList
  170. {
  171. get
  172. {
  173. return _list;
  174. }
  175. }
  176. internal void InitializeParent(Node parent)
  177. {
  178. _parent = parent;
  179. foreach (Node node in _list)
  180. {
  181. node.InitializeParent(_parent);
  182. }
  183. }
  184. public void Reject(Predicate condition)
  185. {
  186. if (null == condition)
  187. {
  188. throw new ArgumentNullException("condition");
  189. }
  190. int index = 0;
  191. foreach (Node node in ToArray())
  192. {
  193. if (condition(node))
  194. {
  195. RemoveAt(index);
  196. }
  197. else
  198. {
  199. ++index;
  200. }
  201. }
  202. }
  203. public void RemoveAt(int index)
  204. {
  205. //Node existing = (Node)InnerList[index];
  206. //existing.InitializeParent(null);
  207. InnerList.RemoveAt(index);
  208. }
  209. public void ExtendWithClones(IEnumerable<T> items)
  210. {
  211. foreach (T item in items)
  212. {
  213. InnerList.Add(item.CloneNode());
  214. }
  215. }
  216. public void ReplaceAt(int i, T newItem)
  217. {
  218. //Node existing = (Node)InnerList[i];
  219. //existing.InitializeParent(null);
  220. _list[i] = newItem;
  221. Initialize(newItem);
  222. }
  223. public void Add(T item)
  224. {
  225. Initialize(item);
  226. _list.Add(item);
  227. }
  228. public void Extend(IEnumerable<T> items)
  229. {
  230. AssertNotNull("items", items);
  231. foreach (T item in items)
  232. {
  233. Add(item);
  234. }
  235. }
  236. public bool Replace(T existing, T newItem)
  237. {
  238. AssertNotNull("existing", existing);
  239. for (int i = 0; i < _list.Count; ++i)
  240. {
  241. if (this[i] == existing)
  242. {
  243. if (null == newItem)
  244. {
  245. RemoveAt(i);
  246. }
  247. else
  248. {
  249. ReplaceAt(i, newItem);
  250. }
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. public void Insert(int index, T item)
  257. {
  258. Initialize(item);
  259. InnerList.Insert(index, item);
  260. }
  261. public void Remove(Node item)
  262. {
  263. InnerList.Remove(item);
  264. }
  265. override public int GetHashCode()
  266. {
  267. return base.GetHashCode();
  268. }
  269. override public bool Equals(object rhs)
  270. {
  271. NodeCollection<T> other = rhs as NodeCollection<T>;
  272. if (null == other) return false;
  273. if (InnerList.Count != other.Count) return false;
  274. IEnumerator<T> enumerator = other.GetEnumerator();
  275. foreach (T mine in this)
  276. {
  277. if (!enumerator.MoveNext()) return false;
  278. if (!mine.Equals(enumerator.Current)) return false;
  279. }
  280. return true;
  281. }
  282. void Initialize(Node item)
  283. {
  284. AssertNotNull("item", item);
  285. if (null != _parent)
  286. {
  287. item.InitializeParent(_parent);
  288. }
  289. }
  290. private void AssertNotNull(string descrip, object o)
  291. {
  292. if (o == null)
  293. {
  294. throw new ArgumentException(
  295. String.Format("null reference for: {0}", descrip));
  296. }
  297. }
  298. }
  299. }