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

net.automatalib.util.tries.SuffixTrieNode Maven / Gradle / Ivy

Go to download

This artifact provides various common utility operations for analyzing and manipulating automata and graphs, such as traversal, minimization and copying.

There is a newer version: 0.11.0
Show newest version
/* Copyright (C) 2013-2019 TU Dortmund
 * This file is part of AutomataLib, http://www.automatalib.net/.
 *
 * 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 net.automatalib.util.tries;

import java.util.List;
import java.util.NoSuchElementException;

import net.automatalib.words.Word;
import net.automatalib.words.WordBuilder;

/**
 * A node in a {@link SuffixTrie}.
 *
 * @param 
 *         symbol class.
 *
 * @author Malte Isberner
 */
public class SuffixTrieNode extends Word {

    private final I symbol;
    private final SuffixTrieNode parent;

    /**
     * Root constructor.
     */
    public SuffixTrieNode() {
        this.symbol = null;
        this.parent = null;
    }

    public SuffixTrieNode(I symbol, SuffixTrieNode parent) {
        this.symbol = symbol;
        this.parent = parent;
    }

    public static  Word toExplicitWord(SuffixTrieNode node) {
        WordBuilder wb = new WordBuilder<>(node.depth());
        appendSuffix(node, wb);
        return wb.toWord();
    }

    // TODO: replace by getter/attribute?
    public int depth() {
        return depth(this);
    }

    public static  int depth(SuffixTrieNode node) {
        int d = 0;
        SuffixTrieNode iter = node;

        while (iter.parent != null) {
            d++;
            iter = iter.parent;
        }
        return d;
    }

    public static  void appendSuffix(SuffixTrieNode node, List symList) {
        SuffixTrieNode iter = node;

        while (iter.parent != null) {
            symList.add(iter.symbol);
            iter = iter.parent;
        }
    }

    public void appendSuffix(List symList) {
        appendSuffix(this, symList);
    }

    public I getSymbol() {
        return symbol;
    }

    @Override
    public I getSymbol(int index) {
        return getSymbol(this, index);
    }

    public static  I getSymbol(SuffixTrieNode node, int index) {
        SuffixTrieNode iter = node;
        for (int i = index; i > 0; i--) {
            iter = iter.parent;
        }
        return iter.symbol;
    }

    public SuffixTrieNode getParent() {
        return parent;
    }

    public boolean isRoot() {
        return (parent == null);
    }

    public Word getSuffix() {
        if (parent == null) {
            return Word.epsilon();
        }
        WordBuilder wb = new WordBuilder<>(depth());
        appendSuffix(wb);
        return wb.toWord();
    }

    @Override
    public int length() {
        return depth();
    }

    @Override
    public Iterator iterator() {
        return new Iterator<>(this);
    }

    /**
     * Optimized iterator for the implicit word representation.
     *
     * @param 
     *         symbol class
     *
     * @author Malte Isberner
     */
    private static final class Iterator implements java.util.Iterator {

        private SuffixTrieNode current;

        Iterator(SuffixTrieNode node) {
            this.current = node;
        }

        @Override
        public boolean hasNext() {
            return !current.isRoot();
        }

        @Override
        public I next() {
            if (current.isRoot()) {
                throw new NoSuchElementException();
            }
            I sym = current.symbol;
            current = current.parent;
            return sym;
        }
    }
}