Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Misc/ILScopeList.cs @ 12064

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

#1967: ILNumerics source for experimentation

File size: 3.9 KB
RevLine 
[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
40using System;
41using System.Collections.Generic;
42using System.Linq;
43using System.Text;
44
45namespace ILNumerics {
46    internal class ILScopeList<T> {
47
48        #region attributes
49        T[] m_data;
50        int m_count;
51        #endregion
52
53        #region properties
54        public int Count {
55            get { return m_count; }
56            set { m_count = value; }
57        }
58        #endregion
59
60        #region
61        public ILScopeList() {
62            m_data = new T[10];
63            m_count = 0;
64        }
65        public ILScopeList(int capacity) {
66            m_data = new T[capacity];
67            m_count = 0;
68        }
69        #endregion
70
71        #region public interface
72        public T Pop() {
73            if (m_count == 0) throw new IndexOutOfRangeException("The ILScopeList is empty. Pop cannot be completed.");
74            T ret = m_data[--m_count];
75            m_data[m_count + 1] = default(T);
76            return ret;
77        }
78        public void Push(T element) {
79            ensureLength(m_count);
80            m_data[m_count++] = element;
81        }
82        public T Peek() {
83            if (m_count == 0) throw new IndexOutOfRangeException("The ILScopeList is empty. Peek cannot be completed.");
84            return m_data[m_count-1];
85        }
86        /// <summary>
87        /// peek element at distance 'index' from END!!
88        /// </summary>
89        /// <param name="index"></param>
90        /// <returns></returns>
91        public T Peek(int index) {
92            index = m_count - 1 - index;
93            return m_data[index];
94        }
95        #endregion
96
97        #region private helper
98        private void ensureLength(int newIndex) {
99            while (newIndex >= m_data.Length) {
100                T[] newArr = new T[m_data.Length * 2];
101                System.Array.Copy(m_data,newArr,m_count);
102                m_data = newArr;
103            }
104        }
105        #endregion
106
107    }
108}
Note: See TracBrowser for help on using the repository browser.