OrderedHashSet: map // {T, {node{T}} dq // {T} add(x): if has(x): // don't change position in queue // as it's a subtle behavior, instead let // the caller explicity call remove(x) and add(x) return map[x] = dq.enqueue(x) bool has(x): return map.hasKey(x) bool remove(x): if !has(x): return false dq.removeNode(map[x]) map.remove(x) return true [] foreach(): for x in dq: yield x // deque interface dq: node enqueue(x): bool removeNode(node):
No comments:
Post a Comment