content: /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package accord.utils;
  
  import java.util.function.IntFunction;
  
  public class TinyEnumSet<E extends Enum<E>>
  {
      protected final int bitset;
  
      public TinyEnumSet(Enum<E> ... values)
      {
          this.bitset = encode(values);
      }
  
      public TinyEnumSet(Enum<E> value)
      {
          this.bitset = encode(value);
      }
  
      public TinyEnumSet()
      {
          this.bitset = 0;
      }
  
      public TinyEnumSet(int bitset)
      {
          this.bitset = bitset;
      }
  
      public static <E extends Enum<E>> TinyEnumSet<E> allOf(Class<E> clazz)
      {
          int count = clazz.getEnumConstants().length;
          return new TinyEnumSet<>(-1 >>> (32 - count));
      }
  
      public static <E extends Enum<E>> TinyEnumSet<E> of(Enum<E> value)
      {
          return new TinyEnumSet<>(encode(value));
      }
  
      public static <E extends Enum<E>> TinyEnumSet<E> of(Enum<E> ... values)
      {
          return new TinyEnumSet<>(encode(values));
      }
  
      public static <E extends Enum<E>> int encode(Enum<E> ... values)
      {
          int bitset = 0;
          for (Enum<E> v : values)
          {
              Invariants.requireArgument(v.ordinal() < 32);
              bitset |= 1 << v.ordinal();
          }
          return bitset;
      }
  
      public static <E extends Enum<E>> int encode(Enum<E> value)
      {
          return 1 << value.ordinal();
      }
  
      public static <E extends Enum<E>> boolean contains(int bitset, E value)
      {
          return contains(bitset, value.ordinal());
      }
  
      public static boolean contains(int bitset, int ordinal)
      {
          return 0 != (bitset & (1 << ordinal));
      }
  
      public boolean contains(E value)
      {
          return contains(bitset, value.ordinal());
      }
  
      public boolean contains(int ordinal)
      {
          return contains(bitset, ordinal);
      }
  
      public TinyEnumSet<E> or(TinyEnumSet<E> or)
      {
          return or(this, or, TinyEnumSet::new);
      }
  
      public TinyEnumSet<E> with(E flag)
      {
          return new TinyEnumSet<>(bitset | encode(flag));
      }
  
      public static <S extends TinyEnumSet<?>> S or(S a, S b, IntFunction<S> constructor)
      {
          int newBitset = a.bitset | b.bitset;
          return newBitset == a.bitset ? a : newBitset == b.bitset ? b : constructor.apply(newBitset);
      }
  
      public boolean test(E kind)
      {
          return testOrdinal(kind.ordinal());
      }
  
      public boolean testOrdinal(int ordinal)
      {
          return 0 != (bitset & (1 << ordinal));
      }
  
      public boolean equals(Object that)
      {
          return that != null && that.getClass() == TinyEnumSet.class && ((TinyEnumSet<?>) that).bitset == bitset;
      }
  
      public int bitset()
      {
          return bitset;
      }
  
      protected String toString(Object[] universe)
      {
          if (bitset == 0)
              return "{}";
  
          StringBuilder sb = new StringBuilder();
          sb.append('{');
          int bits = this.bitset;
          while (bits != 0)
          {
              if (sb.length() > 1)
                  sb.append(',');
              int i = Integer.numberOfTrailingZeros(bits);
              sb.append(universe[i]);
              bits ^= 1 << i;
          }
          sb.append('}');
          return sb.toString();
      }
  
  }
  
  