[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 System.Collections.Generic;
|
---|
| 42 | using System.Linq;
|
---|
| 43 | using System.Text;
|
---|
| 44 | using ILNumerics.Storage;
|
---|
| 45 | using ILNumerics.Exceptions;
|
---|
| 46 |
|
---|
| 47 | namespace ILNumerics {
|
---|
| 48 |
|
---|
| 49 | /// <summary>
|
---|
| 50 | /// Boolean array for high performance relational operations on arbitrary arrays
|
---|
| 51 | /// </summary>
|
---|
| 52 | /// <remarks>
|
---|
| 53 | /// Logical arrays store true/false conditions as elements. Each element consumes
|
---|
| 54 | /// one byte. Logical arrays are the output parameter of all relational comparisons.</remarks>
|
---|
| 55 | [Serializable]
|
---|
| 56 | public sealed class ILOutLogical : ILBaseLogical {
|
---|
| 57 |
|
---|
| 58 | #region attributes
|
---|
| 59 | private ILLogical m_originalArray;
|
---|
| 60 | #endregion
|
---|
| 61 |
|
---|
| 62 | #region constructors
|
---|
| 63 | /// <summary>
|
---|
| 64 | /// Constructor - create logical array of type <c>Byte</c> of specified size
|
---|
| 65 | /// </summary>
|
---|
| 66 | /// <param name="size">
|
---|
| 67 | /// Variable length int array specifying the number and size of dimensions to
|
---|
| 68 | /// be created.
|
---|
| 69 | /// </param>
|
---|
| 70 | /// <remarks>
|
---|
| 71 | /// The size parameter may not be null or an empty array! An Exception will be
|
---|
| 72 | /// thrown in this case. The dimensions will be trimmed before processing
|
---|
| 73 | /// (removing trailing non singleton dimensions).
|
---|
| 74 | /// Depending on the requested size an ILArray < byte > of the specified dimensions
|
---|
| 75 | /// will be created. The type of storage will be <c>bool</c>.
|
---|
| 76 | /// </remarks>
|
---|
| 77 | internal ILOutLogical(params int[] size)
|
---|
| 78 | : base(new ILLogicalStorage(new ILSize(size)),false) {
|
---|
| 79 | NumberNonZero = sumElements();
|
---|
| 80 | }
|
---|
| 81 | /// <summary>
|
---|
| 82 | /// Constructor - create logical array of type <c>byte</c> of specified size
|
---|
| 83 | /// </summary>
|
---|
| 84 | /// <param name="size">
|
---|
| 85 | /// Size descriptor
|
---|
| 86 | /// </param>
|
---|
| 87 | /// <remarks>
|
---|
| 88 | /// The size parameter may not be null. An Exception will be
|
---|
| 89 | /// thrown in this case. The dimensions will be trimmed before processing
|
---|
| 90 | /// (removing trailing singleton dimensions).
|
---|
| 91 | /// Depending on the requested size an logical array of the specified dimensions
|
---|
| 92 | /// will be created. The element type is be <c>bool</c>.
|
---|
| 93 | /// </remarks>
|
---|
| 94 | internal ILOutLogical(ILSize size)
|
---|
| 95 | : base(new ILLogicalStorage(size),false) {
|
---|
| 96 | NumberNonZero = sumElements();
|
---|
| 97 | }
|
---|
| 98 | /// <summary>
|
---|
| 99 | /// Constructor creating logical array from dense storage
|
---|
| 100 | /// </summary>
|
---|
| 101 | /// <param name="A">Input array, the storage of this ILArray will directly be used for
|
---|
| 102 | /// storage of the new logical array</param>
|
---|
| 103 | internal ILOutLogical(ILLogicalStorage A)
|
---|
| 104 | : base(A,false) {
|
---|
| 105 | NumberNonZero = sumElements();
|
---|
| 106 | }
|
---|
| 107 | /// <summary>
|
---|
| 108 | /// Constructor creating logical array from (dense) storage
|
---|
| 109 | /// </summary>
|
---|
| 110 | /// <param name="A">Input array, the storage of this ILArray will directly be used for
|
---|
| 111 | /// storage of the new logical array</param>
|
---|
| 112 | /// <param name="numberNonZero">Number of nonzero elements in A. Must be positive or 0.</param>
|
---|
| 113 | /// <remarks>Providing this parameter prevents the constructor from having to count the
|
---|
| 114 | /// 'true' elements in A.</remarks>
|
---|
| 115 | internal ILOutLogical(ILLogicalStorage A, long numberNonZero)
|
---|
| 116 | : base(A, false) {
|
---|
| 117 | if (numberNonZero < 0)
|
---|
| 118 | throw new ILNumerics.Exceptions.ILArgumentException("invalid number of non-zero-elements given!");
|
---|
| 119 | NumberNonZero = numberNonZero;
|
---|
| 120 | }
|
---|
| 121 | /// <summary>
|
---|
| 122 | /// Constructor - create logical array of specified size from data array
|
---|
| 123 | /// </summary>
|
---|
| 124 | /// <param name="size">
|
---|
| 125 | /// Variable length int array specifying the number and size of dimensions to
|
---|
| 126 | /// be created.
|
---|
| 127 | /// </param>
|
---|
| 128 | /// <param name="data">byte array matching the size of the dimensions
|
---|
| 129 | /// specified. The data will directly be used as storage! No copy will be made!</param>
|
---|
| 130 | /// <remarks>
|
---|
| 131 | /// The size parameter may not be null or an empty array! An Exception will be
|
---|
| 132 | /// thrown in this case. The dimensions will be trimmed before processing
|
---|
| 133 | /// (removing trailing non singleton dimensions).
|
---|
| 134 | /// Depending on the requested size an logical array of the specified size
|
---|
| 135 | /// will be created. The type of storage will be <c>byte</c>.
|
---|
| 136 | /// </remarks>
|
---|
| 137 | internal ILOutLogical(byte[] data, params int[] size)
|
---|
| 138 | : base(new ILLogicalStorage( data, new ILSize(size)),false) {
|
---|
| 139 | NumberNonZero = sumElements();
|
---|
| 140 | }
|
---|
| 141 | /// <summary>
|
---|
| 142 | /// Constructor creating logical array, provide predefined storage
|
---|
| 143 | /// </summary>
|
---|
| 144 | /// <param name="data">Predefined storage elements. The array will directly be used
|
---|
| 145 | /// as underlying storage. No copy will be made! </param>
|
---|
| 146 | /// <param name="size">Dimensions specification.</param>
|
---|
| 147 | internal ILOutLogical(byte[] data, ILSize size)
|
---|
| 148 | : base(new ILLogicalStorage(data, size), false) {
|
---|
| 149 | NumberNonZero = sumElements();
|
---|
| 150 | }
|
---|
| 151 | /// <summary>
|
---|
| 152 | /// Constructor creating logical array, predefined storage (fast version)
|
---|
| 153 | /// </summary>
|
---|
| 154 | /// <param name="data">predefined storage elements. The array will directly be used
|
---|
| 155 | /// as underlying storage. No copy will be made! </param>
|
---|
| 156 | /// <param name="size">Dimensions specification.</param>
|
---|
| 157 | /// <param name="nonZeroCount">number of nonzero elements in <paramref name="data"/>.
|
---|
| 158 | /// Providing this parameter prevents from counting the 'true' elements (again). </param>
|
---|
| 159 | internal ILOutLogical(byte[] data, ILSize size, long nonZeroCount)
|
---|
| 160 | : base(new ILLogicalStorage(data, size), false) {
|
---|
| 161 | if (nonZeroCount < 0)
|
---|
| 162 | throw new ILNumerics.Exceptions.ILArgumentException("invalid number of non-zero-elements given!");
|
---|
| 163 | NumberNonZero = nonZeroCount;
|
---|
| 164 | }
|
---|
| 165 | #endregion
|
---|
| 166 |
|
---|
| 167 | #region implicit cast operators
|
---|
| 168 | #region conversional operators
|
---|
| 169 | /// <summary>
|
---|
| 170 | /// Convert logical array to output parameter type array
|
---|
| 171 | /// </summary>
|
---|
| 172 | /// <param name="A">Source logical array</param>
|
---|
| 173 | /// <returns>Output parameter type array</returns>
|
---|
| 174 | public static implicit operator ILOutLogical(ILLogical A) {
|
---|
| 175 | if (object.Equals(A,null))
|
---|
| 176 | return null;
|
---|
| 177 | ILOutLogical ret = new ILOutLogical(A.Storage);
|
---|
| 178 | ret.m_originalArray = A;
|
---|
| 179 | return ret;
|
---|
| 180 | }
|
---|
| 181 | #endregion
|
---|
| 182 | #endregion
|
---|
| 183 |
|
---|
| 184 | #region index access + mutability
|
---|
| 185 | /// <summary>
|
---|
| 186 | /// Subarray access
|
---|
| 187 | /// </summary>
|
---|
| 188 | /// <param name="range">Range specification</param>
|
---|
| 189 | /// <returns>Reference pointing to the elements of this array specified by range. If used for removal:
|
---|
| 190 | /// the array will be changed to a referencing array having the parts requested removed and reshaped accordingly.</returns>
|
---|
| 191 | /// <remarks>Query access: for N-dimensional arrays trailing dimensions will be choosen to be 0. Therefore you
|
---|
| 192 | /// may ommit those trailing dimensions in range.
|
---|
| 193 | /// <para>The indexer may be used for querying or altering single/any elements
|
---|
| 194 | /// in this array. <c>range</c> may contains index specifications for one ... any
|
---|
| 195 | /// dimension. The array returned will have the size specified by range.</para>
|
---|
| 196 | /// <para>The indexer may also be used for removing parts of the array. Therefore an empty array
|
---|
| 197 | /// (of the same type) or 'null' must be assigned to the range specified by <c>range</c> using the set-access. <c>range</c>
|
---|
| 198 | /// must contain exactly one dimension specification other than null. This may be any vector-sized numeric ILArray of any
|
---|
| 199 | /// type. If <c>range</c> applies
|
---|
| 200 | /// to less dimensions than dimensions existing in the array, the upper dimensions will be
|
---|
| 201 | /// merged and the array will be reshaped before applying the removal to it.</para>
|
---|
| 202 | /// <para>In case of removal the ILArray returned will be a reference array.</para></remarks>
|
---|
| 203 | public ILRetLogical this[params ILBaseArray[] dims] {
|
---|
| 204 | get {
|
---|
| 205 | return new ILRetLogical((ILLogicalStorage)Storage.Subarray(dims));
|
---|
| 206 | }
|
---|
| 207 | set {
|
---|
| 208 | SetRange(value,dims);
|
---|
| 209 | NumberNonZero = sumElements();
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | /// <summary>
|
---|
| 213 | /// Set single value to element at index specified
|
---|
| 214 | /// </summary>
|
---|
| 215 | /// <param name="value">New value</param>
|
---|
| 216 | /// <param name="idx">Index of element to be altered</param>
|
---|
| 217 | public void SetValue(byte value, params int[] idx) {
|
---|
| 218 | Storage.SetValueTyped(value, idx);
|
---|
| 219 | }
|
---|
| 220 | /// <summary>
|
---|
| 221 | /// Alter a range of this array
|
---|
| 222 | /// </summary>
|
---|
| 223 | /// <param name="value">Array with new values</param>
|
---|
| 224 | /// <param name="range">Range specification</param>
|
---|
| 225 | public void SetRange(ILInLogical value, params ILBaseArray[] range) {
|
---|
| 226 | using (ILScope.Enter(value))
|
---|
| 227 | using (ILScope.Enter(range)) {
|
---|
| 228 | if (object.Equals(value, null)) {
|
---|
| 229 | Storage.IndexSubrange(null, range);
|
---|
| 230 | } else {
|
---|
| 231 | using (ILScope.Enter(value))
|
---|
| 232 | Storage.IndexSubrange(value.Storage, range);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | /// <summary>
|
---|
| 237 | /// Replace the elements of this array with another array's elements, preventing memory leaks
|
---|
| 238 | /// </summary>
|
---|
| 239 | /// <param name="value">New array</param>
|
---|
| 240 | public ILRetLogical a {
|
---|
| 241 | set { Assign(value); }
|
---|
| 242 | get { return this.C; }
|
---|
| 243 | }
|
---|
| 244 | /// <summary>
|
---|
| 245 | /// Replaces storage of this array with new array elements, registers this array for out-of-scope disposal
|
---|
| 246 | /// </summary>
|
---|
| 247 | /// <param name="value">new array</param>
|
---|
| 248 | public void Assign(ILRetLogical value) {
|
---|
| 249 | if (!IsDisposed)
|
---|
| 250 | Storage.Dispose();
|
---|
| 251 | ILLogicalStorage storage = (ILLogicalStorage)value.GiveStorageAwayOrClone();
|
---|
| 252 | m_storage = storage;
|
---|
| 253 | if (!ILMath.isnull(m_originalArray)) {
|
---|
| 254 | (m_originalArray as ILDenseArray<byte>).Storage = storage;
|
---|
| 255 | }
|
---|
| 256 | //ILScope.Context.RegisterArray(this);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | #endregion
|
---|
| 260 |
|
---|
| 261 | #region memory management
|
---|
| 262 | internal override bool EnterScope() {
|
---|
| 263 | return false;
|
---|
| 264 | }
|
---|
| 265 | #endregion
|
---|
| 266 |
|
---|
| 267 | }
|
---|
| 268 | }
|
---|