Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/ResourcePool.cs @ 13792

Last change on this file since 13792 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 1021 bytes
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Controls;
6using System.Diagnostics;
7using System.Diagnostics.Contracts;
8
9namespace Microsoft.Research.DynamicDataDisplay.Common
10{
11  [DebuggerDisplay("Count = {Count}")]
12  public sealed class ResourcePool<T>
13  {
14    private readonly List<T> pool = new List<T>();
15
16    public T Get()
17    {
18      T item;
19
20      if (pool.Count < 1)
21      {
22        item = default(T);
23      }
24      else
25      {
26        int index = pool.Count - 1;
27        item = pool[index];
28        pool.RemoveAt(index);
29      }
30
31      return item;
32    }
33
34    public void Put(T item)
35    {
36      if (item == null)
37        throw new ArgumentNullException("item");
38
39      int index = pool.IndexOf(item);
40      if (index != -1)
41        throw new InvalidOperationException("Cannot release item that is already contained in pool.");
42
43      pool.Add(item);
44    }
45
46    public int Count
47    {
48      get { return pool.Count; }
49    }
50
51    public void Clear()
52    {
53      pool.Clear();
54    }
55  }
56}
Note: See TracBrowser for help on using the repository browser.