1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | using System;
|
---|
41 | using System.Text;
|
---|
42 | using ILNumerics.Storage;
|
---|
43 | using ILNumerics.Exceptions;
|
---|
44 | using System.Collections.Generic;
|
---|
45 |
|
---|
46 | namespace ILNumerics {
|
---|
47 | /// <summary>
|
---|
48 | /// ILCell : container class holding arbitrary array objects
|
---|
49 | /// </summary>
|
---|
50 | /// <remarks>
|
---|
51 | /// ILCell acts as general purpose container. It stores arbitrary arrays of arbitrary element type.
|
---|
52 | ///
|
---|
53 | /// </remarks>
|
---|
54 | public sealed class ILOutCell : ILBaseCell {
|
---|
55 |
|
---|
56 | #region attributes
|
---|
57 | ILCell m_originalCell;
|
---|
58 | private static readonly bool s_isTempArray = false;
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | #region properties
|
---|
62 | /// <summary>
|
---|
63 | /// Replace the elements of this array with another array's elements, preventing memory leaks
|
---|
64 | /// </summary>
|
---|
65 | /// <param name="value">New array</param>
|
---|
66 | public ILRetCell a {
|
---|
67 | set { Assign(value); }
|
---|
68 | get { return this.C; }
|
---|
69 | }
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | #region constructors
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// do not use this constructor! Out arrays are to be created implicitely only!
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="cellStorage">storage of source cell</param>
|
---|
78 | internal ILOutCell(ILCellStorage cellStorage)
|
---|
79 | : base(cellStorage, s_isTempArray) { }
|
---|
80 |
|
---|
81 | #endregion constructors
|
---|
82 |
|
---|
83 | #region implicit casts
|
---|
84 | /// <summary>
|
---|
85 | /// Implicitely convert persistent cell to output parameter type cell
|
---|
86 | /// </summary>
|
---|
87 | /// <param name="A">Original cell</param>
|
---|
88 | /// <returns>Output parameter cell</returns>
|
---|
89 | public static implicit operator ILOutCell(ILCell A) {
|
---|
90 | if (object.Equals(A, null))
|
---|
91 | return null;
|
---|
92 | ILOutCell ret = new ILOutCell(A.Storage);
|
---|
93 | ret.m_originalCell = A;
|
---|
94 | return ret;
|
---|
95 | }
|
---|
96 | #endregion
|
---|
97 |
|
---|
98 | #region public interface
|
---|
99 | /// <summary>
|
---|
100 | /// Replaces storage of this array with new array elements, registers this array for out-of-scope disposal
|
---|
101 | /// </summary>
|
---|
102 | /// <param name="value">New array</param>
|
---|
103 | public void Assign(ILRetCell value) {
|
---|
104 | if (!IsDisposed)
|
---|
105 | Storage.Dispose();
|
---|
106 | ILCellStorage storage = (ILCellStorage)value.GiveStorageAwayOrClone();
|
---|
107 | m_storage = storage;
|
---|
108 | if (!ILMath.isnull(m_originalCell)) {
|
---|
109 | (m_originalCell as ILDenseArray<ILStorage>).Storage = storage;
|
---|
110 | }
|
---|
111 | //ILScope.Context.RegisterArray(this);
|
---|
112 | }
|
---|
113 | /// <summary>
|
---|
114 | /// Set single element of the cell
|
---|
115 | /// </summary>
|
---|
116 | /// <param name="value">The new value</param>
|
---|
117 | /// <param name="idx">Indices specifying the location to set the element to</param>
|
---|
118 | /// <remarks>The function supports the following features:
|
---|
119 | /// <list type="bullet">
|
---|
120 | /// <item>Automatic expansion of the cell, when addressing an element outside of the cells size limits.</item>
|
---|
121 | /// <item>Before storing the new element into the cell, an old element may existing on the same location gets disposed.</item>
|
---|
122 | /// <item>A clone of the new value is stored, therefore, none of the source and the stored element are altered, whenever the other cell is altered (value semantics).</item>
|
---|
123 | /// <item>The function supports deep index addressing. This is the only way of altering array elements inside the cell - without recreation.</item>
|
---|
124 | /// </list>
|
---|
125 | /// <para>Removal of parts of the cell is <b>not</b> supported. If null or an empty array is provided as <paramref name="value"/>, the corresponding
|
---|
126 | /// element is overwritten or removed.</para>
|
---|
127 | /// </remarks>
|
---|
128 | public void SetValue(ILBaseArray value, params int[] idx) {
|
---|
129 | using (ILScope.Enter(value)) {
|
---|
130 | Storage.SetValueTyped(value.Storage, idx);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | /// <summary>
|
---|
134 | /// Set single element of the cell
|
---|
135 | /// </summary>
|
---|
136 | /// <param name="value">The new value</param>
|
---|
137 | /// <param name="idx">Indices specifying the location to set the element to</param>
|
---|
138 | /// <remarks>The function supports the following features:
|
---|
139 | /// <list type="bullet">
|
---|
140 | /// <item>Automatic expansion of the cell, when addressing an element outside of the cells size limits.</item>
|
---|
141 | /// <item>Before storing the new element into the cell, an old element may existing on the same location gets disposed.</item>
|
---|
142 | /// <item>A clone of the new value is stored, therefore, none of the source and the stored element are altered, whenever the other cell is altered (value semantics).</item>
|
---|
143 | /// <item>The function supports deep index addressing. This is the only way of altering array elements inside the cell - without recreation.</item>
|
---|
144 | /// </list>
|
---|
145 | /// <para>Removal of parts of the cell is <b>not</b> supported. If null or an empty array is provided as <paramref name="value"/>, the corresponding
|
---|
146 | /// element is overwritten or removed.</para>
|
---|
147 | /// </remarks>
|
---|
148 | internal void SetValue(ILStorage value, params int[] idx) {
|
---|
149 | Storage.SetValueTyped(value, idx);
|
---|
150 | }
|
---|
151 | #endregion
|
---|
152 |
|
---|
153 | #region Index access
|
---|
154 | /// <summary>
|
---|
155 | /// Get/set/remove single element
|
---|
156 | /// </summary>
|
---|
157 | /// <paramref name="indices" value="index to element"/>
|
---|
158 | /// <value>Inner element, new inner element or null</value>
|
---|
159 | /// <remarks>The type of access depends on the length of indices. If indices contains only one element,
|
---|
160 | /// the array will be accessed via sequential index access. This is sometimes called referred to as 'linear'
|
---|
161 | /// index addressing.
|
---|
162 | /// Sequential index access reflects the index of internal storage the way the data are actually organized
|
---|
163 | /// in memory. This access method is mainly convinient for vectors where you are not interested of orientation.
|
---|
164 | /// The following example demonstrates sequential index access for ILArray's (which also holds for ILCells):
|
---|
165 | /// <example>For <c>ILArray<double> A = ILMath.counter(1,12);</c>, <c>A[2]</c> gives: 3.0.
|
---|
166 | /// But the transpose
|
---|
167 | /// <c>A.T[2]</c> gives also: 3.0.
|
---|
168 | /// For matrices and N-dimensional arrays this holds as well:
|
---|
169 | /// <code>
|
---|
170 | /// ILArray<double> A = ILMath.counter(1.0,1.0,3,2,2);
|
---|
171 | /// A =
|
---|
172 | /// [1.0 4.0
|
---|
173 | /// 2.0 5.0
|
---|
174 | /// 3.0 6.0
|
---|
175 | ///
|
---|
176 | /// 7.0 10.0
|
---|
177 | /// 8.0 11.0
|
---|
178 | /// 9.0 12.0]
|
---|
179 | ///
|
---|
180 | /// A = ILMath.Reshape(A,3,2,2);
|
---|
181 | /// A[10] gives 11.0
|
---|
182 | /// A[10,1] gives ILArgumentException -> out of range
|
---|
183 | /// A[2,1,1] gives 12.0
|
---|
184 | /// A[2,1] gives 6.0 (set trailing dimension to '0')</code></example>
|
---|
185 | /// <para>If the element addressed is a ILCell itself, a deep reference to this element will be returned instead.
|
---|
186 | /// I.e. all elements of the ILCell will be recursively replaced with references to itself. Therefore, altering the
|
---|
187 | /// elements returned will not alter the elements contained in the cell.</para>
|
---|
188 | /// <para>
|
---|
189 | /// <list type="bullet">
|
---|
190 | /// <listheader>The type of the element returned depends on the type of the element addressed:</listheader>
|
---|
191 | /// <item>For ILArray<ElementType> the array returned will be a clone of the original array.</item>
|
---|
192 | /// <item>For ILCell the ILBaseArray returned is a deep reference of the original elements stored.</item>
|
---|
193 | /// <item>For other types the behavior is undefined. (since other types are not implemented yet ;)</item>
|
---|
194 | /// </list> </para>
|
---|
195 | /// <para>This indexer may also be used for direct access to inner elements of (elements of elements of ...) this cell:
|
---|
196 | /// <example>
|
---|
197 | /// <code>
|
---|
198 | /// ILCell innerCell = new ILCell(2,1);
|
---|
199 | /// innerCell[0] = ILMath.vec(10,200);
|
---|
200 | /// innerCell[1] = new int[] {-10,-20,-30};
|
---|
201 | /// ILCell cell = new ILCell(2,1);
|
---|
202 | /// cell[0] = innerCell;
|
---|
203 | /// cell[1] = new string[] {"foobla"};
|
---|
204 | /// // cell is now:
|
---|
205 | /// // [ILCell,(1x2)]
|
---|
206 | /// // [innerCell[0], ILArray<double>(1x181)]
|
---|
207 | /// // [innerCell[0], ILArray<double>(1x3)]
|
---|
208 | /// // [ILArray<string>,(1x1)]
|
---|
209 | ///
|
---|
210 | /// cell[0,0] -> will give innerCell eq. ILCell (1x2)
|
---|
211 | /// cell[0,1] -> will give ILArray<string>
|
---|
212 | /// cell[0,0,0,1] -> will give innerCell[1] eq. ILArray<int>(1x3)
|
---|
213 | /// </code>
|
---|
214 | /// </example>
|
---|
215 | /// In the last example above the trailing indices specified make the indexer walk down into the ILCell element and retrieve
|
---|
216 | /// the content of this element. This kind of index access may be done as deep as you want. Just
|
---|
217 | /// append the inner indices into inner elements to the right side of index specification. Addressing inner elements
|
---|
218 | /// this way is the only way to alter elements <b>directly</b> inside the ILCell. </para>
|
---|
219 | /// <para>Output parameter type cell carry a reference to the original array they were created from.
|
---|
220 | /// Modifications of outpur parameter type cells are immediately applied to the original array also.</para></remarks>
|
---|
221 | public ILRetCell this[params int[] indices] {
|
---|
222 | get {
|
---|
223 | ILStorage val = Storage.GetValueTyped(indices);
|
---|
224 | if (val is ILCellStorage)
|
---|
225 | return new ILRetCell((ILCellStorage)val);
|
---|
226 | else
|
---|
227 | return new ILRetCell(new ILStorage[] { val }, ILSize.Scalar1_1);
|
---|
228 | }
|
---|
229 | set {
|
---|
230 | using (ILScope.Enter(value)) {
|
---|
231 | if (!object.Equals(value, null) && value.Storage.FromImplicitCast && value.IsScalar) {
|
---|
232 | SetValue((value as ILDenseArray<ILStorage>).GetValue(0), indices);
|
---|
233 | } else {
|
---|
234 | SetValue((object.Equals(value, null)) ? null : value.Storage, indices);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | /// <summary>
|
---|
241 | /// Subarray access. Get/set regular subarray.
|
---|
242 | /// </summary>
|
---|
243 | /// <param name="indices">Address range</param>
|
---|
244 | /// <returns>Reference cell array with subarray addressed by <c>indices</c>. </returns>
|
---|
245 | /// <remarks>Query access: for N-dimensional cell arrays missing trailing dimensions indices will be choosen to be 0. Therefore you
|
---|
246 | /// may ommit those trailing dimensions in <c>indices</c>.
|
---|
247 | /// <para>The indexer may be used for querying or altering single/any elements
|
---|
248 | /// in this cell. <c>indices</c> may contains index specifications for one to any
|
---|
249 | /// dimension. The cell array returned will have the size specified by <c>indices</c>.</para>
|
---|
250 | /// <para>Values returned will be reference cells. All elements contained will be 'deep references' created by
|
---|
251 | /// recursively walking downwards the elements and replacing them by references to itself. Therefore altering the
|
---|
252 | /// values returned will not alter the original elements.</para>
|
---|
253 | /// <para>The indexer may also be used for removing parts of the cell. Therefore null must be assigned to the range specified by <c>indices</c> (using the set-access). <c>indices</c>
|
---|
254 | /// must contain exactly one dimension specification other than 'full' in this case. This may be any vector-sized numeric ILArray of any
|
---|
255 | /// numeric type. If <c>indices</c> apply to fewer dimensions than the number of dimensions existing, the upper dimensions will be
|
---|
256 | /// merged and the array will be reshaped before applying the removal to it.
|
---|
257 | /// <example>
|
---|
258 | /// <code>
|
---|
259 | /// ILCell C = new ILCell(4,10);
|
---|
260 | /// C[":",2] = null; // >- will remove the third column (index: 2) from the cell.
|
---|
261 | /// C[full,vec(2,5)] = null; >- will remove columns 3...6
|
---|
262 | /// C[1,1] = null; >- will produce an error. Only one dimension can be specified not full!
|
---|
263 | /// </code></example></para>
|
---|
264 | /// <para>The general behavior of this access methods is full compatible with the corresponding Matlab/Octave/Scilab access: a(:) = []. </para>
|
---|
265 | /// <para>Output parameter type cell carry a reference to the original array they were created from.
|
---|
266 | /// Modifications of outpur parameter type cells are immediately applied to the original array also.</para></remarks>
|
---|
267 | public new ILRetCell this[params ILBaseArray[] indices] {
|
---|
268 | get {
|
---|
269 | using (ILScope.Enter(indices)) {
|
---|
270 | ILCellStorage elements = (ILCellStorage)Storage.Subarray(indices);
|
---|
271 | return new ILRetCell(elements);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | set {
|
---|
275 | using (ILScope.Enter(indices))
|
---|
276 | using (ILScope.Enter(value)) {
|
---|
277 | if (Object.ReferenceEquals(value, null)) {
|
---|
278 | Storage.IndexSubrange(null, indices);
|
---|
279 | } else {
|
---|
280 | //if (value.Storage.FromImplicitCast && value.IsScalar) {
|
---|
281 | // Storage.IndexSubrange((ILDenseStorage<ILStorage>)value.GetValue(0), indices);
|
---|
282 | //} else {
|
---|
283 | Storage.IndexSubrange((ILCellStorage)value.Storage.Clone(), indices);
|
---|
284 | //}
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | #endregion index access
|
---|
291 |
|
---|
292 | #region memory management
|
---|
293 | internal override bool EnterScope() {
|
---|
294 | return false;
|
---|
295 | }
|
---|
296 | #endregion
|
---|
297 | }
|
---|
298 | }
|
---|