Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Array/ILInCell.cs @ 10217

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

#1967: ILNumerics source for experimentation

File size: 10.1 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.Text;
42using ILNumerics.Storage;
43using ILNumerics.Exceptions;
44using System.Collections.Generic;
45
46namespace 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    /// </remarks>
53    public sealed class ILInCell : ILBaseCell {
54
55        private static readonly bool s_isTempArray = true;
56
57        #region constructors
58        /// <summary>
59        /// Create cell object with pre-created data in specified dimensions
60        /// </summary>
61        /// <param name="data">Predefined element data array, will be used for new cell (no copy will be made)</param>
62        /// <param name="size">Size of the new cell</param>
63        /// <remarks>Object array data will directly be used for storage. No
64        /// copy will be made. However, any arrays referenced by data are dereferenced for storage inside the cell. The size must match prod(size)</remarks>
65        [Obsolete("use <see cref='ILNumerics.ILMath.cell'/> instead!")]
66        internal ILInCell(ILStorage[] data, params int[] size)
67            : base(new ILCellStorage(data, new ILSize(size)), s_isTempArray) {
68        }
69        /// <summary>
70        /// Create cell object with pre-created data in specified dimensions
71        /// </summary>
72        /// <param name="data">predefined element data array, will be used for new cell (no copy will be made)</param>
73        /// <param name="size">size of the new cell</param>
74        /// <remarks>object array data will directly be used for storage. No
75        /// copy will be made. However, any arrays referenced by data are dereferenced for storage inside the cell. The size must match prod(size)</remarks>
76        [Obsolete("use <see cref='ILNumerics.ILMath.cell'/> instead!")]
77        internal ILInCell(ILStorage[] data, ILSize size)
78            : base(new ILCellStorage(data, size), s_isTempArray) {
79        }
80        /// <summary>
81        /// create new cell object, elements will be 'null'
82        /// </summary>
83        /// <param name="size">dimension sizes of the new cell</param>
84        [Obsolete("use <see cref='ILNumerics.ILMath.cell'/> instead!")]
85        internal ILInCell(params int[] size)
86            : base(new ILCellStorage(new ILStorage[prod(size)], new ILSize(size)), s_isTempArray) { }
87
88        internal ILInCell(ILCellStorage cellStorage)
89            : base(cellStorage, s_isTempArray) { }
90
91        [Obsolete("use <see cref='ILNumerics.ILMath.cell'/> instead!")]
92        internal ILInCell(ILSize size, params ILStorage[] values)
93            : base(new ILCellStorage(values, size), s_isTempArray) { }
94
95        #endregion constructors
96
97        #region implicit casts (for subarray definitions)
98        #region constructional operators
99        //public static implicit operator ILCell(double value) {
100        //    return createScalar(value);
101        //}
102        //public static implicit operator ILCell(float value) {
103        //    return createScalar(value);
104        //}
105        //public static implicit operator ILCell(byte value) {
106        //    return createScalar(value);
107        //}
108        //public static implicit operator ILCell(int[] value) {
109        //    return createArray(value);
110        //}
111        #endregion
112
113        #region conversional operators
114        /// <summary>
115        /// Convert temporary cell to input parameter type cell
116        /// </summary>
117        /// <param name="cell">Temporary cell</param>
118        /// <returns>Input parameter type cell</returns>
119        public static implicit operator ILInCell(ILRetCell cell) {
120            if (object.Equals(cell, null))
121                return null;
122            ILInCell ret = new ILInCell((ILCellStorage)cell.GiveStorageAwayOrClone());
123            if (Settings.AllowInArrayAssignments)
124                ILScope.Context.RegisterArray(ret);
125            return ret;
126        }
127        /// <summary>
128        /// Convert temporary cell to input parameter type cell
129        /// </summary>
130        /// <param name="cell">ILCell</param>
131        /// <returns>Input parameter type cell</returns>
132        public static implicit operator ILInCell(ILCell cell) {
133            if (object.Equals(cell, null))
134                return null;
135            ILInCell ret = new ILInCell(new ILCellStorage(
136                cell.Storage.GetDataArray(), cell.Size));
137            if (Settings.AllowInArrayAssignments)
138                ILScope.Context.RegisterArray(ret);
139            return ret;
140        }
141        /// <summary>
142        /// Convert output parameter type cell to input parameter type cell
143        /// </summary>
144        /// <param name="cell">Output parameter type cell</param>
145        /// <returns>Input parameter type cell</returns>
146        public static implicit operator ILInCell(ILOutCell cell) {
147            if (object.Equals(cell, null))
148                return null;
149            ILInCell ret = new ILInCell(new ILCellStorage(
150                cell.Storage.GetDataArray(), cell.Size));
151            if (Settings.AllowInArrayAssignments)
152                ILScope.Context.RegisterArray(ret);
153            return ret;
154        }
155        #endregion
156        #endregion
157
158        #region helper functions
159        internal static long prod(int[] sizes) {
160            int ret = 1;
161            for (int i = 0; i < sizes.Length; i++)
162                ret *= sizes[i];
163            return ret;
164        }
165        #endregion helper functions
166
167        #region Index access
168        /// <summary>
169        /// Return single element in a scalar cell
170        /// </summary>
171        /// <paramref name="indices" value="Index to element"/>
172        /// <remarks>
173        /// <para>The indexer retrieves a single element from within the cell. It returns the single element
174        /// in a new (scalar) cell. Use dereferencing functions like GetArray&lt;T>() in order to retrieve
175        /// the element value (ie. the array addressed) without a cell container.</para>
176        /// <para>Input parameter type cells are immutable! They are not intended to be altered! Therefore,
177        /// write access on such cells is disabled. In order to alter content of a cell, assign the cell to a regular
178        /// persistent cell of type <see cref="T:ILNumerics.ILCell"/>.</para></remarks>
179        public ILRetCell this[params int[] indices] {
180            get {
181                ILStorage val = Storage.GetValueTyped(indices);
182                if (val is ILCellStorage)
183                    return new ILRetCell((ILCellStorage)val);
184                else
185                    return new ILRetCell(new ILStorage[] { val }, ILSize.Scalar1_1);
186            }
187        }
188
189        /// <summary>
190        /// Subarray access. Get regular subarray.
191        /// </summary>
192        /// <param name="indices">Address range</param>
193        /// <returns>Cell array with subcell range from this instance, addressed by <c>indices</c> </returns>
194        /// <remarks>
195        /// <para>The indexer is used to query subcell ranges. All general indexing/ subarray addressing rules apply.
196        /// The elements addressed are returned within a new temporary cell instance.</para>
197        /// <para>Input parameter type cells are immutable! They are not intended to be altered! Therefore,
198        /// write access on such cells is disabled. In order to alter content of a cell, assign the cell to a regular
199        /// persistent cell of type <see cref="T:ILNumerics.ILCell"/>.</para></remarks>
200        public ILRetCell this[params ILBaseArray[] indices] {
201            get {
202                using (ILScope.Enter(indices)) {
203                    ILCellStorage elements = (ILCellStorage)Storage.Subarray(indices);
204                    return new ILRetCell(elements);
205                }
206            }
207        }
208
209        #endregion index access
210
211        #region memory management
212        internal override void LeaveScope() {
213            m_scopeCounter--;
214            if (m_scopeCounter <= 0) {
215                Dispose();
216            }
217        }
218        #endregion
219
220    }
221}
Note: See TracBrowser for help on using the repository browser.