[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 |
|
---|
| 40 | using System;
|
---|
| 41 | using System.Collections.Generic;
|
---|
| 42 | using System.Collections.Concurrent;
|
---|
| 43 | using System.Linq;
|
---|
| 44 | using System.Text;
|
---|
| 45 |
|
---|
| 46 | namespace ILNumerics.Data {
|
---|
| 47 | public sealed class ILIntList {
|
---|
| 48 |
|
---|
| 49 | private static readonly int INITIAL_CAPACITY = 4;
|
---|
| 50 | private static Queue<ILIntList> s_cache = new Queue<ILIntList>();
|
---|
| 51 | private static object s_lock = new object();
|
---|
| 52 | public static ILIntList Create() {
|
---|
| 53 | lock (s_lock) {
|
---|
| 54 | ILIntList ret;
|
---|
| 55 | if (s_cache.Count > 0) {
|
---|
| 56 | ret = s_cache.Dequeue();
|
---|
| 57 | System.Diagnostics.Debug.Assert(ret.m_data.Length >= INITIAL_CAPACITY && ret.Count == 0);
|
---|
| 58 | return ret;
|
---|
| 59 | } else {
|
---|
| 60 | return new ILIntList();
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public static ILIntList Create(int initialCapacity) {
|
---|
| 66 |
|
---|
| 67 | lock (s_lock) {
|
---|
| 68 | ILIntList ret;
|
---|
| 69 | if (s_cache.Count > 0) {
|
---|
| 70 | ret = s_cache.Dequeue();
|
---|
| 71 | System.Diagnostics.Debug.Assert(ret.m_data.Length >= INITIAL_CAPACITY && ret.Count == 0);
|
---|
| 72 | return ret;
|
---|
| 73 | } else {
|
---|
| 74 | return new ILIntList();
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private static void Cache(ILIntList list) {
|
---|
| 80 | lock (s_lock) {
|
---|
| 81 | list.Clear();
|
---|
| 82 | s_cache.Enqueue(list);
|
---|
| 83 | System.Diagnostics.Debug.Assert(list.m_data.Length >= INITIAL_CAPACITY && list.Count == 0);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | int[] m_data;
|
---|
| 88 | int m_count;
|
---|
| 89 | int[] m_initialData;
|
---|
| 90 |
|
---|
| 91 | internal ILIntList( ILDenseArray<int> array ) {
|
---|
| 92 | m_count = array.D.NumberOfElements;
|
---|
| 93 | m_data = ILMemoryPool.Pool.New<int>(m_count);
|
---|
| 94 | m_initialData = new int[INITIAL_CAPACITY];
|
---|
| 95 | System.Array.Copy(array.GetArrayForRead(), m_data, m_count);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private ILIntList() {
|
---|
| 99 | m_count = 0;
|
---|
| 100 | m_data = new int[INITIAL_CAPACITY];
|
---|
| 101 | m_initialData = m_data;
|
---|
| 102 | }
|
---|
| 103 | private ILIntList(int initialCapacity) {
|
---|
| 104 | m_count = 0;
|
---|
| 105 | m_data = new int[initialCapacity];
|
---|
| 106 | m_initialData = null;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | public int this[int index] {
|
---|
| 111 | get {
|
---|
| 112 | //if (index >= m_count)
|
---|
| 113 | // throw new IndexOutOfRangeException();
|
---|
| 114 | return m_data[index];
|
---|
| 115 | }
|
---|
| 116 | set { m_data[index] = value; }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public int Count {
|
---|
| 120 | get { return m_count; }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public IEnumerator<int> GetEnumerator() {
|
---|
| 124 | for (int i = 0; i < m_count; i++) {
|
---|
| 125 | yield return m_data[i];
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | internal bool Contains( int val ) {
|
---|
| 130 | for (int i = 0; i < m_count; i++) {
|
---|
| 131 | if (m_data[i] == val) return true;
|
---|
| 132 | }
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | internal void Add( int val ) {
|
---|
| 137 | if (m_count == m_data.Length) {
|
---|
| 138 | doubleCapacity();
|
---|
| 139 | }
|
---|
| 140 | m_data[m_count++] = val;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | private void doubleCapacity() {
|
---|
| 144 | // we always give our data array at least INITIAL_CAPACITY elements
|
---|
| 145 | int[] newArr = ILMemoryPool.Pool.New<int>(m_data.Length * 2);
|
---|
| 146 | System.Array.Copy(m_data,newArr,m_count);
|
---|
| 147 | if (m_data != m_initialData)
|
---|
| 148 | ILMemoryPool.Pool.RegisterObject(m_data);
|
---|
| 149 | m_data = newArr;
|
---|
| 150 | }
|
---|
| 151 | /// <summary>
|
---|
| 152 | /// caution! This may bring the current instance of ILIntList in an UNUSABLE and inconsistent state!
|
---|
| 153 | /// </summary>
|
---|
| 154 | /// <returns>internal int[] data array</returns>
|
---|
| 155 | internal int[] GetArray() {
|
---|
| 156 | return m_data;
|
---|
| 157 | }
|
---|
| 158 | /// <summary>
|
---|
| 159 | /// transfers internal data to new array and disposes this list
|
---|
| 160 | /// </summary>
|
---|
| 161 | /// <returns></returns>
|
---|
| 162 | internal ILRetArray<int> ToTempArray() {
|
---|
| 163 | ILSize newDims = new ILSize(1, m_count);
|
---|
| 164 | ILRetArray<int> ret = new ILRetArray<int>(new Storage.ILDenseStorage<int>(m_data,newDims));
|
---|
| 165 | if (m_data == m_initialData) {
|
---|
| 166 | m_data = new int[INITIAL_CAPACITY];
|
---|
| 167 | m_initialData = m_data;
|
---|
| 168 | } else {
|
---|
| 169 | m_data = m_initialData;
|
---|
| 170 | }
|
---|
| 171 | Cache(this);
|
---|
| 172 | return ret;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | internal void Clear() {
|
---|
| 176 | if (m_data.Length > INITIAL_CAPACITY) {
|
---|
| 177 | ILMemoryPool.Pool.RegisterObject(m_data);
|
---|
| 178 | m_data = m_initialData;
|
---|
| 179 | }
|
---|
| 180 | m_count = 0;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | internal void Dispose() {
|
---|
| 184 | Cache(this);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | }
|
---|
| 188 | }
|
---|