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

/IronPython_1_0/Src/IronPython/Runtime/Tuple.cs

#
C# | 424 lines | 309 code | 91 blank | 24 comment | 50 complexity | 2e604af4afd7b92aac6c6c39f540d3ba MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* **********************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. *
  5. * This source code is subject to terms and conditions of the Shared Source License
  6. * for IronPython. A copy of the license can be found in the License.html file
  7. * at the root of this distribution. If you can not locate the Shared Source License
  8. * for IronPython, please send an email to ironpy@microsoft.com.
  9. * By using this source code in any fashion, you are agreeing to be bound by
  10. * the terms of the Shared Source License for IronPython.
  11. *
  12. * You must not remove this notice, or any other, from this software.
  13. *
  14. * **********************************************************************************/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. using IronPython.Runtime.Types;
  20. using IronPython.Runtime.Operations;
  21. namespace IronPython.Runtime {
  22. [PythonType("tuple")]
  23. public class Tuple : ISequence, ICollection, IEnumerable, IEnumerable<object>, IComparable, IDynamicObject, IList<object> {
  24. private static Tuple EMPTY = new Tuple();
  25. #region Python Constructors
  26. // Tuples are immutable so their initialization happens in __new__
  27. // They also explicitly implement __new__ so they can perform the
  28. // appropriate caching.
  29. [PythonName("__new__")]
  30. public static Tuple PythonNew(DynamicType cls) {
  31. if (cls == TypeCache.Tuple) {
  32. return EMPTY;
  33. } else {
  34. Tuple tupObj = cls.ctor.Call(cls) as Tuple;
  35. if (tupObj == null) throw Ops.TypeError("{0} is not a subclass of tuple", cls);
  36. return tupObj;
  37. }
  38. }
  39. [PythonName("__new__")]
  40. public static Tuple PythonNew(DynamicType cls, object sequence) {
  41. if (cls == TypeCache.Tuple) {
  42. if (sequence.GetType() == typeof(Tuple)) return (Tuple)sequence;
  43. return new Tuple(MakeItems(sequence));
  44. } else {
  45. Tuple tupObj = cls.ctor.Call(cls, sequence) as Tuple;
  46. if (tupObj == null) throw Ops.TypeError("{0} is not a subclass of tuple", cls);
  47. return tupObj;
  48. }
  49. }
  50. #endregion
  51. public static Tuple Make(object o) {
  52. if (o is Tuple) return (Tuple)o;
  53. return new Tuple(MakeItems(o));
  54. }
  55. public static Tuple MakeTuple(params object[] items) {
  56. if (items.Length == 0) return EMPTY;
  57. return new Tuple(items);
  58. }
  59. internal static Tuple MakeExpandableTuple(params object[] items) {
  60. if (items.Length == 0) return EMPTY;
  61. return new Tuple(true, items);
  62. }
  63. private static object[] MakeItems(object o) {
  64. if (o is Tuple) {
  65. return ((Tuple)o).data;
  66. } else if (o is string) {
  67. string s = (string)o;
  68. object[] res = new object[s.Length];
  69. for (int i = 0; i < res.Length; i++) {
  70. res[i] = Ops.Char2String(s[i]);
  71. }
  72. return res;
  73. } else if (o is List) {
  74. return ((List)o).GetObjectArray();
  75. } else {
  76. ArrayList l = new ArrayList();
  77. IEnumerator i = Ops.GetEnumerator(o);
  78. while (i.MoveNext()) {
  79. l.Add(i.Current);
  80. }
  81. return l.ToArray();
  82. }
  83. }
  84. private readonly object[] data;
  85. private readonly bool expandable;
  86. public Tuple(object o) {
  87. this.data = MakeItems(o);
  88. }
  89. private Tuple(object[] items) {
  90. this.data = items;
  91. }
  92. public Tuple() {
  93. this.data = Ops.EMPTY;
  94. }
  95. internal Tuple(bool expandable, object[] items) {
  96. this.expandable = expandable;
  97. this.data = items;
  98. }
  99. internal Tuple(Tuple other, object o) {
  100. this.data = other.Expand(o);
  101. }
  102. internal bool IsExpandable {
  103. get {
  104. return expandable;
  105. }
  106. }
  107. /// <summary>
  108. /// Return a copy of this tuple's data array.
  109. /// </summary>
  110. internal object[] ToArray() {
  111. object[] copy = new object[data.Length];
  112. Array.Copy(data, copy, data.Length);
  113. return copy;
  114. }
  115. #region ISequence Members
  116. [PythonName("__len__")]
  117. public int GetLength() {
  118. return data.Length;
  119. }
  120. [PythonName("__contains__")]
  121. public object ContainsValueWrapper(object item) {
  122. return Ops.Bool2Object(ContainsValue(item));
  123. }
  124. public virtual bool ContainsValue(object item) {
  125. return ArrayOps.Contains(data, data.Length, item);
  126. }
  127. public virtual object this[int index] {
  128. get {
  129. return data[Ops.FixIndex(index, data.Length)];
  130. }
  131. }
  132. [PythonName("__add__")]
  133. public virtual object AddSequence(object other) {
  134. Tuple o = other as Tuple;
  135. if (o == null) throw Ops.TypeErrorForBadInstance("can only concatenate tuple (not \"{0}\") to tuple", other);
  136. return MakeTuple(ArrayOps.Add(data, data.Length, o.data, o.data.Length));
  137. }
  138. [PythonName("__iadd__")]
  139. public virtual object __iadd__(object other) {
  140. return AddSequence(other);
  141. }
  142. [PythonName("__mul__")]
  143. public virtual object MultiplySequence(object count) {
  144. return Ops.MultiplySequence<Tuple>(MultiplySequenceWorker, this, count);
  145. }
  146. [PythonName("__rmul__")]
  147. public virtual object ReverseMultiply(object count) {
  148. return MultiplySequenceWorker(this, Converter.ConvertToInt32(count));
  149. }
  150. private static Tuple MultiplySequenceWorker(Tuple self, int count) {
  151. return MakeTuple(ArrayOps.Multiply(self.data, self.data.Length, count));
  152. }
  153. [PythonName("__imul__")]
  154. public virtual object __imul__(object count) {
  155. return MultiplySequence(count);
  156. }
  157. [PythonName("__getslice__")]
  158. public virtual object GetSlice(int start, int stop) {
  159. if (start < 0) start = 0;
  160. if (stop > GetLength()) stop = GetLength();
  161. return MakeTuple(ArrayOps.GetSlice(data, start, stop));
  162. }
  163. public object this[Slice slice] {
  164. get {
  165. return Make(ArrayOps.GetSlice(data, data.Length, slice));
  166. }
  167. }
  168. #endregion
  169. #region ICollection Members
  170. public bool IsSynchronized {
  171. get { return false; }
  172. }
  173. public int Count {
  174. get { return data.Length; }
  175. }
  176. public void CopyTo(Array array, int index) {
  177. Array.Copy(data, 0, array, index, data.Length);
  178. }
  179. public object SyncRoot {
  180. get {
  181. return this;
  182. }
  183. }
  184. #endregion
  185. #region IEnumerable Members
  186. /// <summary>
  187. /// public class to get optimized
  188. /// </summary>
  189. public class TupleEnumerator : IEnumerator, IEnumerator<object> {
  190. int curIndex;
  191. Tuple tuple;
  192. public TupleEnumerator(Tuple t) {
  193. tuple = t;
  194. curIndex = -1;
  195. }
  196. #region IEnumerator Members
  197. public object Current {
  198. get { return tuple[curIndex]; }
  199. }
  200. public bool MoveNext() {
  201. if ((curIndex + 1) >= tuple.Count) {
  202. return false;
  203. }
  204. curIndex++;
  205. return true;
  206. }
  207. public void Reset() {
  208. curIndex = -1;
  209. }
  210. #endregion
  211. #region IDisposable Members
  212. public void Dispose() {
  213. }
  214. #endregion
  215. }
  216. public IEnumerator GetEnumerator() {
  217. return new TupleEnumerator(this);
  218. }
  219. #endregion
  220. [PythonName("__hash__")]
  221. public override int GetHashCode() {
  222. int ret = 6551;
  223. foreach (object o in data) {
  224. ret = (ret << 5) ^ (ret >> 26) ^ Ops.Hash(o);
  225. }
  226. return ret;
  227. }
  228. [PythonName("__eq__")]
  229. public override bool Equals(object obj) {
  230. Tuple l = obj as Tuple;
  231. if (l == null) return false;
  232. if (data.Length != l.data.Length) return false;
  233. for (int i = 0; i < data.Length; i++) {
  234. if (!Ops.EqualRetBool(data[i], l.data[i])) return false;
  235. }
  236. return true;
  237. }
  238. public int CompareTo(object other) {
  239. Tuple l = other as Tuple;
  240. if (l == null) throw new ArgumentException("expected list");
  241. return Ops.CompareArrays(data, data.Length, l.data, l.data.Length);
  242. }
  243. [PythonName("__str__")]
  244. public override string ToString() {
  245. StringBuilder buf = new StringBuilder();
  246. buf.Append("(");
  247. for (int i = 0; i < data.Length; i++) {
  248. if (i > 0) buf.Append(", ");
  249. buf.Append(Ops.StringRepr(data[i]));
  250. }
  251. if (data.Length == 1) buf.Append(",");
  252. buf.Append(")");
  253. return buf.ToString();
  254. }
  255. internal object[] Expand(object value) {
  256. object[] args;
  257. int length = data.Length;
  258. if (value == null)
  259. args = new object[length];
  260. else
  261. args = new object[length + 1];
  262. for (int i = 0; i < length; i++) {
  263. args[i] = data[i];
  264. }
  265. if (value != null) {
  266. args[length] = value;
  267. }
  268. return args;
  269. }
  270. [PythonName("__getnewargs__")]
  271. public object GetNewArgs() {
  272. // Call "new Tuple()" to force result to be a Tuple (otherwise, it could possibly be a Tuple subclass)
  273. return Tuple.MakeTuple(new Tuple(this));
  274. }
  275. #region IEnumerable<object> Members
  276. IEnumerator<object> IEnumerable<object>.GetEnumerator() {
  277. return new TupleEnumerator(this);
  278. }
  279. #endregion
  280. #region IDynamicObject Members
  281. public DynamicType GetDynamicType() {
  282. return TypeCache.Tuple;
  283. }
  284. #endregion
  285. #region IList<object> Members
  286. int IList<object>.IndexOf(object item) {
  287. for (int i = 0; i < Count; i++) {
  288. if (Ops.EqualRetBool(this[i], item)) return i;
  289. }
  290. return -1;
  291. }
  292. void IList<object>.Insert(int index, object item) {
  293. throw new InvalidOperationException("Tuple is readonly");
  294. }
  295. void IList<object>.RemoveAt(int index) {
  296. throw new InvalidOperationException("Tuple is readonly");
  297. }
  298. object IList<object>.this[int index] {
  299. get {
  300. return this[index];
  301. }
  302. set {
  303. throw new InvalidOperationException("Tuple is readonly");
  304. }
  305. }
  306. #endregion
  307. #region ICollection<object> Members
  308. void ICollection<object>.Add(object item) {
  309. throw new InvalidOperationException("Tuple is readonly");
  310. }
  311. void ICollection<object>.Clear() {
  312. throw new InvalidOperationException("Tuple is readonly");
  313. }
  314. bool ICollection<object>.Contains(object item) {
  315. return this.ContainsValue(item);
  316. }
  317. void ICollection<object>.CopyTo(object[] array, int arrayIndex) {
  318. for (int i = 0; i < Count; i++) {
  319. array[arrayIndex + i] = this[i];
  320. }
  321. }
  322. int ICollection<object>.Count {
  323. get { return this.Count; }
  324. }
  325. bool ICollection<object>.IsReadOnly {
  326. get { return true; }
  327. }
  328. bool ICollection<object>.Remove(object item) {
  329. throw new InvalidOperationException("Tuple is readonly");
  330. }
  331. #endregion
  332. }
  333. }