Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Storage/ILCountableArray.cs @ 12518

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

#1967: ILNumerics source for experimentation

File size: 6.6 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.Threading;
43using System.Linq;
44using System.Text;
45
46namespace ILNumerics.Storage {
47    [Serializable]
48    internal sealed class ILCountableArray<ElementType> {
49   
50        private ElementType[] m_data;
51        private int m_length;
52        private int m_referenceCount;
53
54        #region properties
55        /// <summary>
56        /// Access to the internal system array
57        /// </summary>
58        internal ElementType[] Data  { get { return m_data; } }
59        /// <summary>
60        /// minimal length of the system array to be used for computations
61        /// </summary>
62        internal int Length  { get { return m_length; } }
63      #endregion
64
65        /// <summary>
66        /// create new countable array by size
67        /// </summary>
68        /// <param name="length"></param>
69        /// <remarks>The memory for the newly created array is requested from memory pool. All elements of the array
70        /// are initialized with default(ElementType).</remarks>
71        internal ILCountableArray(int length) {
72            bool dummy;
73            m_data = ILMemoryPool.Pool.New<ElementType>(length, false, out dummy);
74            m_length = length;
75            // reference counting is matter of the hosting object!
76            m_referenceCount = 0;
77        }
78        /// <summary>
79        /// create new countable array by size
80        /// </summary>
81        /// <param name="length"></param>
82        /// <remarks>The memory for the newly created array is requested from memory pool. Depending on the value of
83        /// 'clear', the elements of the array are NOT initialized and therefore may contain garbage data!.</remarks>
84        internal ILCountableArray(int length, bool clear) {
85            bool dummy;
86            m_data = ILMemoryPool.Pool.New<ElementType>(length, clear, out dummy);
87            m_length = length;
88            // reference counting is matter of the hosting object!
89            m_referenceCount = 0;
90        }
91
92        /// <summary>
93        /// create new countable array, provide system array
94        /// </summary>
95        /// <param name="data">system array to be used as storage array directly</param>
96        /// <param name="length">minimum lenght of array needed</param>
97        internal ILCountableArray(ElementType[] data, int length) {
98#if VERBOSE
99            //DebuggerTraceHelper.Output.Append(
100            //    String.Format("CountableArray (T[]{0}) " + GetHashCode(),data.GetHashCode())
101            //);
102#endif
103            m_data = data;
104            m_length = length;
105            // reference counting is matter of the hosting object!
106            m_referenceCount = 0;
107        }
108        /// <summary>
109        /// increase reference counter
110        /// </summary>
111        internal void IncreaseReference() {
112            Interlocked.Increment(ref m_referenceCount);
113        }
114        /// <summary>
115        /// decrease reference counter
116        /// </summary>
117        internal void DecreaseReference() {
118            Interlocked.Decrement(ref m_referenceCount);
119            if (m_referenceCount == 0) {
120                Dispose();
121            }
122        }
123        /// <summary>
124        /// Dispose off this array: register it in pool
125        /// </summary>
126        internal void Dispose() {
127#if VERBOSE
128            //System.Diagnostics.Debug.Assert(m_referenceCount == 0);
129            //DebuggerTraceHelper.Output.Append(String.Format("\r\nCountableArray HashCode: " + GetHashCode()));
130#endif
131            if (m_data != null) {
132                ILMemoryPool.Pool.Free(m_data);
133                m_data = null;
134                m_length = 0;
135            }
136        }
137        /// <summary>
138        /// number of storages referencing to this array
139        /// </summary>
140        /// <remarks>In order to in-/decrease the reference counter, use
141        /// the <see cref="ILNumerics.Storage.ILCountableArray&lt;ElementType>.IncreaseReference()"/>
142        /// and <see cref="ILNumerics.Storage.ILCountableArray&lt;ElementType>.DecreaseReference()"/> functions.</remarks>
143        internal int ReferenceCount {
144            get { return m_referenceCount; }
145       
146        }
147        /// <summary>
148        /// return (solid) copy of this countable array
149        /// </summary>
150        /// <returns>newly created array</returns>
151        internal ILCountableArray<ElementType> CreateCopy() {
152            ILCountableArray<ElementType> ret = new ILCountableArray<ElementType>(Length, false);
153            System.Array.Copy(m_data,ret.Data,Length);
154            return ret;
155        }
156    }
157}
Note: See TracBrowser for help on using the repository browser.