Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Storage/ILLeftSideRange.cs @ 12518

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

#1967: ILNumerics source for experimentation

File size: 7.6 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.Data;
45
46namespace ILNumerics.Storage {
47    internal class ILLeftSideRange : ILRange {       
48        /// <summary>
49        /// true for left side ranges, if at least one dimension must be expanded
50        /// </summary>
51        internal bool Expanding {
52            get {
53                return m_expanding;
54            }
55            set {
56                m_expanding = value;
57            }
58        }
59        /// <summary>
60        /// sizes of dimensions to be expanded
61        /// </summary>
62        private int[] m_expandDimensions;
63       /// <summary>
64        /// internal field, stores expanding flag
65        /// </summary>
66        private bool m_expanding;
67        /// <summary>
68        /// array with sizes of dimensions to be expanded
69        /// </summary>
70        internal int[] ExpandDimensions {
71            get {
72                return m_expandDimensions;
73            }
74        }
75
76        #region constructor
77        // indices may not address a single dimension! This case is handled by sequential addressing from callee (ILDenseArray<T>)
78        public ILLeftSideRange(ILSize dimension, ILBaseArray[] indices) : base()  {
79            create(indices,dimension);
80        }
81      #endregion
82
83        protected void create(ILBaseArray[] indices, ILSize dimensions) {
84            using (ILScope.Enter(indices)) {
85                if (indices.Length == 0) {
86                    m_range = new ILIntList[0];
87                    m_size = ILSize.Empty00;
88                    return;
89                }
90                if (indices.Length == 1) {
91                    if (indices[0] is ILDenseArray<string> && indices[0].IsScalar) {
92                        // special case? A[":;0:3;0:end;..."] -> multiple dimensions given as single string
93                        //System.Diagnostics.Debug.Fail("single dimension subarray specification should be handled seperately by ILDenseStorage.Subarray()");
94                        // (some rare cases (remove e.g.) use this path to parse given dimensions... )
95                        string indStr = (string)(indices[0] as ILDenseArray<string>).GetValue(0);
96                        if (String.IsNullOrWhiteSpace(indStr)) {
97                            m_range = new ILIntList[0];
98                            m_size = ILSize.Empty00;
99                            return;
100                        }
101                        string[] dimParts = indStr.Split(';');
102                        if (dimParts.Length > 1) {
103                            indices = new ILBaseArray[dimParts.Length];
104                            for (int i = 0; i < dimParts.Length; i++) {
105                                indices[i] = dimParts[i];
106                            }
107                            // caution: using ILBaseArray as input is EVIL!!
108                            // now we have an array of temparrays! Those are too fragile
109                            // to work with - so start again and safe them in a scope
110                            create(indices,dimensions);
111                            return;
112                        }
113                    }
114                }
115                m_range = new ILIntList[indices.Length];
116                // outdims: immer so wie der range (indices), min 2
117                int[] outDims = new int[Math.Max(2, m_range.Length)];
118                outDims[1] = 1; // in case it is not set later
119                // if indices addresses less dimensions than ex, in dimensions -> expand last dimension
120                int[] inDims = dimensions.ToIntArrayEx(m_range.Length);
121
122                for (int d = 0; d < m_range.Length; d++) {
123                    int min = int.MaxValue, max = int.MinValue, outLen = 0;
124                    m_range[d] = ILIntList.Create();
125                    extractDimension(m_range[d], ref outLen, indices[d], d, ref min, ref max, inDims);
126                    outDims[d] = outLen;
127                    // check limits
128                    if (min < 0) {
129                        throw new Exceptions.ILArgumentException(String.Format("index < 0: {1}, dimension  #{0}", d, min));
130                    }
131                    if (max >= ((d < inDims.Length) ? inDims[d] : 1)) {
132                        #region special cases A(ind) = B
133                        if (dimensions.NonSingletonDimensions > 1 &&
134                            (d == m_range.Length - 1)
135                            && (m_range.Length < dimensions.NumberOfDimensions)) {
136                            throw new Exceptions.ILArgumentException(String.Format("Cannot expand array along ambiguous dimension #{0}", d));
137                        }
138                        m_expanding = true;
139                        if (m_expandDimensions == null) {
140                            m_expandDimensions = new int[Math.Max(indices.Length, 2)];
141                        }
142                        if (indices.Length == 1 /* -> d == 0! */
143                            && dimensions.NonSingletonDimensions == 1 && dimensions[1] > 1) {
144                            // special case: row vector, adressing by single dimension A(end+1) = ...
145                            m_expandDimensions[1] = max + 1;
146                            outDims[1] = outLen;
147                            outDims[0] = 1;
148                            break;
149                        } else {
150                            // common case
151                            m_expandDimensions[d] = max + 1;
152                        }
153                        #endregion
154                    }
155                }
156                m_size = new ILSize(outDims);
157            }
158        }
159    }
160}
Note: See TracBrowser for help on using the repository browser.