Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Array/ILInArray.cs @ 11315

Last change on this file since 11315 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 14.8 KB
Line 
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
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.IO;
44using System.Runtime.Serialization;
45using System.Runtime.CompilerServices;
46using ILNumerics.Storage;
47using ILNumerics.Misc;
48using ILNumerics.Exceptions;
49
50namespace ILNumerics {
51    /// <summary>
52    /// Rectangular array, used as input parameter only
53    /// </summary>
54    /// <typeparam name="ElementType">Inner type. This will mostly be a system numeric type or a
55    /// complex floating point type.</typeparam>
56    /// <remarks><para>This class extends the primary <c>ILArray</c> by optimizing its behavior for the case when used as an
57    /// input parameter to functions.</para><para>When writing your own function all <i>input</i> parameters should be of type <c>ILInArray</c>.
58    /// Similary, all return types should be of type <see cref="ILNumerics.ILRetArray{T}"/> and all "out" parameters of type <see cref="ILNumerics.ILOutArray{T}"/>.</para>
59    /// <para>Other than being used to transfer arguments into functions, <c>ILInArray</c> should not be used.</para>
60    /// </remarks>
61    /// <seealso cref="ILNumerics.ILArray{T}"/>
62    /// <seealso cref="ILNumerics.ILRetArray{T}"/>
63    /// <seealso cref="ILNumerics.ILOutArray{T}"/>
64    [Serializable]
65    public sealed partial class ILInArray<ElementType> : ILDenseArray<ElementType> {
66
67        private static readonly bool s_isTempArray = true;
68
69        #region constructors
70        /// <summary>
71        /// create new ILInArray, specify (dense) storage
72        /// </summary>
73        /// <param name="storage"></param>
74        internal ILInArray(ILDenseStorage<ElementType> storage)
75            : base(storage, s_isTempArray) {
76        }
77        /// <summary>
78        /// create new ILInArray, specify dimensions
79        /// </summary>
80        /// <param name="dimensions"></param>
81        internal ILInArray(ILSize dimensions)
82            : base(new ILDenseStorage<ElementType>(dimensions), s_isTempArray) {
83        }
84        /// <summary>
85        /// create new ILInArray from System.Array
86        /// </summary>
87        /// <param name="elements">System.Array</param>
88        /// <param name="dimensions">dimension specifier</param>
89        internal ILInArray(ElementType[] elements, ILSize dimensions) :
90            base(new ILDenseStorage<ElementType>(elements, dimensions), s_isTempArray) {
91        }
92        /// <summary>
93        /// create new ILInArray from System.Array
94        /// </summary>
95        /// <param name="elements">System.Array</param>
96        /// <param name="dimensions">dimension specifier</param>
97        internal ILInArray(ElementType[] elements, params int[] dimensions) :
98            base(new ILDenseStorage<ElementType>(elements, new ILSize(dimensions)), s_isTempArray) {
99        }
100        /// <summary>
101        /// create new ILInArray from System.Array
102        /// </summary>
103        /// <param name="elements">variable length System.Array</param>
104        internal ILInArray(params ElementType[] elements) :
105            base(new ILDenseStorage<ElementType>(elements, new ILSize(1, elements.Length)), s_isTempArray) {
106        }
107
108        #endregion
109
110        #region implicit cast operators
111        #region constructional operators
112        /// <summary>
113        /// Implicitly convert scalar to array of size 1x1 (scalar).
114        /// </summary>
115        /// <param name="val">System type of size scalar</param>
116        /// <returns>New ILInArray of type ILInArray <![CDATA[<typeof(val)>]]> of size 1x1
117        /// holding the only element with value of val.
118        /// </returns>
119        public static implicit operator ILInArray<ElementType> (ElementType val) {
120            ILInArray<ElementType> ret = new ILInArray<ElementType>(
121                        new ILDenseStorage<ElementType>(
122                            new ElementType[1] {val},
123                            new ILSize(1,1)));
124            return ret;
125        }
126        /// <summary>
127        /// Implicitly cast one dimensional System.Array to ILNumerics array (vector)
128        /// </summary>
129        /// <param name="A">1-dimensional system array, arbitrary type</param>
130        /// <returns>ILNumerics array of same element type as elements of A.
131        /// Row vector. If A is null: empty array.</returns>
132        /// <remarks>The System.Array A will directly be used for the new ILNumerics array!
133        /// No copy will be done! Make sure, not to reference A after this conversion!</remarks>
134        public static implicit operator ILInArray<ElementType> (ElementType[] A) {
135            if (A == null) {
136                return null;
137                //ILDenseStorage<ElementType> dS = new ILDenseStorage<ElementType>(new ElementType[0], ILSize.Empty00);
138                //return new ILInArray<ElementType>(dS);                         
139            }
140            ILArray<ElementType> ret;
141            if (Settings.CreateRowVectorsByDefault)
142                ret = new ILInArray<ElementType>(A, 1, A.Length);
143            else
144                ret = new ILInArray<ElementType>(A, A.Length, 1);
145            if (Settings.AllowInArrayAssignments)
146                ILScope.Context.RegisterArray(ret);
147            return ret;
148        }
149        /// <summary>
150        /// Implicitly convert n-dimensional System.Array to ILNumerics array
151        /// </summary>
152        /// <param name="A">Arbitrarily sized System.Array</param>
153        /// <returns>If A is null: empty array. Else: new ILNumerics array of the same size as A</returns>
154        /// <remarks>The inner type of input array <paramref name="A"/> must match the requested type
155        /// <typeparamref name="ElementType"/>. The resulting ILInArray will reflect all dimensions of
156        /// A. Elements of A will get copied to elements of output array (shallow copy).</remarks>
157        /// <exception cref="ILNumerics.Exceptions.ILCastException"> if type of input does not match
158        /// ElementType</exception>
159        public static implicit operator ILInArray<ElementType> (Array A) {
160            if (A == null) {
161                return null;
162            }
163            if (A.Length == 0) {
164                return new ILInArray<ElementType>(ILSize.Empty00);
165            }
166            if (A.GetType().GetElementType() != typeof(ElementType))
167                throw new ILCastException("inner type of System.Array must match");
168            int[] dims = new int[A.Rank];
169            ElementType[] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
170            int posArr = 0;
171            for (int i = 0; i < dims.Length; i++) {
172                dims[i] = A.GetLength(dims.Length - i - 1);
173            }
174            if (dims.Length == 1 && Settings.CreateRowVectorsByDefault) {
175                dims = new int[2] { 1, dims[0] };
176            }
177            foreach (ElementType item in A)
178                retArr[posArr++] = item;
179            ILInArray<ElementType> ret = new ILInArray<ElementType>(retArr,dims);
180            if (Settings.AllowInArrayAssignments)
181                ILScope.Context.RegisterArray(ret);
182            return ret;
183        }
184        /// <summary>
185        /// Implicitly cast two dimensional System.Array to ILNumerics array
186        /// </summary>
187        /// <param name="A">2-dimensional System.Array</param>
188        /// <returns>If A is null: empty array. ILNumerics array of same size and type as A otherwise.</returns>
189        public static implicit operator ILInArray<ElementType>(ElementType[,] A) {
190            if (A == null) {
191                return null;
192            }
193            if (A.Length == 0) {
194                return new ILInArray<ElementType>(ILSize.Empty00);
195            }
196            int[] dims = new int[2];
197            ElementType[] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
198            int posArr = 0;
199            for (int i = 0; i < 2; i++) {
200                dims[i] = A.GetLength(dims.Length - i - 1);
201            }
202            foreach (ElementType item in A)
203                retArr[posArr++] = item;
204            ILInArray<ElementType> ret = new ILInArray<ElementType>(retArr, dims);
205            if (Settings.AllowInArrayAssignments)
206                ILScope.Context.RegisterArray(ret);
207            return ret;
208        }
209        /// <summary>
210        /// Implicitly casts three dimensional System.Array to ILNumerics array
211        /// </summary>
212        /// <param name="A">3-dimensional System.Array</param>
213        /// <returns>If A is null: empty array. ILNumerics array of same size and type as A otherwise.</returns>
214        public static implicit operator ILInArray<ElementType>(ElementType[,,] A) {
215            if (A == null) {
216                return null;
217            }
218            if (A.Length == 0) {
219                return new ILInArray<ElementType>(ILSize.Empty00);
220            }
221            int[] dims = new int[3];
222            ElementType[] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
223            int posArr = 0;
224            for (int i = 0; i < 3; i++) {
225                dims[i] = A.GetLength(dims.Length - i - 1);
226            }
227            foreach (ElementType item in A)
228                retArr[posArr++] = item;
229            ILInArray<ElementType> ret = new ILInArray<ElementType>(retArr, dims);
230            if (Settings.AllowInArrayAssignments)
231                ILScope.Context.RegisterArray(ret);
232            return ret;
233        }
234        /// <summary>
235        /// Implicitly cast ILInArray to ILRetCell
236        /// </summary>
237        /// <param name="array">Input array</param>
238        /// <returns>1x1 cell holding the array</returns>
239        public static implicit operator ILRetCell(ILInArray<ElementType> array) {
240            using (ILScope.Enter(array)) {
241                ILRetCell ret = new ILRetCell(ILSize.Scalar1_1, array.Storage);
242                ret.Storage.FromImplicitCast = true;
243                return ret;
244            }
245        }
246        #endregion
247
248        #region conversional operators
249        /// <summary>
250        /// Convert temporary to input parameter array
251        /// </summary>
252        /// <param name="array">Temp array</param>
253        /// <returns>Input parameter array, will survive the current scope only</returns>
254        public static implicit operator ILInArray<ElementType>(ILRetArray<ElementType> array) {
255            if (object.Equals(array, null))
256                return null;
257            ILInArray<ElementType> ret = new ILInArray<ElementType>(array.GiveStorageAwayOrClone());
258            if (Settings.AllowInArrayAssignments)
259                ILScope.Context.RegisterArray(ret);
260            return ret;
261        }
262        /// <summary>
263        /// Convert persistent to input parameter array
264        /// </summary>
265        /// <param name="array">Persistent array</param>
266        /// <returns>Input parameter array, will survive the current scope only</returns>
267        public static implicit operator ILInArray<ElementType>(ILArray<ElementType> array) {
268            if (object.Equals(array, null))
269                return null;
270            ILInArray<ElementType> ret = new ILInArray<ElementType>(new ILDenseStorage<ElementType>(
271                array.Storage.GetDataArray(), array.Size));
272            if (Settings.AllowInArrayAssignments)
273                ILScope.Context.RegisterArray(ret);
274            return ret;
275
276        }
277        /// <summary>
278        /// Convert output paramter array to input parameter array
279        /// </summary>
280        /// <param name="array">Output parameter array</param>
281        /// <returns>Input parameter array, will survive the current scope only</returns>
282        public static implicit operator ILInArray<ElementType>(ILOutArray<ElementType> array) {
283            if (object.Equals(array, null))
284                return null;
285            ILInArray<ElementType> ret = new ILInArray<ElementType>(new ILDenseStorage<ElementType>(
286                array.Storage.GetDataArray(), array.Size));
287            if (Settings.AllowInArrayAssignments)
288                ILScope.Context.RegisterArray(ret);
289            return ret;
290        }
291        #endregion
292        #endregion
293
294        #region indexer + memory management
295        /// <summary>
296        /// Subarray creation/ manipulation/ deletion
297        /// </summary>
298        /// <param name="range">Range specification, defining the size of the subarray</param>
299        /// <returns>Subarray as copy of this array</returns>
300        public ILRetArray<ElementType> this[params ILBaseArray[] range] {
301            get {
302                using (ILScope.Enter(this))
303                using (ILScope.Enter(range))
304                    return new ILRetArray<ElementType>(Storage.Subarray(range));
305            }
306        }
307
308        internal override void LeaveScope() {
309            m_scopeCounter--;
310            if (m_scopeCounter <= 0) {
311                Dispose();
312            }
313        }
314
315        #endregion
316    }
317}
Note: See TracBrowser for help on using the repository browser.