-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Bag.cs
161 lines (137 loc) · 4.3 KB
/
Bag.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace VW
{
/// <summary>
/// Interface to model simple bag.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IBag<T>
{
/// <summary>
/// Try add <paramref name="item"/> to this bag.
/// </summary>
/// <param name="item">The item to add.</param>
/// <returns>True if succesful, false otherwise.</returns>
bool TryAdd(T item);
/// <summary>
/// Remove and return one item from this bag.
/// </summary>
/// <returns>The item removed from the bag or default(T) if there is no item available.</returns>
T Remove();
/// <summary>
/// Remove and return all items from this bag.
/// </summary>
/// <returns>The items removed from the gag.</returns>
IEnumerable<T> RemoveAll();
/// <summary>
/// The number of items this bag contains.
/// </summary>
int Count { get; }
}
/// <summary>
/// Factory class for various bag implementations.
/// </summary>
public static class Bag
{
/// <summary>
/// Creates a simple bound or unbound, not thread-safe bag object.
/// </summary>
/// <typeparam name="T">The type of the items.</typeparam>
/// <param name="max">The maximum number of items this bag should hold.</param>
/// <returns>A new bag instance.</returns>
public static IBag<T> Create<T>(int max = int.MaxValue)
{
return max == int.MaxValue ?
(IBag<T>)new BagImpl<T>() : new BoundedBagImpl<T>(max);
}
/// <summary>
/// Creates an unbound thread-safe, lock free bag.
/// </summary>
/// <typeparam name="T">The type of the items.</typeparam>
/// <returns>A new bag instance.</returns>
public static IBag<T> CreateLockFree<T>()
{
return new LockFreeBagImpl<T>();
}
private abstract class BaseBagImpl<T>
{
protected readonly Stack<T> stack;
internal BaseBagImpl()
{
this.stack = new Stack<T>();
}
public T Remove()
{
return this.stack.Count == 0 ? default(T) : this.stack.Pop();
}
public IEnumerable<T> RemoveAll()
{
var ret = this.stack.ToArray();
this.stack.Clear();
return ret;
}
public int Count
{
get { return this.stack.Count; }
}
}
private sealed class BagImpl<T> : BaseBagImpl<T>, IBag<T>
{
public bool TryAdd(T item)
{
this.stack.Push(item);
return true;
}
}
private sealed class BoundedBagImpl<T> : BaseBagImpl<T>, IBag<T>
{
private readonly int max;
internal BoundedBagImpl(int max)
{
this.max = max;
}
public bool TryAdd(T item)
{
if (this.stack.Count >= this.max)
return false;
this.stack.Push(item);
return true;
}
}
private sealed class LockFreeBagImpl<T> : IBag<T>
{
private ConcurrentQueue<T> queue;
internal LockFreeBagImpl()
{
this.queue = new ConcurrentQueue<T>();
}
public bool TryAdd(T item)
{
this.queue.Enqueue(item);
return true;
}
public T Remove()
{
T result;
if (this.queue.TryDequeue(out result))
return result;
return default(T);
}
public IEnumerable<T> RemoveAll()
{
// TODO: violates the lock constraint. though this is just used at disposable time
return this.queue;
}
public int Count
{
get { return this.queue.Count; }
}
}
}
}