Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Array/ILRegularRange.cs @ 12547

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

#1967: ILNumerics source for experimentation

File size: 6.4 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.Storage;
45using System.Linq.Expressions;
46using ILNumerics.Data;
47
48namespace ILNumerics.Misc {
49    internal sealed class ILRegularRange {
50
51        ILBaseArray m_startA;
52        ILBaseArray m_stepA;
53        ILBaseArray m_endA;
54        int m_start;
55        int m_step;
56        int m_end;
57        bool m_isEvaluated = false;
58        bool m_basedOnEndPlaceholder = false;
59
60        internal ILRegularRange(ILBaseArray start, ILBaseArray step, ILBaseArray end) {
61            m_startA = start;
62            m_stepA = step;
63            m_endA = end;
64            //// make sure, Length will always be positive!
65            //if ((m_start > m_end && m_step > 0) ||
66            //    (m_start < m_end && m_step < 0)) {
67            //    throw new Exceptions.ILArgumentException(String.Format("invalid range: check start:step:end! was: [{0}:{1}:{2}]",m_start,m_step,m_end));
68            //}
69            //if (m_step == 0)  throw new Exceptions.ILArgumentException("invalid range: step must not equal 0");
70        }
71        internal ILRegularRange(int start, int end) {
72            m_startA = start;
73            m_stepA = 1;
74            m_endA = end;
75            // make sure, Length will always be positive!
76            //if (m_start > m_end) throw new Exceptions.ILArgumentException("invalid range: start must be lower as or equal to end");
77        }
78        internal void Evaluate(int dimLength) {
79            if (m_isEvaluated) return;
80            m_step = ILMath.toint32(m_stepA).GetValue(0);
81            evaluateSingle(ref m_start, m_startA, dimLength);
82            evaluateSingle(ref m_end, m_endA, dimLength);
83            if (m_step == 0 || Math.Sign((m_end-m_start)/m_step) < 0) {
84                Length = 0;
85            } else if (m_basedOnEndPlaceholder && dimLength < 0) {
86                Length = 0;
87            } else {
88                Length =  (m_end-m_start) / m_step + 1; // ?? was: (int)Math.Ceiling((m_end - m_start) / (float)m_step) + 1;
89            }
90            m_isEvaluated = true;
91        }
92        private void evaluateSingle(ref int parameter, ILBaseArray input, int dimLength) {
93            if (input is ILBaseArray<Expression>) {
94                parameter = ILExpression.Evaluate((input as ILBaseArray<Expression>).GetValue(0),dimLength);
95                m_basedOnEndPlaceholder = true;
96            } else {
97                parameter = ILMath.toint32(input).GetValue(0);
98            }
99        }
100        internal void Expand(ILIntList target, ref int outLen, ref int min, ref int max, int dimLen) {
101            if (!m_isEvaluated)
102                Evaluate(dimLen);
103            if (Length <= 0 || Math.Sign((m_end-m_start)/m_step) < 0)
104                return;
105
106            int i = m_start;
107            if (m_start == m_end) {
108                target.Add(i);
109            } else if (m_start < m_end) {
110                do {
111                    target.Add(i);     
112                    i += m_step;
113                } while (i <= m_end);
114            } else {
115                do {
116                    target.Add(i);     
117                    i += m_step;
118                } while (i >= m_end);
119            }
120
121            int _min = Math.Min(m_start,m_end);
122            if (_min < min) min = _min;
123            int _max = Math.Max(m_start,m_end);
124            if (_max > max) max = _max;
125            outLen += Length;
126        }
127        public int Length { get; private set; }
128
129        public override string ToString() {
130            return String.Format("[{0}:{1}:{2}]",m_start,m_step,m_end);
131        }
132
133        internal void Extract<T>(T[] source, T[] dest, int dimLen) {
134            if (!m_isEvaluated)
135                Evaluate(dimLen);
136            if (Math.Sign((m_end-m_start)/m_step) < 0)
137                return;
138
139            int i = m_start;
140            int pos = 0;
141            if (m_start < m_end) {
142                do {
143                    dest[pos++] = source[i];
144                    i += m_step;
145                } while (i <= m_end);
146            } else if (m_start > m_end) {
147                do {
148                    dest[pos++] = source[i];
149                    i += m_step;
150                } while (i >= m_end);
151            } else {
152                // start == end
153                dest[0] = source[m_start];
154            }
155        }
156    }
157
158}
Note: See TracBrowser for help on using the repository browser.