<commit_before>/*
 * Copyright 2013-2016 Netherlands Forensic Institute
 *
 * Licensed 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 io.parsingdata.metal.data.selection;

import static io.parsingdata.metal.SafeTrampoline.complete;
import static io.parsingdata.metal.SafeTrampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.data.selection.ByToken.getAllRoots;

import io.parsingdata.metal.SafeTrampoline;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseGraph;
import io.parsingdata.metal.data.ParseItem;
import io.parsingdata.metal.data.ParseValue;
import io.parsingdata.metal.data.Source;
import io.parsingdata.metal.token.Token;

public final class ByOffset {

    private ByOffset() {}

    public static boolean hasRootAtOffset(final ParseGraph graph, final Token definition, final long offset, final Source source) {
        return findItemAtOffset(getAllRoots(graph, definition), offset, source).computeResult() != null;
    }

    public static SafeTrampoline<ParseItem> findItemAtOffset(final ImmutableList<ParseItem> items, final long offset, final Source source) {
        checkNotNull(items, "items");
        checkNotNull(source, "source");
        if (items.isEmpty()) { return complete(() -> null); }
        final ParseItem head = items.head;
        if (head.isValue() && matchesLocation(head.asValue(), offset, source)) { return complete(() -> head); }
        if (head.isGraph()) {
            final ParseValue value = getLowestOffsetValue(head.asGraph(), null).computeResult();
            if (value != null && matchesLocation(value, offset, source)) { return complete(() -> head); }
        }
        return intermediate(() -> findItemAtOffset(items.tail, offset, source));
    }

    private static boolean matchesLocation(final ParseValue value, final long offset, final Source source) {
        return value.slice.offset == offset && value.slice.source.equals(source);
    }

    private static SafeTrampoline<ParseValue> getLowestOffsetValue(final ParseGraph graph, final ParseValue lowest) {
        if (graph.isEmpty() || !graph.getDefinition().isLocal()) { return complete(() -> lowest); }
        if (graph.head.isValue()) {
            return intermediate(() -> getLowestOffsetValue(graph.tail, lowest == null || lowest.slice.offset > graph.head.asValue().slice.offset ? graph.head.asValue() : lowest));
        }
        if (graph.head.isGraph()) {
            return intermediate(() -> getLowestOffsetValue(graph.tail, getLowestOffsetValue(graph.head.asGraph(), lowest).computeResult()));
        }
        return intermediate(() -> getLowestOffsetValue(graph.tail, lowest));
    }

}<commit_msg>#162: Converted ByOffset.getLowestOffsetValue() to be tail call recursive. This completes reimplementation of Sub with trampoline.<commit_after>/*
 * Copyright 2013-2016 Netherlands Forensic Institute
 *
 * Licensed 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 io.parsingdata.metal.data.selection;

import static io.parsingdata.metal.SafeTrampoline.complete;
import static io.parsingdata.metal.SafeTrampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.data.selection.ByToken.getAllRoots;

import io.parsingdata.metal.SafeTrampoline;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseGraph;
import io.parsingdata.metal.data.ParseItem;
import io.parsingdata.metal.data.ParseValue;
import io.parsingdata.metal.data.Source;
import io.parsingdata.metal.token.Token;

public final class ByOffset {

    private ByOffset() {}

    public static boolean hasRootAtOffset(final ParseGraph graph, final Token definition, final long offset, final Source source) {
        return findItemAtOffset(getAllRoots(graph, definition), offset, source).computeResult() != null;
    }

    public static SafeTrampoline<ParseItem> findItemAtOffset(final ImmutableList<ParseItem> items, final long offset, final Source source) {
        checkNotNull(items, "items");
        checkNotNull(source, "source");
        if (items.isEmpty()) { return complete(() -> null); }
        final ParseItem head = items.head;
        if (head.isValue() && matchesLocation(head.asValue(), offset, source)) { return complete(() -> head); }
        if (head.isGraph()) {
            final ParseValue value = getLowestOffsetValue(ImmutableList.create(head.asGraph()), null).computeResult();
            if (value != null && matchesLocation(value, offset, source)) { return complete(() -> head); }
        }
        return intermediate(() -> findItemAtOffset(items.tail, offset, source));
    }

    private static boolean matchesLocation(final ParseValue value, final long offset, final Source source) {
        return value.slice.offset == offset && value.slice.source.equals(source);
    }

    private static SafeTrampoline<ParseValue> getLowestOffsetValue(final ImmutableList<ParseGraph> graphList, final ParseValue lowest) {
        if (graphList.isEmpty()) { return complete(() -> lowest); }
        final ParseGraph graph = graphList.head;
        if (graph.isEmpty() || !graph.getDefinition().isLocal()) {
            return intermediate(() -> getLowestOffsetValue(graphList.tail, lowest));
        }
        if (graph.head.isValue()) {
            return intermediate(() -> getLowestOffsetValue(graphList.tail.add(graph.tail), lowest == null || lowest.slice.offset > graph.head.asValue().slice.offset ? graph.head.asValue() : lowest));
        }
        if (graph.head.isGraph()) {
            return intermediate(() -> getLowestOffsetValue(graphList.tail.add(graph.head.asGraph()).add(graph.tail), lowest));
        }
        return intermediate(() -> getLowestOffsetValue(graphList.tail.add(graph.tail), lowest));
    }

}
{'fraction_non_alphanumeric': 0.11415190984926094, 'fraction_numerical': 0.003951412264012879, 'mean_word_length': 3.9378612716763004, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '829732', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1795, 'n_words': 515}