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

org.apache.lucene.util.fst.PairOutputs Maven / Gradle / Ivy

There is a newer version: 9.10.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.lucene.util.fst;


import java.io.IOException;

import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.util.RamUsageEstimator;

/**
 * An FST {@link Outputs} implementation, holding two other outputs.
 *
 * @lucene.experimental
 */

public class PairOutputs extends Outputs> {

  private final Pair NO_OUTPUT;
  private final Outputs outputs1;
  private final Outputs outputs2;

  /** Holds a single pair of two outputs. */
  public static class Pair {
    public final A output1;
    public final B output2;

    // use newPair
    private Pair(A output1, B output2) {
      this.output1 = output1;
      this.output2 = output2;
    }

    @Override @SuppressWarnings("rawtypes")
    public boolean equals(Object other) {
      if (other == this) {
        return true;
      } else if (other instanceof Pair) {
        Pair pair = (Pair) other;
        return output1.equals(pair.output1) && output2.equals(pair.output2);
      } else {
        return false;
      }
    }

    @Override
    public int hashCode() {
      return output1.hashCode() + output2.hashCode();
    }

    @Override
    public String toString() {
      return "Pair(" + output1 + "," + output2 + ")";
    }
  };

  public PairOutputs(Outputs outputs1, Outputs outputs2) {
    this.outputs1 = outputs1;
    this.outputs2 = outputs2;
    NO_OUTPUT = new Pair<>(outputs1.getNoOutput(), outputs2.getNoOutput());
  }

  /** Create a new Pair */
  public Pair newPair(A a, B b) {
    if (a.equals(outputs1.getNoOutput())) {
      a = outputs1.getNoOutput();
    }
    if (b.equals(outputs2.getNoOutput())) {
      b = outputs2.getNoOutput();
    }

    if (a == outputs1.getNoOutput() && b == outputs2.getNoOutput()) {
      return NO_OUTPUT;
    } else {
      final Pair p = new Pair<>(a, b);
      assert valid(p);
      return p;
    }
  }

  // for assert
  private boolean valid(Pair pair) {
    final boolean noOutput1 = pair.output1.equals(outputs1.getNoOutput());
    final boolean noOutput2 = pair.output2.equals(outputs2.getNoOutput());

    if (noOutput1 && pair.output1 != outputs1.getNoOutput()) {
      return false;
    }

    if (noOutput2 && pair.output2 != outputs2.getNoOutput()) {
      return false;
    }

    if (noOutput1 && noOutput2) {
      if (pair != NO_OUTPUT) {
        return false;
      } else {
        return true;
      }
    } else {
      return true;
    }
  }
  
  @Override
  public Pair common(Pair pair1, Pair pair2) {
    assert valid(pair1);
    assert valid(pair2);
    return newPair(outputs1.common(pair1.output1, pair2.output1),
                   outputs2.common(pair1.output2, pair2.output2));
  }

  @Override
  public Pair subtract(Pair output, Pair inc) {
    assert valid(output);
    assert valid(inc);
    return newPair(outputs1.subtract(output.output1, inc.output1),
                    outputs2.subtract(output.output2, inc.output2));
  }

  @Override
  public Pair add(Pair prefix, Pair output) {
    assert valid(prefix);
    assert valid(output);
    return newPair(outputs1.add(prefix.output1, output.output1),
                    outputs2.add(prefix.output2, output.output2));
  }

  @Override
  public void write(Pair output, DataOutput writer) throws IOException {
    assert valid(output);
    outputs1.write(output.output1, writer);
    outputs2.write(output.output2, writer);
  }

  @Override
  public Pair read(DataInput in) throws IOException {
    A output1 = outputs1.read(in);
    B output2 = outputs2.read(in);
    return newPair(output1, output2);
  }
  
  @Override
  public void skipOutput(DataInput in) throws IOException {
    outputs1.skipOutput(in);
    outputs2.skipOutput(in);
  }

  @Override
  public Pair getNoOutput() {
    return NO_OUTPUT;
  }

  @Override
  public String outputToString(Pair output) {
    assert valid(output);
    return "";
  }

  @Override
  public String toString() {
    return "PairOutputs<" + outputs1 + "," + outputs2 + ">";
  }

  private static final long BASE_NUM_BYTES = RamUsageEstimator.shallowSizeOf(new Pair(null, null));

  @Override
  public long ramBytesUsed(Pair output) {
    long ramBytesUsed = BASE_NUM_BYTES;
    if (output.output1 != null) {
      ramBytesUsed += outputs1.ramBytesUsed(output.output1);
    }
    if (output.output2 != null) {
      ramBytesUsed += outputs2.ramBytesUsed(output.output2);
    }
    return ramBytesUsed;
  }
}