-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathWorklist.cs
More file actions
57 lines (53 loc) · 1.93 KB
/
Copy pathWorklist.cs
File metadata and controls
57 lines (53 loc) · 1.93 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
57
using System.Collections.Generic;
namespace Semmle.Util
{
/// <summary>
/// A worklist of items, providing the operations of adding an item, checking
/// whether there are new items and iterating a chunk of unprocessed items.
/// Any one item will only be accepted into the worklist once.
/// </summary>
public class Worklist<T>
{
private readonly HashSet<T> internalSet = new HashSet<T>();
private LinkedList<T> internalList = new LinkedList<T>();
private bool hasNewElements = false;
/// <summary>
/// Gets a value indicating whether this instance has had any new elements added
/// since the last time <c>GetUnprocessedElements()</c> was called.
/// </summary>
/// <value>
/// <c>true</c> if this instance has new elements; otherwise, <c>false</c>.
/// </value>
public bool HasNewElements => hasNewElements;
/// <summary>
/// Add the specified element to the worklist.
/// </summary>
/// <param name='element'>
/// If set to <c>true</c> element.
/// </param>
public bool Add(T element)
{
if (internalSet.Contains(element))
return false;
internalSet.Add(element);
internalList.AddLast(element);
hasNewElements = true;
return true;
}
/// <summary>
/// Gets the unprocessed elements that have been accumulated since the last time
/// this method was called. If <c>HasNewElements == true</c>, the resulting list
/// will be non-empty.
/// </summary>
/// <returns>
/// The unprocessed elements.
/// </returns>
public LinkedList<T> GetUnprocessedElements()
{
var result = internalList;
internalList = new LinkedList<T>();
hasNewElements = false;
return result;
}
}
}