src/jdk.internal.vm.compiler/share/classes/org.graalvm.util/src/org/graalvm/util/EconomicSet.java
changeset 48861 47f19ff9903c
parent 48860 5bce1b7e7800
child 48862 e13c8c5d9eb3
equal deleted inserted replaced
48860:5bce1b7e7800 48861:47f19ff9903c
     1 /*
       
     2  * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package org.graalvm.util;
       
    24 
       
    25 import java.util.Iterator;
       
    26 
       
    27 import org.graalvm.util.impl.EconomicMapImpl;
       
    28 
       
    29 /**
       
    30  * Memory efficient set data structure.
       
    31  */
       
    32 public interface EconomicSet<E> extends UnmodifiableEconomicSet<E> {
       
    33 
       
    34     boolean add(E element);
       
    35 
       
    36     void remove(E element);
       
    37 
       
    38     void clear();
       
    39 
       
    40     default void addAll(EconomicSet<E> values) {
       
    41         addAll(values.iterator());
       
    42     }
       
    43 
       
    44     default void addAll(Iterable<E> values) {
       
    45         addAll(values.iterator());
       
    46     }
       
    47 
       
    48     default void addAll(Iterator<E> values) {
       
    49         while (values.hasNext()) {
       
    50             add(values.next());
       
    51         }
       
    52     }
       
    53 
       
    54     default void removeAll(EconomicSet<E> values) {
       
    55         removeAll(values.iterator());
       
    56     }
       
    57 
       
    58     default void removeAll(Iterable<E> values) {
       
    59         removeAll(values.iterator());
       
    60     }
       
    61 
       
    62     default void removeAll(Iterator<E> values) {
       
    63         while (values.hasNext()) {
       
    64             remove(values.next());
       
    65         }
       
    66     }
       
    67 
       
    68     default void retainAll(EconomicSet<E> values) {
       
    69         Iterator<E> iterator = iterator();
       
    70         while (iterator.hasNext()) {
       
    71             E key = iterator.next();
       
    72             if (!values.contains(key)) {
       
    73                 iterator.remove();
       
    74             }
       
    75         }
       
    76     }
       
    77 
       
    78     /**
       
    79      * Creates a new set guaranteeing insertion order when iterating over its elements with the
       
    80      * default {@link Equivalence#DEFAULT} comparison strategy.
       
    81      */
       
    82     static <E> EconomicSet<E> create() {
       
    83         return EconomicSet.create(Equivalence.DEFAULT);
       
    84     }
       
    85 
       
    86     /**
       
    87      * Creates a new set guaranteeing insertion order when iterating over its elements.
       
    88      */
       
    89     static <E> EconomicSet<E> create(Equivalence strategy) {
       
    90         return EconomicMapImpl.create(strategy);
       
    91     }
       
    92 
       
    93     /**
       
    94      * Creates a new set guaranteeing insertion order when iterating over its elements with the
       
    95      * default {@link Equivalence#DEFAULT} comparison strategy and inserts all elements of the
       
    96      * specified collection.
       
    97      */
       
    98     static <E> EconomicSet<E> create(int initialCapacity) {
       
    99         return EconomicSet.create(Equivalence.DEFAULT, initialCapacity);
       
   100     }
       
   101 
       
   102     /**
       
   103      * Creates a new set guaranteeing insertion order when iterating over its elements with the
       
   104      * default {@link Equivalence#DEFAULT} comparison strategy and inserts all elements of the
       
   105      * specified collection.
       
   106      */
       
   107     static <E> EconomicSet<E> create(UnmodifiableEconomicSet<E> c) {
       
   108         return EconomicSet.create(Equivalence.DEFAULT, c);
       
   109     }
       
   110 
       
   111     /**
       
   112      * Creates a new set guaranteeing insertion order when iterating over its elements and
       
   113      * initializes with the given capacity.
       
   114      */
       
   115     static <E> EconomicSet<E> create(Equivalence strategy, int initialCapacity) {
       
   116         return EconomicMapImpl.create(strategy, initialCapacity);
       
   117     }
       
   118 
       
   119     /**
       
   120      * Creates a new set guaranteeing insertion order when iterating over its elements and inserts
       
   121      * all elements of the specified collection.
       
   122      */
       
   123     static <E> EconomicSet<E> create(Equivalence strategy, UnmodifiableEconomicSet<E> c) {
       
   124         return EconomicMapImpl.create(strategy, c);
       
   125     }
       
   126 }