Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/Auxiliary/ResourcePoolExtensions.cs @ 13749

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

#2283 added GUI and charts; fixed MCTS

File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Diagnostics;
6
7namespace Microsoft.Research.DynamicDataDisplay.Common.Auxiliary
8{
9  public static class ResourcePoolExtensions
10  {
11    /// <summary>
12    /// Gets item from the pool, or creates new item if pool doesn't have more items.
13    /// </summary>
14    /// <typeparam name="T"></typeparam>
15    /// <param name="pool">The pool.</param>
16    /// <returns></returns>
17    public static T GetOrCreate<T>(this ResourcePool<T> pool) where T : new()
18    {
19      T instance = pool.Get();
20      if (instance == null)
21      {
22        instance = new T();
23      }
24
25      return instance;
26    }
27
28    /// <summary>
29    /// Releases all items of given sequence into the pool.
30    /// </summary>
31    /// <typeparam name="T"></typeparam>
32    /// <param name="pool">Pool, which will contain all released elements from the sequence.</param>
33    /// <param name="sequence"></param>
34    public static void ReleaseAll<T>(this ResourcePool<T> pool, IEnumerable<T> sequence)
35    {
36      foreach (var item in sequence)
37      {
38        pool.Put(item);
39      }
40    }
41  }
42}
Note: See TracBrowser for help on using the repository browser.