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

net.amygdalum.util.text.linkeddawg.LinkedCharDawg Maven / Gradle / Ivy

The newest version!
package net.amygdalum.util.text.linkeddawg;

import java.util.Iterator;
import java.util.NoSuchElementException;

import net.amygdalum.util.text.CharAutomaton;
import net.amygdalum.util.text.CharDawg;
import net.amygdalum.util.text.CharNavigator;
import net.amygdalum.util.text.CharNode;

/**
 * A LinkedCharWordGraph is a Directed Acyclic Word Graph based on characters.
 * It has following properties: - acyclic (no back links, no support links) -
 * each node may be reached by one multiple other nodes (i.e. not a tree)
 * 
 * @param  the type of attachment storable in each graph node
 */
public class LinkedCharDawg implements CharDawg {

	private CharNode root;

	public LinkedCharDawg(CharNode root) {
		this.root = root;
	}

	@Override
	public CharAutomaton cursor() {
		return new Cursor<>(root);
	}

	@Override
	public boolean contains(char[] chars) {
		CharNode node = root;
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			node = node.nextNode(c);
			if (node == null) {
				return false;
			}
		}
		return true;
	}

	@Override
	public T find(char[] chars) {
		CharNode node = root;
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			node = node.nextNode(c);
			if (node == null) {
				return null;
			}
		}
		return node.getAttached();
	}

	@Override
	public CharNavigator navigator() {
		return new LinkedCharNavigator<>(root);
	}

	private static class Cursor implements CharAutomaton {

		private CharNode current;
		private CharNode root;
		private AttachmentIterator iterator;

		public Cursor(CharNode root) {
			this.root = root;
			this.current = root;
			this.iterator = new AttachmentIterator<>();
		}

		@Override
		public Iterator iterator() {
			iterator.init(current);
			return iterator;
		}

		@Override
		public void reset() {
			this.current = root;
		}

		@Override
		public boolean lookahead(char c) {
			return current.nextNode(c) != null;
		}

		@Override
		public boolean accept(char c) {
			CharNode next = current.nextNode(c);
			if (next == null) {
				return false;
			}
			current = next;
			return true;
		}

		@Override
		public boolean hasAttachments() {
			return current != null
				&& current.getAttached() != null;
		}

	}

	private static class AttachmentIterator implements Iterator {

		private CharNode current;

		public void init(CharNode current) {
			this.current = current;
		}

		@Override
		public boolean hasNext() {
			return current != null
				&& current.getAttached() != null;
		}

		@Override
		public S next() {
			if (current != null) {
				S attached = current.getAttached();
				current = null;
				if (attached != null) {
					return attached;
				}
			}
			throw new NoSuchElementException();
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}

	}
}