andrew4582

Pool

Dec 1st, 2010
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. namespace ServiceModelTesting {
  2.     using System;
  3.     using System.Runtime;
  4.  
  5.     internal class Pool<T> where T:class {
  6.         private int count;
  7.         private T[] items;
  8.  
  9.         public Pool(int maxCount) {
  10.             this.items = new T[maxCount];
  11.         }
  12.  
  13.         public void Clear() {
  14.             for(int i = 0;i < this.count;i++) {
  15.                 this.items[i] = default(T);
  16.             }
  17.             this.count = 0;
  18.         }
  19.  
  20.         public bool Return(T item) {
  21.             if(this.count < this.items.Length) {
  22.                 this.items[this.count++] = item;
  23.                 return true;
  24.             }
  25.             return false;
  26.         }
  27.  
  28.         public T Take() {
  29.             if(this.count > 0) {
  30.                 T local = this.items[--this.count];
  31.                 this.items[this.count] = default(T);
  32.                 return local;
  33.             }
  34.             return default(T);
  35.         }
  36.  
  37.         public int Count {
  38.             get {
  39.                 return this.count;
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment