PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/DLR_Main/Languages/IronPython/IronPythonTest/Indexable.cs

https://bitbucket.org/mdavid/dlr
C# | 663 lines | 559 code | 88 blank | 16 comment | 116 complexity | 154bbb6ad5af39962a99fac6365a00a6 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. namespace IronPythonTest {
  19. public class Tuple {
  20. object[] val;
  21. public Tuple(object a) {
  22. val = new object[] { a };
  23. }
  24. public Tuple(object a, object b) {
  25. val = new object[] { a, b };
  26. }
  27. public Tuple(object a, object b, object c) {
  28. val = new object[] { a, b, c };
  29. }
  30. public Tuple(object a, object b, object c, object d) {
  31. val = new object[] { a, b, c, d };
  32. }
  33. public Tuple(object a, object b, object c, object d, object e) {
  34. val = new object[] { a, b, c, d, e };
  35. }
  36. private static bool Compare(object x, object y) {
  37. if (x != null) {
  38. if (y != null) {
  39. return x.Equals(y);
  40. } else return false;
  41. } else {
  42. return y == null;
  43. }
  44. }
  45. public override bool Equals(object obj) {
  46. Tuple other = obj as Tuple;
  47. if (other == null) return false;
  48. if (val != null) {
  49. if (other.val != null) {
  50. if (val.Length != other.val.Length) {
  51. return false;
  52. }
  53. for (int i = 0; i < val.Length; i++) {
  54. if (!Compare(val[i], other.val[i])) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. } else return false;
  60. } else return other.val == null;
  61. }
  62. public override int GetHashCode() {
  63. int hash = 0;
  64. if (val != null && val.Length > 0) {
  65. hash = val[0].GetHashCode();
  66. for (int i = 1; i < val.Length; i++) {
  67. hash ^= val[i].GetHashCode();
  68. }
  69. }
  70. return hash;
  71. }
  72. }
  73. public struct StructIndexable {
  74. private List<int?> _data;
  75. public int? this[int index] {
  76. get {
  77. if (_data == null || _data.Count < index) {
  78. return null;
  79. }
  80. return _data[index];
  81. }
  82. set {
  83. if (_data == null) {
  84. _data = new List<int?>();
  85. }
  86. while (_data.Count <= index) {
  87. _data.Add(null);
  88. }
  89. _data[index] = value;
  90. }
  91. }
  92. }
  93. public class Indexable {
  94. #if !SILVERLIGHT
  95. System.Collections.Hashtable ht = new System.Collections.Hashtable();
  96. #else
  97. Dictionary<object, object> ht = new Dictionary<object,object>();
  98. #endif
  99. public string this[int index] {
  100. get {
  101. return (string)ht[index];
  102. }
  103. set {
  104. ht[index] = value;
  105. }
  106. }
  107. public string this[string index] {
  108. get {
  109. return (string)ht[index];
  110. }
  111. set {
  112. ht[index] = value;
  113. }
  114. }
  115. public string this[double index] {
  116. get {
  117. return (string)ht[index];
  118. }
  119. set {
  120. ht[index] = value;
  121. }
  122. }
  123. public string this[int i, int j] {
  124. get {
  125. string ret = (string)ht[new Tuple(i, j)];
  126. return ret;
  127. }
  128. set {
  129. ht[new Tuple(i, j)] = value;
  130. }
  131. }
  132. public string this[int i, double j] {
  133. get {
  134. return (string)ht[new Tuple(i, j)];
  135. }
  136. set {
  137. ht[new Tuple(i, j)] = value;
  138. }
  139. }
  140. public string this[int i, string j] {
  141. get {
  142. return (string)ht[new Tuple(i, j)];
  143. }
  144. set {
  145. ht[new Tuple(i, j)] = value;
  146. }
  147. }
  148. public string this[double i, int j] {
  149. get {
  150. return (string)ht[new Tuple(i, j)];
  151. }
  152. set {
  153. ht[new Tuple(i, j)] = value;
  154. }
  155. }
  156. public string this[double i, double j] {
  157. get {
  158. return (string)ht[new Tuple(i, j)];
  159. }
  160. set {
  161. ht[new Tuple(i, j)] = value;
  162. }
  163. }
  164. public string this[double i, string j] {
  165. get {
  166. return (string)ht[new Tuple(i, j)];
  167. }
  168. set {
  169. ht[new Tuple(i, j)] = value;
  170. }
  171. }
  172. public string this[string i, int j] {
  173. get {
  174. return (string)ht[new Tuple(i, j)];
  175. }
  176. set {
  177. ht[new Tuple(i, j)] = value;
  178. }
  179. }
  180. public string this[string i, double j] {
  181. get {
  182. return (string)ht[new Tuple(i, j)];
  183. }
  184. set {
  185. ht[new Tuple(i, j)] = value;
  186. }
  187. }
  188. public string this[string i, string j] {
  189. get {
  190. return (string)ht[new Tuple(i, j)];
  191. }
  192. set {
  193. ht[new Tuple(i, j)] = value;
  194. }
  195. }
  196. }
  197. public class IndexableList : IList {
  198. List<object> myList = new List<object>();
  199. public object this[string index] {
  200. get {
  201. return myList[Int32.Parse(index)];
  202. }
  203. set {
  204. myList[Int32.Parse(index)] = value;
  205. }
  206. }
  207. #region IList Members
  208. public int Add(object value) {
  209. int res = myList.Count;
  210. myList.Add(value);
  211. return res;
  212. }
  213. public void Clear() {
  214. myList.Clear();
  215. }
  216. public bool Contains(object value) {
  217. return myList.Contains(value);
  218. }
  219. public int IndexOf(object value) {
  220. return myList.IndexOf(value);
  221. }
  222. public void Insert(int index, object value) {
  223. myList.Insert(index, value);
  224. }
  225. public bool IsFixedSize {
  226. get { return false; }
  227. }
  228. public bool IsReadOnly {
  229. get { return false; }
  230. }
  231. public void Remove(object value) {
  232. myList.Remove(value);
  233. }
  234. public void RemoveAt(int index) {
  235. myList.RemoveAt(index);
  236. }
  237. public object this[int index] {
  238. get {
  239. return myList[index];
  240. }
  241. set {
  242. myList[index] = value;
  243. }
  244. }
  245. #endregion
  246. #region ICollection Members
  247. public void CopyTo(Array array, int index) {
  248. throw new NotImplementedException();
  249. }
  250. public int Count {
  251. get { return myList.Count; }
  252. }
  253. public bool IsSynchronized {
  254. get { return false; }
  255. }
  256. public object SyncRoot {
  257. get { throw new Exception("The method or operation is not implemented."); }
  258. }
  259. #endregion
  260. #region IEnumerable Members
  261. public IEnumerator GetEnumerator() {
  262. return myList.GetEnumerator();
  263. }
  264. #endregion
  265. }
  266. public class PropertyAccessClass {
  267. public int this[int i] {
  268. get {
  269. return i;
  270. }
  271. set {
  272. if (i != value) {
  273. throw new IndexOutOfRangeException();
  274. }
  275. }
  276. }
  277. public int this[int i, int j] {
  278. get {
  279. return i + j;
  280. }
  281. set {
  282. if (i + j != value) {
  283. throw new IndexOutOfRangeException();
  284. }
  285. }
  286. }
  287. public int this[int i, int j, int k] {
  288. get {
  289. return i + j + k;
  290. }
  291. set {
  292. if (i + j + k != value) {
  293. throw new IndexOutOfRangeException();
  294. }
  295. }
  296. }
  297. }
  298. public class MultipleIndexes {
  299. Dictionary<object, object> ht = new Dictionary<object, object>();
  300. // works like Hashtable indexer--returns null instead of throwing if the key isn't found
  301. private object GetValue(Tuple t) {
  302. object result;
  303. ht.TryGetValue(t, out result);
  304. return result;
  305. }
  306. public object this[object i] {
  307. get {
  308. return GetValue(new Tuple(i));
  309. }
  310. set {
  311. ht[new Tuple(i)] = value;
  312. }
  313. }
  314. public object this[object i, object j] {
  315. get {
  316. return GetValue(new Tuple(i, j));
  317. }
  318. set {
  319. ht[new Tuple(i, j)] = value;
  320. }
  321. }
  322. public object this[object i, object j, object k] {
  323. get {
  324. return GetValue(new Tuple(i, j, k));
  325. }
  326. set {
  327. ht[new Tuple(i, j, k)] = value;
  328. }
  329. }
  330. public object this[object i, object j, object k, object l] {
  331. get {
  332. return GetValue(new Tuple(i, j, k, l));
  333. }
  334. set {
  335. ht[new Tuple(i, j, k, l)] = value;
  336. }
  337. }
  338. public object this[object i, object j, object k, object l, object m] {
  339. get {
  340. return GetValue(new Tuple(i, j, k, l, m));
  341. }
  342. set {
  343. ht[new Tuple(i, j, k, l, m)] = value;
  344. }
  345. }
  346. }
  347. #if !SILVERLIGHT // TODO: LastIndexOf
  348. public class UsePythonListAsList {
  349. IList<int> list;
  350. public UsePythonListAsList(IList<int> list) {
  351. this.list = list;
  352. }
  353. public int Inspect() {
  354. list.Clear();
  355. for (int i = 0; i < 100; i++) list.Add(i);
  356. int flag = 0;
  357. if (list.IndexOf(5) == 5) flag += 1;
  358. if (list.IndexOf(1000) == -1) flag += 10;
  359. if (list[5] == 5) flag += 100;
  360. if (list.Remove(5)) flag += 1000;
  361. if (list.IndexOf(5) == -1) flag += 10000;
  362. return flag;
  363. }
  364. public void AddRemove() {
  365. int value = 20;
  366. list.Insert(0, value);
  367. list.Insert(3, value);
  368. list.Insert(5, value);
  369. list.Insert(list.Count, value);
  370. list[list.Count / 2] = value;
  371. list.RemoveAt(2);
  372. list.RemoveAt(list.Count - 1);
  373. }
  374. public int Loop() {
  375. int sum = 0;
  376. foreach (int t in list) sum += t;
  377. return sum;
  378. }
  379. }
  380. public class UsePythonListAsArrayList {
  381. ArrayList list;
  382. public UsePythonListAsArrayList(ArrayList list) {
  383. this.list = list;
  384. }
  385. public void AddRemove() {
  386. foreach (object o in new object[] { 1, 2L, "string", typeof(int) }) {
  387. list.Add(o);
  388. }
  389. list.Remove(2L);
  390. list.RemoveAt(0);
  391. list.RemoveRange(1, 2);
  392. list.Reverse();
  393. list.InsertRange(1, new object[] { 100, 30.4, (byte)3 });
  394. list.SetRange(list.Count - 2, new object[] { (ushort)20, -200 });
  395. list.Reverse();
  396. //list.Capacity = list.Count / 2;
  397. }
  398. public int Inspect() {
  399. list.Clear();
  400. for (int i = 0; i < 10; i++) list.Add(i);
  401. list.AddRange(new string[] { "a", "bc" });
  402. int flag = 0;
  403. if (list.IndexOf("a") == 10
  404. && list.IndexOf(4, 3) == 4 && list.IndexOf(4, 6) == -1
  405. && list.IndexOf(3, 1, 4) == 3 && list.IndexOf(4, 1, 10) == 4
  406. && list.IndexOf(3, 7, 3) == -1
  407. && list.LastIndexOf("a") == 10 && list.LastIndexOf("bc") == 11
  408. && list.LastIndexOf(4, 5) == 4
  409. && list.LastIndexOf(4, 6, 2) == -1 && list.LastIndexOf(4, 6, 3) == 4
  410. && list.LastIndexOf(1, 6, 5) == -1 && list.LastIndexOf(1, 6, 6) == 1
  411. && list.LastIndexOf(0, 6, 6) == -1 && list.LastIndexOf(0, 6, 7) == 0
  412. )
  413. flag += 1;
  414. if (list.Count == 12) flag += 10;
  415. if (list.Contains(8)) flag += 100;
  416. if (list.Contains("abc") == false) flag += 1000;
  417. if (list.BinarySearch("5", new MyComparer()) == 5
  418. && list.BinarySearch(0, 10, "a", new MyComparer()) == -1
  419. && list.BinarySearch(0, 10, 5, new MyComparer()) == 5
  420. )
  421. flag += 10000;
  422. return flag;
  423. }
  424. public int Loop(out string strs) {
  425. strs = null;
  426. int sum = 0;
  427. foreach (object o in list) {
  428. if (o is int) {
  429. sum += (int)o;
  430. } else if (o is string) {
  431. strs += (string)o;
  432. }
  433. }
  434. return sum;
  435. }
  436. class MyComparer : IComparer {
  437. public int Compare(object x, object y) {
  438. return string.Compare(x.ToString(), y.ToString());
  439. }
  440. }
  441. }
  442. #endif
  443. public class UsePythonDictAsDictionary {
  444. IDictionary<string, int> dict;
  445. public UsePythonDictAsDictionary(IDictionary<string, int> dict) {
  446. this.dict = dict;
  447. }
  448. public void AddRemove() {
  449. dict.Add("hello", 1000);
  450. dict.Add(new KeyValuePair<string, int>("world", 2000));
  451. dict.Add("python", 3000);
  452. dict.Remove("python");
  453. }
  454. public int Inspect(out string keys, out int values) {
  455. dict.Clear();
  456. for (int i = 0; i < 10; i++)
  457. dict.Add(i.ToString(), i);
  458. int flag = 0;
  459. if (dict.ContainsKey("5")) flag += 1;
  460. if (dict["5"] == 5) flag += 10;
  461. if (dict.Count == 10) flag += 100;
  462. int val;
  463. if (dict.TryGetValue("6", out val)) flag += 1000;
  464. if (dict.TryGetValue("spam", out val) == false) flag += 10000;
  465. keys = string.Empty;
  466. foreach (string s in SortedArray(dict.Keys, null)) {
  467. keys += s;
  468. }
  469. values = 0;
  470. foreach (int v in dict.Values) {
  471. values += v;
  472. }
  473. return flag;
  474. }
  475. private T[] SortedArray<T>(ICollection<T> value, Comparison<T> comparer) {
  476. T[] array = new T[value.Count];
  477. value.CopyTo(array, 0);
  478. if (comparer != null) {
  479. Array.Sort(array, comparer);
  480. } else {
  481. Array.Sort(array);
  482. }
  483. return array;
  484. }
  485. private static int KeyValueComparer(KeyValuePair<string, int> x, KeyValuePair<string, int> y) {
  486. return String.Compare(x.Key, y.Key);
  487. }
  488. public void Loop(out string keys, out int values) {
  489. keys = string.Empty;
  490. values = 0;
  491. foreach (KeyValuePair<string, int> pair in SortedArray(dict, KeyValueComparer)) {
  492. keys += pair.Key;
  493. values += pair.Value;
  494. }
  495. }
  496. }
  497. public class CSharpEnumerable {
  498. public IEnumerable<int> GetEnumerableOfInt() {
  499. yield return 1;
  500. yield return 2;
  501. yield return 3;
  502. }
  503. public IEnumerable<object> GetEnumerableOfObject() {
  504. yield return 1;
  505. yield return 2;
  506. yield return 3;
  507. }
  508. public IEnumerable GetEnumerable() {
  509. yield return 1;
  510. yield return 2;
  511. yield return 3;
  512. }
  513. public IEnumerator<int> GetEnumeratorOfInt() {
  514. yield return 1;
  515. yield return 2;
  516. yield return 3;
  517. }
  518. public IEnumerator<object> GetEnumeratorOfObject() {
  519. yield return 1;
  520. yield return 2;
  521. yield return 3;
  522. }
  523. public IEnumerator GetEnumerator() {
  524. yield return 1;
  525. yield return 2;
  526. yield return 3;
  527. }
  528. }
  529. #if !SILVERLIGHT // TODO: this doesn't seem to be used anywhere--remove?
  530. public class UsePythonDictAsHashtable {
  531. Hashtable table;
  532. public UsePythonDictAsHashtable(Hashtable table) {
  533. this.table = table;
  534. }
  535. public void AddRemove() {
  536. table.Add(200, 400);
  537. table.Add("spam", "spam");
  538. table.Remove("spam");
  539. }
  540. public int Inspect(out int keysum, out int valuesum) {
  541. table.Clear();
  542. for (int i = 0; i < 10; i++) {
  543. table.Add(i, i * i);
  544. }
  545. int flag = 0;
  546. if (table.Contains(0)) flag += 1;
  547. if (table.Contains("0") == false) flag += 10;
  548. if (table.ContainsKey(3)) flag += 100;
  549. if (table.ContainsValue(81)) flag += 1000;
  550. if ((int)table[8] == 64) flag += 10000;
  551. table[8] = 89;
  552. if ((int)table[8] == 89) flag += 100000;
  553. if (table.Count == 10) flag += 1000000;
  554. keysum = 0;
  555. foreach (object o in table.Keys) {
  556. keysum += (int)o;
  557. }
  558. valuesum = 0;
  559. foreach (object o in table.Values) {
  560. valuesum += (int)o;
  561. }
  562. return flag;
  563. }
  564. public int Loop() {
  565. int sum = 0;
  566. IDictionaryEnumerator ide = table.GetEnumerator();
  567. while (ide.MoveNext()) {
  568. sum += (int)ide.Value;
  569. sum += (int)ide.Key;
  570. }
  571. return sum;
  572. }
  573. }
  574. #endif
  575. }