Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Storage/ILRightSideRange.cs @ 10367

Last change on this file since 10367 was 9102, checked in by gkronber, 11 years ago

#1967: ILNumerics source for experimentation

File size: 7.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.Data;
45
46namespace ILNumerics.Storage {
47    internal class ILRightSideRange : ILRange {
48        /// <summary>
49        /// rempat consctructor - for performance reasons
50        /// </summary>
51        /// <param name="dimension">source array dimensions</param>
52        /// <param name="range">destination array repmattings </param>
53        internal ILRightSideRange(ILSize dimension, params int[] range) {
54            if (range == null || range.Length == 0) {
55                m_range = new ILIntList[0];
56                m_size = ILSize.Empty00;
57            } else {
58                m_range = new ILIntList[Math.Max(range.Length, dimension.NumberOfDimensions)];
59                int[] outSizes = dimension.ToIntArray(m_range.Length);
60                int i = 0;
61                for (; i < range.Length; i++) {
62                    int limit = range[i];
63                    if (limit < 0)
64                        throw new Exceptions.ILArgumentException("dimension specifier must not be negative");
65                    // 'full' dimension addressed
66                    int fullVal = -(dimension[i] - 1);
67                    ILIntList intList = ILIntList.Create(limit, fullVal);
68                    //for (int c = limit; c --> 0; ) {
69                    //    intList.Add(val);
70                    //}
71                    m_range[i] = intList;
72                    outSizes[i] *= limit;
73                }
74                for (; i < m_range.Length; i++) {
75                    if (i < dimension.NumberOfDimensions) {
76                        int fullVal = -(dimension[i] - 1);
77                        m_range[i] = ILIntList.Create(1, fullVal);
78                    } else {
79                        m_range[i] = ILIntList.Create(1, 0);
80                    }
81                }
82                m_size = new ILSize(outSizes);
83            }
84        }
85        internal ILRightSideRange(ILSize dimension, ILBaseArray[] indices)
86            : base()  {
87            create(indices,dimension);
88        }
89        protected void create(ILBaseArray[] indices, ILSize dimensions) {
90            using (ILScope.Enter(indices)) {
91                //if (object.Equals(indices, null)) {
92                //    // A(null)
93                //    indices = new ILBaseArray[] { null };
94                //}
95                if (indices.Length == 0) {
96                    m_range = new ILIntList[0];
97                    m_size = ILSize.Empty00;
98                    return;
99                }
100                if (indices.Length == 1) {
101                    if (indices[0] is ILDenseArray<string> && indices[0].IsScalar) {
102                        // special case? A[":;0:3;0:end;..."] -> multiple dimensions given as single string
103                        System.Diagnostics.Debug.Fail("single dimension subarray specification should be handled seperately by ILDenseStorage.Subarray()");
104                        string indStr = (string)(indices[0] as ILDenseArray<string>).GetValue(0);
105                        if (String.IsNullOrWhiteSpace(indStr)) {
106                            m_range = new ILIntList[0];
107                            m_size = ILSize.Empty00;
108                            return;
109                        }
110                        string[] dimParts = indStr.Split(';');
111                        if (dimParts.Length > 1) {
112                            indices = new ILBaseArray[dimParts.Length];
113                            for (int i = 0; i < dimParts.Length; i++) {
114                                indices[i] = dimParts[i];
115                            }
116                            create(indices,dimensions);
117                            return;
118                        }
119                    }
120                }
121                m_range = new ILIntList[indices.Length];  // due to outer constraines: >= 2 dimensions
122                // outdims: immer so wie der range (indices), min 2
123                int[] outDims = new int[m_range.Length];
124                // if indices addresses less dimensions than ex, in dimensions -> expand last dimension
125                int[] inDims = dimensions.ToIntArrayEx(m_range.Length);
126
127                for (int d = 0; d < m_range.Length; d++) {
128                    int min = int.MaxValue, max = int.MinValue, outLen = 0;
129                    m_range[d] = ILIntList.Create();
130                    extractDimension(m_range[d], ref outLen, indices[d], d, ref min, ref max, inDims);
131                    outDims[d] = outLen;
132                    // check limits
133                    if (min < 0) {
134                        throw new Exceptions.ILArgumentException(String.Format("index < 0: {1}, dimension  #{0}", d, min));
135                    }
136                    if (max >= ((d < inDims.Length) ? inDims[d] : 1)) {
137                        throw new Exceptions.ILArgumentException(String.Format("index out of range: {1}, dimension: {0}", d, max));
138                    }
139                }
140                m_size = new ILSize(outDims);
141            }
142        }
143
144    }
145}
Note: See TracBrowser for help on using the repository browser.