[9102] | 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 ILNumerics;
|
---|
| 42 | using ILNumerics.Misc;
|
---|
| 43 | using ILNumerics.Storage;
|
---|
| 44 | using ILNumerics.Exceptions;
|
---|
| 45 | using System.Runtime.Serialization;
|
---|
| 46 | using System.Runtime.Serialization.Formatters.Binary;
|
---|
| 47 | using System.IO;
|
---|
| 48 | using System.Text;
|
---|
| 49 | using System.Collections.Generic;
|
---|
| 50 | using System.Collections;
|
---|
| 51 |
|
---|
| 52 | namespace ILNumerics {
|
---|
| 53 |
|
---|
| 54 | /// <summary>
|
---|
| 55 | /// Typed base class for all ILNumerics data storage classes for any storage type.
|
---|
| 56 | /// </summary>
|
---|
| 57 | /// <remarks><para>You should not use this type directly. It serves as a base class
|
---|
| 58 | /// for all typed storages only and will be used by derived classes like ILArray<![CDATA[<>]]>.</para>
|
---|
| 59 | /// <para>If you are looking for an (untyped) base class to be used as generic class for any ILArray types, you should use ILBaseArray instead!</para>
|
---|
| 60 | /// </remarks>
|
---|
| 61 | [Serializable]
|
---|
| 62 | public abstract partial class ILBaseArray<ElementType>
|
---|
| 63 | : ILBaseArray, IEnumerable<ElementType> {
|
---|
| 64 |
|
---|
| 65 | internal ILBaseArray(ILStorage storage, bool isTempArray) : base(storage, isTempArray) { }
|
---|
| 66 |
|
---|
| 67 | #region attributes
|
---|
| 68 | #endregion
|
---|
| 69 |
|
---|
| 70 | #region properties
|
---|
| 71 | /// <summary>
|
---|
| 72 | /// Determine if this array has complex elements.
|
---|
| 73 | /// </summary>
|
---|
| 74 | public override bool IsComplex {
|
---|
| 75 | get {
|
---|
| 76 | return (this is ILBaseArray<complex>
|
---|
| 77 | || this is ILBaseArray<fcomplex>);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | /// <summary>
|
---|
| 81 | /// Determine if this array holds numeric values.
|
---|
| 82 | /// </summary>
|
---|
| 83 | /// <remarks>An ILArray is numeric as long as its elements are one of the
|
---|
| 84 | /// following types:
|
---|
| 85 | /// <list type="table">
|
---|
| 86 | /// <listheader>
|
---|
| 87 | /// <term>inner type</term>
|
---|
| 88 | /// </listheader>
|
---|
| 89 | /// <item>
|
---|
| 90 | /// <term>System.double</term>
|
---|
| 91 | /// <description>floating point, real, 8 bytes </description>
|
---|
| 92 | /// </item>
|
---|
| 93 | /// <item>
|
---|
| 94 | /// <term>System.float</term>
|
---|
| 95 | /// <description>floating point real, 4 bytes</description>
|
---|
| 96 | /// </item>
|
---|
| 97 | /// <item>
|
---|
| 98 | /// <term>ILNumerics.complex</term>
|
---|
| 99 | /// <description>floating point complex, 16 bytes</description>
|
---|
| 100 | /// </item>
|
---|
| 101 | /// <item>
|
---|
| 102 | /// <term>ILNumerics.fcomplex</term>
|
---|
| 103 | /// <description>floating point complex, 8 bytes</description>
|
---|
| 104 | /// </item>
|
---|
| 105 | /// <item>
|
---|
| 106 | /// <term>System.char</term>
|
---|
| 107 | /// <description>integer, real, 1 byte</description>
|
---|
| 108 | /// </item>
|
---|
| 109 | /// <item>
|
---|
| 110 | /// <term>System.byte</term>
|
---|
| 111 | /// <description>integer, real, 1 byte</description>
|
---|
| 112 | /// </item>
|
---|
| 113 | /// <item>
|
---|
| 114 | /// <term>System.Int16</term>
|
---|
| 115 | /// <description>integer, real, 2 byte</description>
|
---|
| 116 | /// </item>
|
---|
| 117 | /// <item>
|
---|
| 118 | /// <term>System.Int32</term>
|
---|
| 119 | /// <description>integer, real, 4 byte</description>
|
---|
| 120 | /// </item>
|
---|
| 121 | /// <item>
|
---|
| 122 | /// <term>System.Int64</term>
|
---|
| 123 | /// <description>integer, real, 8 byte</description>
|
---|
| 124 | /// </item>
|
---|
| 125 | /// <item>
|
---|
| 126 | /// <term>System.UInt16</term>
|
---|
| 127 | /// <description>unsigned integer, real, 2 byte</description>
|
---|
| 128 | /// </item>
|
---|
| 129 | /// <item>
|
---|
| 130 | /// <term>System.UInt32</term>
|
---|
| 131 | /// <description>unsigned integer, real, 4 byte</description>
|
---|
| 132 | /// </item>
|
---|
| 133 | /// <item>
|
---|
| 134 | /// <term>System.UInt64</term>
|
---|
| 135 | /// <description>unsigned integer, real, 8 byte</description>
|
---|
| 136 | /// </item>
|
---|
| 137 | /// </list>
|
---|
| 138 | /// </remarks>
|
---|
| 139 | public override bool IsNumeric {
|
---|
| 140 | get {
|
---|
| 141 | if (this is ILBaseArray<double> ||
|
---|
| 142 | this is ILBaseArray<float> ||
|
---|
| 143 | this is ILBaseArray<complex> ||
|
---|
| 144 | this is ILBaseArray<fcomplex> ||
|
---|
| 145 | this is ILBaseArray<byte> ||
|
---|
| 146 | this is ILBaseArray<char> ||
|
---|
| 147 | this is ILBaseArray<Int16> ||
|
---|
| 148 | this is ILBaseArray<Int32> ||
|
---|
| 149 | this is ILBaseArray<Int64> ||
|
---|
| 150 | this is ILBaseArray<UInt16> ||
|
---|
| 151 | this is ILBaseArray<UInt32> ||
|
---|
| 152 | this is ILBaseArray<UInt64>)
|
---|
| 153 | return true;
|
---|
| 154 | return false;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /// <summary>
|
---|
| 159 | /// Access to internal typed storage
|
---|
| 160 | /// </summary>
|
---|
| 161 | internal new ILStorage<ElementType> Storage {
|
---|
| 162 | get { return (m_storage as ILStorage<ElementType>); }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | #endregion
|
---|
| 166 |
|
---|
| 167 | #region public interface
|
---|
| 168 | /// <summary>
|
---|
| 169 | /// Serialize this array into a binary stream.
|
---|
| 170 | /// </summary>
|
---|
| 171 | /// <param name="outStream">System.IO.Stream to receive the byte stream
|
---|
| 172 | /// for this ILBaseArray</param>
|
---|
| 173 | /// <returns>True on success, false on error.</returns>
|
---|
| 174 | public virtual bool Serialize(Stream outStream) {
|
---|
| 175 | try {
|
---|
| 176 | BinaryFormatter bf = new BinaryFormatter();
|
---|
| 177 | bf.Serialize(outStream, this);
|
---|
| 178 | return true;
|
---|
| 179 | } catch (Exception) {
|
---|
| 180 | return false;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | /// <summary>
|
---|
| 184 | /// Deserialize / restore array from binary stream 'inStream'
|
---|
| 185 | /// </summary>
|
---|
| 186 | /// <param name="inStream">System.IO.Stream to reconstruct the
|
---|
| 187 | /// array from</param>
|
---|
| 188 | /// <returns>Array reconstructed from stream.</returns>
|
---|
| 189 | /// <exception cref="System.Runtime.Serialization.SerializationException">If the array could not get restored</exception>
|
---|
| 190 | public static ILBaseArray<ElementType> Deserialize(Stream inStream) {
|
---|
| 191 | try {
|
---|
| 192 | BinaryFormatter bf = new BinaryFormatter();
|
---|
| 193 | ILBaseArray<ElementType> ret = (ILBaseArray<ElementType>)bf.Deserialize(inStream);
|
---|
| 194 | return ret;
|
---|
| 195 | } catch (Exception e) {
|
---|
| 196 | throw e;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /// <summary>
|
---|
| 201 | /// Get single element from this array
|
---|
| 202 | /// </summary>
|
---|
| 203 | /// <param name="idx">Indices, location of element</param>
|
---|
| 204 | /// <returns>The selected value</returns>
|
---|
| 205 | public virtual ElementType GetValue(params int[] idx) {
|
---|
| 206 | return Storage.GetValueTyped(idx);
|
---|
| 207 | }
|
---|
| 208 | /// <summary>
|
---|
| 209 | /// Get minimum and maximum value of all elements - if any
|
---|
| 210 | /// </summary>
|
---|
| 211 | /// <param name="min">[Output] Minimum value</param>
|
---|
| 212 | /// <param name="max">[Output] Maximum value</param>
|
---|
| 213 | /// <returns>true if the limits exists and could be computed, false otherwise</returns>
|
---|
| 214 | /// <remarks>Empty arrays will return false. In this case the output parameter will be: default(ElementType).
|
---|
| 215 | /// </remarks>
|
---|
| 216 | public virtual bool GetLimits(out ElementType min, out ElementType max) {
|
---|
| 217 | return Storage.GetLimits(out min, out max);
|
---|
| 218 | }
|
---|
| 219 | /// <summary>
|
---|
| 220 | /// Get minimum and maximum value of all elements - if any
|
---|
| 221 | /// </summary>
|
---|
| 222 | /// <param name="min">[Output] Minimum value</param>
|
---|
| 223 | /// <param name="max">[Output] Maximum value</param>
|
---|
| 224 | /// <param name="includeInfNaNs">true: recognize Inf, NaN values; false: ignore those values</param>
|
---|
| 225 | /// <returns>true if the limits exists and could be computed, false otherwise</returns>
|
---|
| 226 | /// <remarks>Empty arrays will return false. In this case the output parameter will be: default(ElementType).</remarks>
|
---|
| 227 | internal bool GetLimits(out ElementType min, out ElementType max, bool includeInfNaNs) {
|
---|
| 228 | return Storage.GetLimits(out min, out max, includeInfNaNs);
|
---|
| 229 | }
|
---|
| 230 | #endregion
|
---|
| 231 |
|
---|
| 232 | #region IEnumerable<ILBaseArray<ElementType>> Member
|
---|
| 233 |
|
---|
| 234 | /// <summary>
|
---|
| 235 | /// Enumerator returning elements as ElementType
|
---|
| 236 | /// </summary>
|
---|
| 237 | /// <returns>Enumerator</returns>
|
---|
| 238 | /// <remarks>This method enables the us of ILNumerics arrays in foreach loops.
|
---|
| 239 | /// <para>This iterator implements IEnumerable<ElementType> explicitely and is used in situations,
|
---|
| 240 | /// where instances of ILNumerics arrays are casted to instances of the IEnumerable interface. This iterator
|
---|
| 241 | /// is not integrated into the ILNumerics memory management.</para></remarks>
|
---|
| 242 | /// <example><code>ILDenseStorage<T> A = ILMath.rand(5,4,6);
|
---|
| 243 | /// foreach (double element in A) {
|
---|
| 244 | /// // all elements are scalar double values
|
---|
| 245 | /// String.Format("Element: {0} ",element);
|
---|
| 246 | /// // Note: 'element' cannot be used to alter the collection!
|
---|
| 247 | /// }
|
---|
| 248 | /// </code></example>
|
---|
| 249 | IEnumerator<ElementType> System.Collections.Generic.IEnumerable<ElementType>.GetEnumerator() {
|
---|
| 250 | return Storage.GetEnumerator();
|
---|
| 251 | }
|
---|
| 252 | /// <summary>
|
---|
| 253 | /// Enumerator returning elements as ElementType
|
---|
| 254 | /// </summary>
|
---|
| 255 | /// <returns>Enumerator</returns>
|
---|
| 256 | /// <remarks>This method enables the use of ILNumerics arrays in foreach loops directly.</remarks>
|
---|
| 257 | IEnumerator System.Collections.IEnumerable.GetEnumerator() {
|
---|
| 258 | return GetEnumerator();
|
---|
| 259 | }
|
---|
| 260 | /// <summary>
|
---|
| 261 | /// Enumerator returning elements as ElementType
|
---|
| 262 | /// </summary>
|
---|
| 263 | /// <returns>Enumerator</returns>
|
---|
| 264 | /// <remarks>This method enables the use of ILNumerics arrays in foreach loops directly.</remarks>
|
---|
| 265 | public abstract IEnumerator<ElementType> GetEnumerator();
|
---|
| 266 | #endregion
|
---|
| 267 |
|
---|
| 268 | #region public interface
|
---|
| 269 | #endregion
|
---|
| 270 | }
|
---|
| 271 | } |
---|