/Util/MyDictionary.cs

https://bitbucket.org/henderea/popupmultibox · C# · 515 lines · 450 code · 59 blank · 6 comment · 21 complexity · 69c4d633e288a0ca8c29aadd9beff5f2 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. // ReSharper disable CheckNamespace
  6. namespace Henderson.Util.MyDictionary
  7. // ReSharper restore CheckNamespace
  8. {
  9. public class MyDictionary : IEnumerable<MyDictionary>
  10. {
  11. readonly Dictionary<MyKey, MyDictionary> dict;
  12. object val;
  13. MyKey myKey;
  14. public MyDictionary this[MyKey key]
  15. {
  16. get
  17. {
  18. try
  19. {
  20. val = null;
  21. return dict[key];
  22. }
  23. catch
  24. {
  25. try
  26. {
  27. dict[key] = new MyDictionary();
  28. dict[key].myKey = key;
  29. return dict[key];
  30. }
  31. catch { }
  32. return null;
  33. }
  34. }
  35. set
  36. {
  37. try
  38. {
  39. dict[key] = value;
  40. dict[key].myKey = key;
  41. }
  42. catch { }
  43. }
  44. }
  45. public static implicit operator KeyValuePair<MyKey, MyDictionary>(MyDictionary m)
  46. {
  47. try
  48. {
  49. return new KeyValuePair<MyKey, MyDictionary>(m.myKey, m);
  50. }
  51. catch { }
  52. return default(KeyValuePair<MyKey, MyDictionary>);
  53. }
  54. public static implicit operator MyDictionary(KeyValuePair<MyKey, MyDictionary> o)
  55. {
  56. try
  57. {
  58. MyDictionary d = o.Value;
  59. d.myKey = o.Key;
  60. return d;
  61. }
  62. catch { }
  63. return null;
  64. }
  65. public static implicit operator string(MyDictionary m)
  66. {
  67. try
  68. {
  69. return m.val + "";
  70. }
  71. catch { }
  72. return "";
  73. }
  74. public static implicit operator MyDictionary(string o)
  75. {
  76. MyDictionary d = new MyDictionary();
  77. try
  78. {
  79. d.val = o;
  80. return d;
  81. }
  82. catch { }
  83. return null;
  84. }
  85. public static implicit operator int(MyDictionary m)
  86. {
  87. try
  88. {
  89. return (int)((double)m.val);
  90. }
  91. catch { }
  92. return 0;
  93. }
  94. public static implicit operator MyDictionary(int o)
  95. {
  96. MyDictionary d = new MyDictionary();
  97. try
  98. {
  99. d.val = (double)o;
  100. return d;
  101. }
  102. catch { }
  103. return null;
  104. }
  105. public static implicit operator long(MyDictionary m)
  106. {
  107. try
  108. {
  109. return (long)((double)m.val);
  110. }
  111. catch { }
  112. return 0;
  113. }
  114. public static implicit operator MyDictionary(long o)
  115. {
  116. MyDictionary d = new MyDictionary();
  117. try
  118. {
  119. d.val = (double)o;
  120. return d;
  121. }
  122. catch { }
  123. return null;
  124. }
  125. public static implicit operator double(MyDictionary m)
  126. {
  127. try
  128. {
  129. return (double)m.val;
  130. }
  131. catch { }
  132. return 0;
  133. }
  134. public static implicit operator MyDictionary(double o)
  135. {
  136. MyDictionary d = new MyDictionary();
  137. try
  138. {
  139. d.val = o;
  140. return d;
  141. }
  142. catch { }
  143. return null;
  144. }
  145. public static implicit operator bool(MyDictionary m)
  146. {
  147. try
  148. {
  149. return (bool)m.val;
  150. }
  151. catch { }
  152. return false;
  153. }
  154. public static implicit operator MyDictionary(bool o)
  155. {
  156. MyDictionary d = new MyDictionary();
  157. try
  158. {
  159. d.val = o;
  160. return d;
  161. }
  162. catch { }
  163. return null;
  164. }
  165. public static implicit operator DateTime(MyDictionary m)
  166. {
  167. try
  168. {
  169. return (DateTime)m.val;
  170. }
  171. catch { }
  172. return default(DateTime);
  173. }
  174. public static implicit operator MyDictionary(DateTime o)
  175. {
  176. MyDictionary d = new MyDictionary();
  177. try
  178. {
  179. d.val = o;
  180. return d;
  181. }
  182. catch { }
  183. return null;
  184. }
  185. public static implicit operator MyDictionary(MyKey m)
  186. {
  187. try
  188. {
  189. MyDictionary d = new MyDictionary();
  190. d.val = m.Key;
  191. return d;
  192. }
  193. catch { }
  194. return null;
  195. }
  196. public object Value
  197. {
  198. get
  199. {
  200. return val;
  201. }
  202. }
  203. public MyDictionary[] Values
  204. {
  205. get
  206. {
  207. return dict.Values.ToArray();
  208. }
  209. }
  210. public object[] OValues
  211. {
  212. get
  213. {
  214. List<object> lst = new List<object>(0);
  215. foreach (MyDictionary d in dict.Values)
  216. {
  217. if (d.Value != null)
  218. lst.Add(d.Value);
  219. lst.Add(d);
  220. }
  221. return lst.ToArray();
  222. }
  223. }
  224. public string[] SValues
  225. {
  226. get
  227. {
  228. List<string> lst = new List<string>(0);
  229. lst.AddRange(dict.Values.Select(d => (string) d));
  230. return lst.ToArray();
  231. }
  232. }
  233. public MyKey[] Keys
  234. {
  235. get
  236. {
  237. return dict.Keys.ToArray();
  238. }
  239. }
  240. public string[] SKeys
  241. {
  242. get
  243. {
  244. List<string> lst = new List<string>(0);
  245. lst.AddRange(dict.Keys.Select(k => (string) k));
  246. return lst.ToArray();
  247. }
  248. }
  249. // ReSharper disable InconsistentNaming
  250. public int[] IKeys
  251. // ReSharper restore InconsistentNaming
  252. {
  253. get
  254. {
  255. List<int> lst = new List<int>(0);
  256. foreach (MyKey k in dict.Keys)
  257. {
  258. try
  259. {
  260. lst.Add(int.Parse(k+""));
  261. }
  262. catch { }
  263. }
  264. return lst.ToArray();
  265. }
  266. }
  267. public MyDictionary()
  268. {
  269. dict = new Dictionary<MyKey, MyDictionary>(0);
  270. }
  271. public override bool Equals(object obj)
  272. {
  273. if (obj is MyKey)
  274. return obj.Equals(this);
  275. if (obj is MyDictionary && ((MyDictionary)obj).Value != null)
  276. return ((MyDictionary)obj).Value.Equals(Value);
  277. if (obj is MyDictionary)
  278. {
  279. try
  280. {
  281. return ((MyDictionary)obj).dict.Equals(dict);
  282. }
  283. catch { }
  284. return false;
  285. }
  286. if (obj is string)
  287. return ((string)obj).Equals(this);
  288. if (obj is int)
  289. return ((int)obj).Equals(this);
  290. if (obj is double)
  291. return ((double)obj).Equals(this);
  292. if (obj is bool)
  293. return ((bool)obj).Equals(this);
  294. return false;
  295. }
  296. public override int GetHashCode()
  297. {
  298. try
  299. {
  300. return val.GetHashCode();
  301. }
  302. catch { }
  303. return base.GetHashCode();
  304. }
  305. #region IEnumerable Members
  306. IEnumerator IEnumerable.GetEnumerator()
  307. {
  308. return new MyValueIterator(dict);
  309. }
  310. #endregion
  311. #region IEnumerable<MyDictionary> Members
  312. IEnumerator<MyDictionary> IEnumerable<MyDictionary>.GetEnumerator()
  313. {
  314. return new MyValueIterator(dict);
  315. }
  316. #endregion
  317. public string MakeRepresentationString()
  318. {
  319. return MakeRepresentationString(4);
  320. }
  321. public string MakeRepresentationString(int indentAmount)
  322. {
  323. if (indentAmount <= 0)
  324. indentAmount = 1;
  325. return MakeKeyValuePairsString(this, 0, indentAmount);
  326. }
  327. private static string MakeKeyValuePairsString(MyDictionary dict, int lv, int indentAmount)
  328. {
  329. string rval = "";
  330. foreach (KeyValuePair<MyKey, MyDictionary> d in dict)
  331. {
  332. for (int i = 0; i < lv * indentAmount; i++)
  333. {
  334. rval += " ";
  335. }
  336. rval += d.Key + ": " + d.Value + "\n";
  337. if (d.Value.Value == null)
  338. rval += MakeKeyValuePairsString(d.Value, lv + 1, indentAmount);
  339. }
  340. return rval;
  341. }
  342. }
  343. public class MyKey
  344. {
  345. // ReSharper disable FieldCanBeMadeReadOnly.Local
  346. object key;
  347. // ReSharper restore FieldCanBeMadeReadOnly.Local
  348. public object Key
  349. {
  350. get
  351. {
  352. return key;
  353. }
  354. }
  355. private MyKey(object key)
  356. {
  357. this.key = key;
  358. }
  359. public static implicit operator string(MyKey m)
  360. {
  361. try
  362. {
  363. return m.key + "";
  364. }
  365. catch { }
  366. return "";
  367. }
  368. public static implicit operator MyKey(string o)
  369. {
  370. return new MyKey(o);
  371. }
  372. public static implicit operator int(MyKey m)
  373. {
  374. try
  375. {
  376. return (int)((double)m.key);
  377. }
  378. catch { }
  379. return 0;
  380. }
  381. public static implicit operator MyKey(int o)
  382. {
  383. return new MyKey((double)o);
  384. }
  385. public static implicit operator MyKey(MyDictionary o)
  386. {
  387. try
  388. {
  389. return new MyKey(o.Value);
  390. }
  391. catch { }
  392. return null;
  393. }
  394. public override bool Equals(object obj)
  395. {
  396. if (obj is MyKey)
  397. return ((MyKey)obj).key.Equals(key);
  398. if (obj is MyDictionary && ((MyDictionary)obj).Value != null)
  399. return ((MyDictionary)obj).Value.Equals(key);
  400. if (obj is string)
  401. return ((string)obj).Equals(this);
  402. if (obj is int)
  403. return ((int)obj).Equals(this);
  404. return false;
  405. }
  406. public override int GetHashCode()
  407. {
  408. try
  409. {
  410. return key.GetHashCode();
  411. }
  412. catch { }
  413. return base.GetHashCode();
  414. }
  415. }
  416. class MyValueIterator : IEnumerator<MyDictionary>
  417. {
  418. private readonly IEnumerator<MyDictionary> iter;
  419. public MyValueIterator(Dictionary<MyKey, MyDictionary> dict)
  420. {
  421. iter = dict.Values.GetEnumerator();
  422. }
  423. #region IEnumerator<MyDictionary> Members
  424. public MyDictionary Current
  425. {
  426. get
  427. {
  428. return iter.Current;
  429. }
  430. }
  431. #endregion
  432. #region IDisposable Members
  433. public void Dispose()
  434. {
  435. iter.Dispose();
  436. }
  437. #endregion
  438. #region IEnumerator Members
  439. object IEnumerator.Current
  440. {
  441. get
  442. {
  443. return iter.Current;
  444. }
  445. }
  446. public bool MoveNext()
  447. {
  448. return iter.MoveNext();
  449. }
  450. public void Reset()
  451. {
  452. iter.Reset();
  453. }
  454. #endregion
  455. }
  456. }