/Branches/0.9.5-DeltaShell/src/Common/DelftTools.Functions/Generic/ConvertedArray.cs

# · C# · 380 lines · 314 code · 64 blank · 2 comment · 0 complexity · 498e3f36a793a6838afcce0f8177e5c0 MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using DelftTools.Functions.Conversion;
  6. using DelftTools.Utils.Collections;
  7. using DelftTools.Utils.Data;
  8. namespace DelftTools.Functions.Generic
  9. {
  10. public class ConvertedArray<TTarget, TSource> : Unique<long>, IMultiDimensionalArray<TTarget>, IMultiDimensionalArray
  11. where TTarget : IComparable
  12. where TSource : IComparable
  13. {
  14. private readonly IMultiDimensionalArray<TSource> source;
  15. private readonly Func<TSource, TTarget> toTarget;
  16. private readonly Func<TTarget, TSource> toSource;
  17. public ConvertedArray(IMultiDimensionalArray<TSource> source, Func<TTarget, TSource> toSource, Func<TSource, TTarget> toTarget)
  18. {
  19. this.toSource = toSource;
  20. this.toTarget = toTarget;
  21. this.source = source;
  22. }
  23. public IEnumerator<TTarget> GetEnumerator()
  24. {
  25. return new ConvertedEnumerator<TTarget, TSource>(source.GetEnumerator(), toTarget);
  26. }
  27. IEnumerator IEnumerable.GetEnumerator()
  28. {
  29. return GetEnumerator();
  30. }
  31. public void CopyTo(Array array, int index)
  32. {
  33. throw new NotImplementedException();
  34. }
  35. public bool Remove(TTarget item)
  36. {
  37. return source.Remove(toSource(item));
  38. }
  39. public int Count
  40. {
  41. get { return source.Count; }
  42. }
  43. public bool IsReadOnly
  44. {
  45. get { return ((IList)source).IsReadOnly; }
  46. }
  47. IMultiDimensionalArrayView IMultiDimensionalArray.Select(int[] start, int[] end)
  48. {
  49. return Select(start, end);
  50. }
  51. IMultiDimensionalArrayView IMultiDimensionalArray.Select(int dimension, int start, int end)
  52. {
  53. return Select(dimension, start, end);
  54. }
  55. IMultiDimensionalArrayView IMultiDimensionalArray.Select(int dimension, int[] indexes)
  56. {
  57. return Select(dimension, indexes);
  58. }
  59. public IMultiDimensionalArrayView<TTarget> Select(int dimension, int[] indexes)
  60. {
  61. return new ConvertedArray<TTarget, TSource>(source, toSource, toTarget).Select(dimension, indexes);
  62. }
  63. public IMultiDimensionalArrayView<TTarget> Select(int[] start, int[] end)
  64. {
  65. return new ConvertedArray<TTarget, TSource>(source, toSource, toTarget).Select(start,end);
  66. }
  67. int IMultiDimensionalArray.Count
  68. {
  69. get { return Count; }
  70. }
  71. public void Resize(params int[] newShape)
  72. {
  73. source.Resize(newShape);
  74. }
  75. public TTarget this[params int[] indexes]
  76. {
  77. get
  78. {
  79. return toTarget(source[indexes]);
  80. }
  81. set
  82. {
  83. source[indexes] = toSource(value);
  84. }
  85. }
  86. object IMultiDimensionalArray.this[params int[] index]
  87. {
  88. get
  89. {
  90. return this[index];
  91. }
  92. set
  93. {
  94. this[index]= (TTarget) value;
  95. }
  96. }
  97. #if MONO
  98. object IMultiDimensionalArray.this[int index]
  99. {
  100. get
  101. {
  102. return this[index];
  103. }
  104. set
  105. {
  106. this[index]= (TTarget) value;
  107. }
  108. }
  109. #endif
  110. int ICollection.Count
  111. {
  112. get { return Count; }
  113. }
  114. public object SyncRoot
  115. {
  116. get { return source.SyncRoot; }
  117. }
  118. public bool IsSynchronized
  119. {
  120. get { return source.IsSynchronized; }
  121. }
  122. public int Add(object value)
  123. {
  124. return source.Add((object)toSource((TTarget) value));
  125. }
  126. public bool Contains(object value)
  127. {
  128. return source.Contains(toSource((TTarget) value));
  129. }
  130. public void Add(TTarget item)
  131. {
  132. source.Add(toSource(item));
  133. }
  134. public void Clear()
  135. {
  136. source.Clear();
  137. }
  138. public bool Contains(TTarget item)
  139. {
  140. return source.Contains(toSource(item));
  141. }
  142. public void CopyTo(TTarget[] array, int arrayIndex)
  143. {
  144. throw new NotImplementedException();
  145. }
  146. public int IndexOf(TTarget item)
  147. {
  148. return source.IndexOf(toSource(item));
  149. }
  150. public void Insert(int index, TTarget item)
  151. {
  152. source.Insert(index,toSource(item));
  153. }
  154. public void RemoveAt(int index)
  155. {
  156. source.RemoveAt(index);
  157. }
  158. TTarget IList<TTarget>.this[int index]
  159. {
  160. get
  161. {
  162. return this[index];
  163. }
  164. set
  165. {
  166. this[index] = value;
  167. }
  168. }
  169. #if MONO
  170. TTarget IMultiDimensionalArray<TTarget>.this[int index]
  171. {
  172. get
  173. {
  174. return this[index];
  175. }
  176. set
  177. {
  178. this[index] = value;
  179. }
  180. }
  181. #endif
  182. public IMultiDimensionalArray<TTarget> Clone()
  183. {
  184. throw new NotImplementedException();
  185. }
  186. void IMultiDimensionalArray.Clear()
  187. {
  188. Clear();
  189. }
  190. void IMultiDimensionalArray.RemoveAt(int index)
  191. {
  192. RemoveAt(index);
  193. }
  194. public object DefaultValue
  195. {
  196. get { throw new NotImplementedException(); }
  197. set { throw new NotImplementedException(); }
  198. }
  199. public int[] Shape
  200. {
  201. get {return source.Shape; }
  202. }
  203. public int[] Stride
  204. {
  205. get { return source.Stride; }
  206. }
  207. public int Rank
  208. {
  209. get { return source.Rank; }
  210. }
  211. public IMultiDimensionalArrayView<TTarget> Select(int dimension, int start, int end)
  212. {
  213. return new ConvertedArray<TTarget, TSource>(source, toSource, toTarget).Select(dimension, start, end);
  214. }
  215. public void RemoveAt(int dimension, int index)
  216. {
  217. source.RemoveAt(dimension,index);
  218. }
  219. public void RemoveAt(int dimension, int index, int length)
  220. {
  221. source.RemoveAt(dimension,index,length);
  222. }
  223. public void InsertAt(int dimension, int index)
  224. {
  225. source.InsertAt(dimension,index);
  226. }
  227. public void InsertAt(int dimension, int index, int length)
  228. {
  229. source.InsertAt(dimension,index,length);
  230. }
  231. public void Move(int dimension, int index, int length, int newIndex)
  232. {
  233. source.Move(dimension,index,length,newIndex);
  234. }
  235. public bool FireEvents
  236. {
  237. get { return source.FireEvents; }
  238. set { source.FireEvents = value; }
  239. }
  240. public void AddRange(IList values)
  241. {
  242. //foreach??
  243. //source.AddRange(values);
  244. throw new NotImplementedException();
  245. }
  246. public object MaxValue
  247. {
  248. get
  249. {
  250. return toTarget((TSource) source.MaxValue);
  251. }
  252. }
  253. public object MinValue
  254. {
  255. get
  256. {
  257. return toTarget((TSource)source.MinValue);
  258. }
  259. }
  260. public bool IsAutoSorted
  261. {
  262. get { return source.IsAutoSorted; }
  263. set { throw new NotImplementedException(); }
  264. }
  265. public IVariable Owner { get; set; }
  266. void IList.Clear()
  267. {
  268. Clear();
  269. }
  270. public int IndexOf(object value)
  271. {
  272. return source.IndexOf(toSource((TTarget) value));
  273. }
  274. public void Insert(int index, object value)
  275. {
  276. source.Insert(index,toSource((TTarget) value));
  277. }
  278. public void Remove(object value)
  279. {
  280. source.Remove(toSource((TTarget) value));
  281. }
  282. void IList.RemoveAt(int index)
  283. {
  284. RemoveAt(index);
  285. }
  286. object IList.this[int index]
  287. {
  288. get { return this[index]; }
  289. set { this[index] = (TTarget) value; }
  290. }
  291. bool IList.IsReadOnly
  292. {
  293. get { return IsReadOnly; }
  294. }
  295. public bool IsFixedSize
  296. {
  297. get { return source.IsFixedSize; }
  298. }
  299. object ICloneable.Clone()
  300. {
  301. throw new NotImplementedException();
  302. }
  303. public event EventHandler<MultiDimensionalArrayChangingEventArgs> CollectionChanged;
  304. public int InsertAt(int dimension, int index, int length, IList valuesToInsert)
  305. {
  306. throw new NotImplementedException();
  307. }
  308. public event EventHandler<MultiDimensionalArrayChangingEventArgs> CollectionChanging;
  309. public event PropertyChangingEventHandler PropertyChanging;
  310. public event PropertyChangedEventHandler PropertyChanged;
  311. public override string ToString()
  312. {
  313. return MultiDimensionalArrayHelper.ToString(this);
  314. }
  315. }
  316. }