| // Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| package com.android.tools.r8.utils; |
| |
| import com.google.common.collect.Sets; |
| import java.util.ArrayDeque; |
| import java.util.Deque; |
| import java.util.HashSet; |
| import java.util.Set; |
| |
| public class WorkList<T> { |
| |
| private final Deque<T> workingList = new ArrayDeque<>(); |
| private final Set<T> seen; |
| |
| public WorkList(EqualityTest equalityTest) { |
| if (equalityTest == EqualityTest.HASH) { |
| seen = new HashSet<>(); |
| } else { |
| seen = Sets.newIdentityHashSet(); |
| } |
| } |
| |
| public void addIfNotSeen(Iterable<T> items) { |
| items.forEach(this::addIfNotSeen); |
| } |
| |
| public void addIfNotSeen(T item) { |
| if (seen.add(item)) { |
| workingList.addLast(item); |
| } |
| } |
| |
| public boolean hasNext() { |
| return !workingList.isEmpty(); |
| } |
| |
| public T next() { |
| assert hasNext(); |
| return workingList.removeFirst(); |
| } |
| |
| public enum EqualityTest { |
| HASH, |
| IDENTITY |
| } |
| } |