PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/otp.net/Otp/Erlang/List.cs

https://github.com/bmizerany/jungerl
C# | 275 lines | 134 code | 34 blank | 107 comment | 24 complexity | 553ba48b046c22cfc03d48513fdd9059 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, AGPL-1.0
  1. /*``The contents of this file are subject to the Erlang Public License,
  2. * Version 1.1, (the "License"); you may not use this file except in
  3. * compliance with the License. You should have received a copy of the
  4. * Erlang Public License along with this software. If not, it can be
  5. * retrieved via the world wide web at http://www.erlang.org/.
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. * the License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. * AB. All Rights Reserved.''
  15. *
  16. * Converted from Java to C# by Vlad Dumitrescu (vlad_Dumitrescu@hotmail.com)
  17. */
  18. namespace Otp.Erlang
  19. {
  20. using System;
  21. /*
  22. * Provides a C# representation of Erlang lists. Lists are created
  23. * from zero or more arbitrary Erlang terms.
  24. *
  25. * <p> The arity of the list is the number of elements it contains.
  26. **/
  27. [Serializable]
  28. public class List:Erlang.Object
  29. {
  30. private Object[] elems = null;
  31. // todo: use a linked structure to make a proper list with append,
  32. // car, cdr etc methods. The current representation is essensially the
  33. // same as for tuples except that empty lists are allowed.
  34. // private Object car = null;
  35. // private Object cdr = null;
  36. // int arity;
  37. /*
  38. * Create an empty list.
  39. **/
  40. public List()
  41. {
  42. this.elems = null; // empty list
  43. }
  44. /*
  45. * Create a list of characters.
  46. *
  47. * @param str the characters from which to create the list.
  48. **/
  49. public List(System.String str)
  50. {
  51. int len = 0;
  52. if (str != null)
  53. len = str.Length;
  54. if (len > 0)
  55. {
  56. this.elems = new Object[len];
  57. for (int i = 0; i < len; i++)
  58. {
  59. this.elems[i] = new Char(str[i]);
  60. }
  61. }
  62. }
  63. /*
  64. * Create a list containing one element.
  65. *
  66. * @param elem the elememet to make the list from.
  67. **/
  68. public List(Object elem)
  69. {
  70. this.elems = new Object[1];
  71. elems[0] = elem;
  72. }
  73. /*
  74. * Create a list from an array of arbitrary Erlang terms.
  75. *
  76. * @param elems the array of terms from which to create the list.
  77. **/
  78. public List(Object[] elems):this(elems, 0, elems.Length)
  79. {
  80. }
  81. /*
  82. * Create a list from an array of arbitrary Erlang terms.
  83. *
  84. * @param elems the array of terms from which to create the list.
  85. * @param start the offset of the first term to insert.
  86. * @param count the number of terms to insert.
  87. **/
  88. public List(Object[] elems, int start, int count)
  89. {
  90. if ((elems != null) && (count > 0))
  91. {
  92. this.elems = new Object[count];
  93. Array.Copy(elems, start, this.elems, 0, count);
  94. }
  95. }
  96. /*
  97. * Create a list from a stream containing an list encoded in Erlang
  98. * external format.
  99. *
  100. * @param buf the stream containing the encoded list.
  101. *
  102. * @exception DecodeException if the buffer does not
  103. * contain a valid external representation of an Erlang list.
  104. **/
  105. public List(OtpInputStream buf)
  106. {
  107. this.elems = null;
  108. int arity = buf.read_list_head();
  109. if (arity > 0)
  110. {
  111. this.elems = new Object[arity];
  112. for (int i = 0; i < arity; i++)
  113. {
  114. elems[i] = buf.read_any();
  115. }
  116. /*discard the terminating nil (empty list) */
  117. buf.read_nil();
  118. }
  119. }
  120. /*
  121. * Get the arity of the list.
  122. *
  123. * @return the number of elements contained in the list.
  124. **/
  125. public virtual int arity()
  126. {
  127. if (elems == null)
  128. return 0;
  129. else
  130. return elems.Length;
  131. }
  132. /*
  133. * Get the specified element from the list.
  134. *
  135. * @param i the index of the requested element. List elements are
  136. * numbered as array elements, starting at 0.
  137. *
  138. * @return the requested element, of null if i is not a valid
  139. * element index.
  140. **/
  141. public virtual Object elementAt(int i)
  142. {
  143. if ((i >= arity()) || (i < 0))
  144. return null;
  145. return elems[i];
  146. }
  147. /*
  148. * Get all the elements from the list as an array.
  149. *
  150. * @return an array containing all of the list's elements.
  151. **/
  152. public virtual Object[] elements()
  153. {
  154. if (arity() == 0)
  155. return null;
  156. else
  157. {
  158. Object[] res = new Object[arity()];
  159. Array.Copy(this.elems, 0, res, 0, res.Length);
  160. return res;
  161. }
  162. }
  163. /*
  164. * Get the string representation of the list.
  165. *
  166. * @return the string representation of the list.
  167. **/
  168. public override System.String ToString()
  169. {
  170. System.Text.StringBuilder s = new System.Text.StringBuilder();
  171. int _arity = arity();
  172. s.Append("[");
  173. for (int i = 0; i < _arity; i++)
  174. {
  175. if (i > 0)
  176. s.Append(",");
  177. s.Append(elems[i].ToString());
  178. }
  179. s.Append("]");
  180. return s.ToString();
  181. }
  182. /*
  183. * Convert this list to the equivalent Erlang external
  184. * representation. Note that this method never encodes lists as
  185. * strings, even when it is possible to do so.
  186. *
  187. * @param buf An output stream to which the encoded list should be
  188. * written.
  189. *
  190. **/
  191. public override void encode(OtpOutputStream buf)
  192. {
  193. int _arity = arity();
  194. if (_arity > 0)
  195. {
  196. buf.write_list_head(_arity);
  197. for (int i = 0; i < _arity; i++)
  198. {
  199. buf.write_any(elems[i]);
  200. }
  201. }
  202. buf.write_nil();
  203. }
  204. /*
  205. * Determine if two lists are equal. Lists are equal if they have
  206. * the same arity and all of the elements are equal.
  207. *
  208. * @param o the list to compare to.
  209. *
  210. * @return true if the lists have the same arity and all the
  211. * elements are equal.
  212. **/
  213. public override bool Equals(System.Object o)
  214. {
  215. if (!(o is List))
  216. return false;
  217. List l = (List) o;
  218. int a = this.arity();
  219. if (a != l.arity())
  220. return false;
  221. for (int i = 0; i < a; i++)
  222. {
  223. if (!this.elems[i].Equals(l.elems[i]))
  224. return false;
  225. // early exit
  226. }
  227. return true;
  228. }
  229. public override int GetHashCode()
  230. {
  231. return 1;
  232. }
  233. public override System.Object clone()
  234. {
  235. List newList = (List) (base.clone());
  236. newList.elems = new Object[elems.Length];
  237. elems.CopyTo(newList.elems, 0);
  238. return newList;
  239. }
  240. }
  241. }