All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.calcite.linq4j.Ord Maven / Gradle / Ivy

There is a newer version: 1.38.0
Show newest version
/*
 * 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 org.apache.calcite.linq4j;

import com.google.common.collect.ImmutableList;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.RandomAccess;
import java.util.function.ObjIntConsumer;

/**
 * Pair of an element and an ordinal.
 *
 * @param  Element type
 */
public class Ord implements Map.Entry {
  public final int i;
  public final E e;

  /**
   * Creates an Ord.
   */
  public Ord(int i, E e) {
    this.i = i;
    this.e = e;
  }

  /**
   * Creates an Ord.
   */
  public static  Ord of(int n, E e) {
    return new Ord<>(n, e);
  }

  @Override public int hashCode() {
    return Objects.hash(e, i);
  }

  @Override public boolean equals(@Nullable Object obj) {
    return this == obj
        || obj instanceof Ord
        && i == ((Ord) obj).i
        && Objects.equals(e, ((Ord) obj).e);
  }

  /**
   * Creates an iterable of {@code Ord}s over an iterable.
   */
  public static  Iterable> zip(final Iterable iterable) {
    return () -> zip(iterable.iterator());
  }

  /**
   * Creates an iterator of {@code Ord}s over an iterator.
   */
  public static  Iterator> zip(final Iterator iterator) {
    return new Iterator>() {
      int n = 0;

      @Override public boolean hasNext() {
        return iterator.hasNext();
      }

      @Override public Ord next() {
        return Ord.of(n++, iterator.next());
      }

      @Override public void remove() {
        iterator.remove();
      }
    };
  }

  /**
   * Returns a numbered list based on an array.
   */
  public static  List> zip(final E[] elements) {
    return new OrdArrayList<>(elements);
  }

  /**
   * Returns a numbered list.
   */
  public static  List> zip(final List elements) {
    return elements instanceof RandomAccess
        ? new OrdRandomAccessList<>(elements)
        : new OrdList<>(elements);
  }

  /**
   * Iterates over an array in reverse order.
   *
   * 

Given the array ["a", "b", "c"], returns (2, "c") then (1, "b") then * (0, "a"). */ @SafeVarargs // heap pollution is not possible because we only read public static Iterable> reverse(E... elements) { return reverse(ImmutableList.copyOf(elements)); } /** * Iterates over a list in reverse order. * *

Given the list ["a", "b", "c"], returns (2, "c") then (1, "b") then * (0, "a"). */ public static Iterable> reverse(Iterable elements) { final ImmutableList elementList = ImmutableList.copyOf(elements); return () -> new Iterator>() { int i = elementList.size() - 1; @Override public boolean hasNext() { return i >= 0; } @Override public Ord next() { return Ord.of(i, elementList.get(i--)); } }; } @Override public Integer getKey() { return i; } @Override public E getValue() { return e; } @Override public E setValue(E value) { throw new UnsupportedOperationException(); } /** Applies an action to every element of an iterable, passing the zero-based * ordinal of the element to the action. * * @see List#forEach(java.util.function.Consumer) * @see Map#forEach(java.util.function.BiConsumer) * * @param iterable Iterable * @param action The action to be performed for each element * @param Element type */ public static void forEach(Iterable iterable, ObjIntConsumer action) { int i = 0; for (T t : iterable) { action.accept(t, i++); } } /** Applies an action to every element of an array, passing the zero-based * ordinal of the element to the action. * * @see List#forEach(java.util.function.Consumer) * @see Map#forEach(java.util.function.BiConsumer) * * @param ts Array * @param action The action to be performed for each element * @param Element type */ public static void forEach(T[] ts, ObjIntConsumer action) { for (int i = 0; i < ts.length; i++) { action.accept(ts[i], i); } } /** List of {@link Ord} backed by a list of elements. * * @param element type */ private static class OrdList extends AbstractList> { private final List elements; OrdList(List elements) { this.elements = elements; } @Override public Ord get(int index) { return Ord.of(index, elements.get(index)); } @Override public int size() { return elements.size(); } } /** List of {@link Ord} backed by a random-access list of elements. * * @param element type */ private static class OrdRandomAccessList extends OrdList implements RandomAccess { OrdRandomAccessList(List elements) { super(elements); } } /** List of {@link Ord} backed by an array of elements. * * @param element type */ private static class OrdArrayList extends AbstractList> implements RandomAccess { private final E[] elements; OrdArrayList(E[] elements) { this.elements = elements; } @Override public Ord get(int index) { return Ord.of(index, elements[index]); } @Override public int size() { return elements.length; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy