using System.Collections.Generic;
namespace Semmle.Util
{
///
/// 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.
///
public class Worklist
{
private readonly HashSet internalSet = new HashSet();
private LinkedList internalList = new LinkedList();
private bool hasNewElements = false;
///
/// Gets a value indicating whether this instance has had any new elements added
/// since the last time GetUnprocessedElements() was called.
///
///
/// true if this instance has new elements; otherwise, false.
///
public bool HasNewElements => hasNewElements;
///
/// Add the specified element to the worklist.
///
///
/// If set to true element.
///
public bool Add(T element)
{
if (internalSet.Contains(element))
return false;
internalSet.Add(element);
internalList.AddLast(element);
hasNewElements = true;
return true;
}
///
/// Gets the unprocessed elements that have been accumulated since the last time
/// this method was called. If HasNewElements == true, the resulting list
/// will be non-empty.
///
///
/// The unprocessed elements.
///
public LinkedList GetUnprocessedElements()
{
var result = internalList;
internalList = new LinkedList();
hasNewElements = false;
return result;
}
}
}