-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIterableSet.cs
More file actions
56 lines (54 loc) · 1.55 KB
/
IterableSet.cs
File metadata and controls
56 lines (54 loc) · 1.55 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace ManicDigger
{
public class IterableSet<T>
{
public Dictionary<T, int> dictionary = new Dictionary<T, int>();
public List<T> list = new List<T>();
public Dictionary<int, bool> free = new Dictionary<int, bool>();
public int curpos;
public IEnumerable<T> Iterate(int max)
{
int max2 = Math.Min(max, list.Count);
for (int i = 0; i < max2; i++)
{
curpos++;
curpos = curpos % list.Count;
if (!free.ContainsKey(curpos))
{
yield return list[curpos];
}
}
}
public void Add(T value)
{
if (!dictionary.ContainsKey(value))
{
if (free.Count > 0)
{
int pos = System.Linq.Enumerable.First(free.Keys);
free.Remove(pos);
list[pos] = value;
dictionary[value] = pos;
}
else
{
list.Add(value);
dictionary[value] = list.Count - 1;
}
}
}
public void Remove(T value)
{
if (dictionary.ContainsKey(value))
{
int pos = dictionary[value];
dictionary.Remove(value);
//list.Remove(value);
free[pos] = true;
}
}
}
}