Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Array/ILOutArray.cs @ 11316

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

#1967: ILNumerics source for experimentation

File size: 13.0 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.Linq;
43using System.Text;
44using ILNumerics.Storage;
45using ILNumerics.Exceptions;
46
47namespace ILNumerics {
48
49    /// <summary>
50    /// Rectangular array, used as output parameter only
51    /// </summary>
52    /// <typeparam name="ElementType">Inner type. This will mostly be a system numeric type or a
53    /// complex floating point type.</typeparam>
54    /// <remarks><para>This class extends the primary <c>ILArray</c> by optimizing its behavior for the case when used as an
55    /// output parameter of functions.</para><para>When writing your own function all <i>output</i> parameters should be of type <c>ILOutArray</c>. Do not
56    /// use <c>out ILArray</c>! Similary, all return types should be of type <see cref="ILNumerics.ILRetArray{T}"/> and all input parameters of type <see cref="ILNumerics.ILInArray{T}"/>.</para>
57    /// <para>Other than being used to retrieve results from functions, <c>ILOutArray</c> should not be used.</para>
58    /// </remarks>
59    /// <seealso cref="ILNumerics.ILArray{T}"/>
60    /// <seealso cref="ILNumerics.ILRetArray{T}"/>
61    /// <seealso cref="ILNumerics.ILInArray{T}"/>
62    [Serializable]
63    public sealed class ILOutArray<ElementType> : ILDenseArray<ElementType> {
64
65        #region attributes
66        private ILArray<ElementType> m_originalArray;
67        private static bool s_isTempArray = false;
68        #endregion
69
70        #region constructors
71        /// <summary>
72        /// Create new output parameter array
73        /// </summary>
74        /// <param name="storage">Dense storage for the new array</param>
75        internal ILOutArray(ILDenseStorage<ElementType> storage)
76            : base(storage, s_isTempArray) {
77            //ILScope.Context.RegisterArray(this);
78        }
79        #endregion
80
81        #region implicit cast operators
82        #region constructional operators
83        ///// <summary>
84        ///// Implicitly convert scalar to array of size 1x1 (scalar).
85        ///// </summary>
86        ///// <param name="val">System type of size scalar</param>
87        ///// <returns>New ILOutArray of type ILOutArray <![CDATA[<typeof(val)>]]> of size 1x1
88        ///// holding the only element with value of val.
89        ///// </returns>
90        //public static implicit operator ILOutArray<ElementType> (ElementType val) {
91        //    ILOutArray<ElementType> ret = new ILOutArray<ElementType>(
92        //                new ILDenseStorage<ElementType>(
93        //                    new ElementType[1] {val},
94        //                    new ILSize(1,1)));
95        //    return ret;
96        //}
97        ///// <summary>
98        ///// implicitly cast one dimensional System.Array to ILNumerics array (vector)
99        ///// </summary>
100        ///// <param name="A">1 dimensional system array, arbitrary type</param>
101        ///// <returns>ILNumerics array of same element type as elements of A.
102        ///// Row vector. If A is null: empty array.</returns>
103        ///// <remarks>The System.Array A will directly be used for the new ILNumerics array!
104        ///// No copy will be done! Make sure, not to reference A after this conversion!</remarks>
105        //public static implicit operator ILOutArray<ElementType> (ElementType[] A) {
106        //    if (A == null) {
107        //        ILDenseStorage<ElementType> dS = new ILDenseStorage<ElementType>(new ElementType[0], ILSize.Empty00);
108        //        return new ILOutArray<ElementType>(dS);                         
109        //    }
110        //    return new ILOutArray<ElementType>(A,1,A.Length);  // constructor will register the array in scope context!
111        //}
112        ///// <summary>
113        ///// implicitly convert n-dimensional System.Array to ILNumerics array
114        ///// </summary>
115        ///// <param name="A">arbitrarily sized System.Array</param>
116        ///// <returns>If A is null: empty array. Else: new ILNumerics array of the same size as A</returns>
117        ///// <remarks>The inner type of input array <paramref name="A"/> must match the requested type
118        ///// <typeparamref name="ElementType"/>. The resulting ILOutArray will reflect all dimensions of
119        ///// A. Elements of A will get copied to elements of output array (shallow copy).</remarks>
120        ///// <exception cref="ILNumerics.Exceptions.ILCastException"> if type of input does not match
121        ///// ElementType</exception>
122        //public static implicit operator ILOutArray<ElementType> (Array elements) {
123        //    if (elements == null || elements.Length == 0) {
124        //        return new ILOutArray<ElementType>(ILSize.Empty00);
125        //    }
126        //    if (elements.GetType().GetElementType() != typeof(ElementType))
127        //        throw new ILCastException("inner type of System.Array must match");
128        //    int [] dims = new int[elements.Rank];
129        //    ElementType [] retArr = ILMemoryPool.Pool.New<ElementType>(elements.Length);
130        //    int posArr = 0;
131        //    for (int i = 0; i < dims.Length; i++) {
132        //        dims[i] = elements.GetLength(dims.Length-i-1);
133        //    }
134        //    foreach (ElementType item in elements)
135        //        retArr[posArr++] = item;
136        //    ILOutArray<ElementType> ret = new ILOutArray<ElementType>(retArr,dims);
137        //    return ret;
138        //}
139        ///// <summary>
140        ///// implicitly cast two dimensional System.Array to ILNumerics array
141        ///// </summary>
142        ///// <param name="A">2D System.Array</param>
143        ///// <returns>If A is null: empty array. ILNumerics array of same size and type as A otherwise.</returns>
144        //public static implicit operator ILOutArray<ElementType>(ElementType[,] A) {
145        //    if (A == null || A.Length == 0) {
146        //        return new ILOutArray<ElementType>(ILSize.Empty00);
147        //    }
148        //    int[] dims = new int[2];
149        //    ElementType[] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
150        //    int posArr = 0;
151        //    for (int i = 0; i < 2; i++) {
152        //        dims[i] = A.GetLength(dims.Length - i - 1);
153        //    }
154        //    foreach (ElementType item in A)
155        //        retArr[posArr++] = item;
156        //    ILOutArray<ElementType> ret = new ILOutArray<ElementType>(retArr, dims);
157        //    return ret;
158        //}
159        ///// <summary>
160        ///// implicitly cast three dimensional System.Array to ILNumerics array
161        ///// </summary>
162        ///// <param name="A">3D System.Array</param>
163        ///// <returns>If A is null: empty array. ILNumerics array of same size and type as A otherwise.</returns>
164        //public static implicit operator ILOutArray<ElementType>(ElementType[,,] A) {
165        //    if (A == null || A.Length == 0) {
166        //        return new ILOutArray<ElementType>(ILSize.Empty00);
167        //    }
168        //    int[] dims = new int[3];
169        //    ElementType[] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
170        //    int posArr = 0;
171        //    for (int i = 0; i < 3; i++) {
172        //        dims[i] = A.GetLength(dims.Length - i - 1);
173        //    }
174        //    foreach (ElementType item in A)
175        //        retArr[posArr++] = item;
176        //    ILOutArray<ElementType> ret = new ILOutArray<ElementType>(retArr, dims);
177        //    return ret;
178        //}
179        #endregion
180        #region conversional operators
181        /// <summary>
182        /// Creates an output parameter type array from regular array
183        /// </summary>
184        /// <param name="A">Original array</param>
185        /// <returns>Output parameter type array, references the original array</returns>
186        public static implicit operator ILOutArray<ElementType>(ILArray<ElementType> A) {
187            if (object.Equals(A, null))
188                return null;
189            // we over take the storage (identical reference) to the new OutArray
190            // and store the reference to the original as well.
191            // It will be needed to synchronize changes to the denseStorage later (on Remove and Expand).
192            ILOutArray<ElementType> ret = new ILOutArray<ElementType>(A.Storage);
193            ret.m_originalArray = A;
194            return ret;
195        }
196        #endregion
197        #endregion
198
199        #region memory management
200        /// <summary>
201        /// Replace the elements of this array with another array's elements, preventing memory leaks
202        /// </summary>
203        /// <param name="value">New array</param>
204        public ILRetArray<ElementType> a {
205            set { Assign(value); }
206            get { return this.C; }
207        }
208        /// <summary>
209        /// Replaces storage of this array with new array elements, registers this array for out-of-scope disposal
210        /// </summary>
211        /// <param name="value">New array</param>
212        public void Assign(ILRetArray<ElementType> value) {
213            if (!IsDisposed)
214                Storage.Dispose();
215            ILDenseStorage<ElementType> storage = value.GiveStorageAwayOrClone();
216            m_storage = storage;
217            if (!object.ReferenceEquals(m_originalArray, null)) {
218                // update original array as well
219                (m_originalArray as ILDenseArray<ElementType>).Storage = storage;
220            }
221            //ILScope.Context.RegisterArray(this); 
222        }
223        internal override bool EnterScope() {
224            return false;
225        }
226        #endregion
227
228        #region mutability + indexer
229        /// <summary>
230        /// Set single value to element at index specified
231        /// </summary>
232        /// <param name="value">New value</param>
233        /// <param name="idx">Index of element to be altered</param>
234        public void SetValue(ElementType value, params int[] idx) {
235            Storage.SetValueTyped(value, idx);
236        }
237
238        /// <summary>
239        /// Alter range of this array
240        /// </summary>
241        /// <param name="value">Array with new values</param>
242        /// <param name="range">Range specification</param>
243        public void SetRange(ILInArray<ElementType> value, params ILBaseArray[] range) {
244            using (ILScope.Enter(value))
245            using (ILScope.Enter(range)) {
246                if (object.Equals(value, null)) {
247                    Storage.IndexSubrange(null, range);
248                } else {
249                    using (ILScope.Enter(value))
250                        Storage.IndexSubrange(value.Storage, range);
251                }
252            }
253        }
254        /// <summary>
255        /// Subarray creation/manipulation/deletion
256        /// </summary>
257        /// <param name="range">Range specification, defining the size of the subarray</param>
258        /// <returns>Subarray as copy of this array</returns>
259        public ILRetArray<ElementType> this[params ILBaseArray[] range] {
260            get {
261                using (ILScope.Enter(range))
262                    return new ILRetArray<ElementType>(Storage.Subarray(range));
263            }
264            set {
265                SetRange(value, range);
266            }
267        }
268
269        #endregion
270    }
271}
Note: See TracBrowser for help on using the repository browser.