Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Data/ILIntList.cs @ 12448

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

#1967: ILNumerics source for experimentation

File size: 5.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;
44
45namespace ILNumerics.Data {
46    internal class ILIntList {
47
48        int[] m_data;
49        int m_count;
50
51        public static ILIntList Create() {
52            return new ILIntList();
53        }
54        public static ILIntList Create(int initialCapacity) {
55            return new ILIntList(initialCapacity);
56        }
57        public static ILIntList Create(int initialCapacity, int value) {
58            return new ILIntList(initialCapacity, value);
59        }
60        internal ILIntList(ILDenseArray<int> array) {
61            m_count = array.S.NumberOfElements;
62            m_data = ILMemoryPool.Pool.New<int>(m_count);
63            System.Array.Copy(array.GetArrayForRead(), m_data, m_count);
64        }
65
66        private ILIntList() {
67            m_count = 0;
68            m_data = ILMemoryPool.Pool.New<int>(4);
69        }
70        private ILIntList(int initialCapacity) {
71            m_count = 0;
72            m_data = ILMemoryPool.Pool.New<int>(initialCapacity);
73        }
74
75        public ILIntList(int initialCapacity, int value) {
76            m_count = initialCapacity;
77            m_data = ILMemoryPool.Pool.New<int>(initialCapacity);
78            for (int i = 0; i < m_data.Length; i++) {
79                if (i == initialCapacity) break;
80                m_data[i] = value;
81            }
82        }
83
84        public int this[int index] {
85            get {
86                //if (index >= m_count)
87                //    throw new IndexOutOfRangeException();
88                return m_data[index];
89            }
90            set { m_data[index] = value; }
91        }
92
93        public int Count {
94            get { return m_count; }
95        }
96
97        public IEnumerator<int> GetEnumerator() {
98            for (int i = 0; i < m_count; i++) {
99                yield return m_data[i];
100            }
101        }
102
103        internal bool Contains( int val ) {
104            for (int i = 0; i < m_count; i++) {
105                if (m_data[i] == val) return true;
106            }
107            return false;
108        }
109
110        internal void Add( int val ) {
111            if (m_count == m_data.Length) {
112                doubleCapacity();
113            }
114            m_data[m_count++] = val;
115        }
116
117        private void doubleCapacity() {
118            // we always give our data array at least 4 elements
119            int[] newArr = ILMemoryPool.Pool.New<int>(m_data.Length * 2);
120            System.Array.Copy(m_data,newArr,m_count);
121            ILMemoryPool.Pool.Free(m_data);
122            m_data = newArr;
123        }
124        /// <summary>
125        /// caution! This may bring the ILIntList in inconsistent state!
126        /// </summary>
127        /// <returns></returns>
128        internal int[] GetArray() {
129            return m_data;
130        }
131
132        internal ILRetArray<int> ToTempArray() {
133            ILSize newDims = new ILSize(m_count, 1);
134            ILRetArray<int> ret = new ILRetArray<int>(new Storage.ILDenseStorage<int>(m_data,newDims));
135            m_data = null;
136            return ret;
137        }
138
139        internal void Clear() {
140            ILMemoryPool.Pool.Free(m_data);
141            m_data = new int[4];
142            m_count = 0;
143        }
144
145        internal void Dispose() {
146            ILMemoryPool.Pool.Free(m_data);
147            m_count = -1;
148            m_data = null;
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.