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

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

		public override int GetHashCode()
		{
			return 1;
		}

		public override System.Object clone()
		{
			List newList = (List) (base.clone());
			newList.elems = new Object[elems.Length];
			elems.CopyTo(newList.elems, 0);
			return newList;
		}
	}
}
Back to Top