I
- symbol class@ParametersAreNonnullByDefault public abstract class Word<I> extends AbstractPrintable implements ArrayWritable<I>, Iterable<I>
Word
s are generally immutable,
i.e., a single Word
object will never change (unless symbol objects are modified,
which is however highly discouraged).
This class provides the following static methods for creating words in the most common scenarios:
epsilon()
returns the empty word of length 0
fromLetter(Object)
turns a single letter into a word of length 1
fromSymbols(Object...)
creates a word from an array of symbols
fromArray(Object[], int, int)
creates a word from a subrange of a symbols array
fromList(List)
creates a word from a List
of symbols
Modification operations like append(Object)
or concat(Word...)
create
new objects, subsequently invoking these operations on the respective objects returned is
therefore highly inefficient. If words need to be dynamically created, a WordBuilder
should be used.
This is an abstract base class for word representations. Implementing classes only need to implement
However, for the sake of efficiency it is highly encouraged to overwrite the other methods as well, providing specialized realizations.
Constructor and Description |
---|
Word() |
Modifier and Type | Method and Description |
---|---|
protected Word<I> |
_concat(Word<? extends I>... words)
Realizes the concatenation of this word with several other words.
|
protected Word<I> |
_subWord(int fromIndex,
int toIndex)
Internal subword operation implementation.
|
Word<I> |
append(I symbol)
Appends a symbol to this word and returns the result as a new word.
|
List<I> |
asList()
Retrieves a
List view on the contents of this word. |
static <I> Comparator<Word<? extends I>> |
canonicalComparator(Comparator<? super I> symComparator) |
Word<I> |
canonicalNext(Alphabet<I> sigma)
Retrieves the next word after this in canonical order.
|
Word<I> |
concat(Word<? extends I>... words)
Concatenates this word with several other words and returns the result
as a new word.
|
static <I> Word<I> |
epsilon()
Retrieves the empty word.
|
boolean |
equals(Object other) |
I |
firstSymbol()
Retrieves the first symbol of this word.
|
Word<I> |
flatten()
Retrieves a "flattened" version of this word, i.e., without any hierarchical structure
attached.
|
static <I> Word<I> |
fromArray(I[] symbols,
int offset,
int length)
Creates a word from a subrange of an array of symbols.
|
static <I> Word<I> |
fromLetter(I letter)
Constructs a word from a single letter.
|
static <I> Word<I> |
fromList(List<? extends I> symbolList)
Creates a word from a list of symbols
|
static Word<Character> |
fromString(String str) |
static <I> Word<I> |
fromSymbols(I... symbols)
Creates a word from an array of symbols.
|
static <I> Word<I> |
fromWords(Word<? extends I>... words) |
abstract I |
getSymbol(int index)
Return symbol that is at the specified position
|
int |
hashCode() |
boolean |
isEmpty()
Checks if this word is empty, i.e., contains no symbols.
|
boolean |
isPrefixOf(Word<?> other)
Checks if this word is a prefix of another word.
|
boolean |
isSuffixOf(Word<?> other)
Checks if this word is a suffix of another word
|
Iterator<I> |
iterator() |
I |
lastSymbol()
Retrieves the last symbol of this word.
|
abstract int |
length()
Retrieves the length of this word.
|
Word<I> |
longestCommonPrefix(Word<?> other)
Determines the longest common prefix of this word and another
word.
|
Word<I> |
longestCommonSuffix(Word<?> other)
Determines the longest common suffix of this word and another word.
|
Word<I> |
prefix(int prefixLen)
Retrieves a prefix of the given length.
|
List<? extends Word<I>> |
prefixes(boolean longestFirst)
Retrieves the list of all prefixes of this word.
|
Word<I> |
prepend(I symbol)
Prepends a symbol to this word and returns the result as a new word.
|
void |
print(Appendable a)
Outputs the current object.
|
int |
size()
The size of this container.
|
Word<I> |
subWord(int fromIndex)
Retrieves the subword of this word starting at the given index and extending
until the end of this word.
|
Word<I> |
subWord(int fromIndex,
int toIndex)
Retrieves a word representing the specified subrange of this word.
|
Word<I> |
suffix(int suffixLen)
Retrieves a suffix of the given length.
|
List<? extends Word<I>> |
suffixes(boolean longestFirst)
Retrieves the list of all suffixes of this word.
|
<T> Word<T> |
transform(Function<? super I,? extends T> transformer) |
Word<I> |
trimmed() |
static <I> Word<I> |
upcast(Word<? extends I> word) |
void |
writeToArray(int offset,
Object[] array,
int tgtOffset,
int length)
Writes the contents of this container to an array.
|
toString, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static <I> Comparator<Word<? extends I>> canonicalComparator(Comparator<? super I> symComparator)
@Nonnull public static <I> Word<I> epsilon()
Collections.emptyList()
@Nonnull public static <I> Word<I> fromLetter(@Nullable I letter)
letter
- the letter@SafeVarargs @Nonnull public static <I> Word<I> fromSymbols(I... symbols)
symbols
- the symbol array@Nonnull public static <I> Word<I> fromArray(I[] symbols, int offset, int length)
symbols
- the symbols arrayoffset
- the starting index in the arraylength
- the length of the resulting word (from the starting index on)@Nonnull public static <I> Word<I> fromList(List<? extends I> symbolList)
symbolList
- the list of symbols@SafeVarargs public static <I> Word<I> fromWords(Word<? extends I>... words)
@Nullable public abstract I getSymbol(int index)
index
- the positionpublic abstract int length()
public void print(Appendable a) throws IOException
Printable
print
in interface Printable
a
- the appendable.IOException
- if an error occurs during appending.public final int size()
ArrayWritable
size
in interface ArrayWritable<I>
@Nonnull public final Word<I> subWord(int fromIndex, int toIndex)
fromIndex
- the first index, inclusive.toIndex
- the last index, exclusive.@Nonnull public final Word<I> subWord(int fromIndex)
w.subWord(fromIndex, w.length())
fromIndex
- the first index, inclusive@Nonnull protected Word<I> _subWord(int fromIndex, int toIndex)
subWord(int, int)
,
no range checks need to be performed. As this method is flagged as protected,
implementations may rely on the specified indices being valid.fromIndex
- the first index, inclusive (guaranteed to be valid)toIndex
- the last index, exclusive (guaranteed to be valid)public void writeToArray(int offset, Object[] array, int tgtOffset, int length)
ArrayWritable
System.arraycopy(this.toArray(), offset, array, tgtOfs, num);
writeToArray
in interface ArrayWritable<I>
offset
- how many elements of this container to skip.array
- the array in which to store the elements.tgtOffset
- the starting offset in the target array.length
- the maximum number of elements to copy.@Nonnull public List<I> asList()
List
view on the contents of this word.@Nonnull public final Word<I> prefix(int prefixLen)
length
is negative, then a prefix consisting of all but the last
-length
symbols is returned.prefixLen
- the length of the prefix (may be negative, see above).@Nonnull public final Word<I> suffix(int suffixLen)
length
is
negative, then a suffix consisting of all but the first
-length
symbols is returned.suffixLen
- the length of the suffix (may be negative, see above).@Nonnull public List<? extends Word<I>> prefixes(boolean longestFirst)
List.get(int)
or Iterator#next()
.longestFirst
- whether to start with the longest prefix (otherwise, the first prefix
in the list will be the shortest).@Nonnull public List<? extends Word<I>> suffixes(boolean longestFirst)
List.get(int)
or Iterator#next()
.longestFirst
- whether to start with the longest suffix (otherwise, the first suffix
in the list will be the shortest).@Nonnull public Word<I> canonicalNext(Alphabet<I> sigma)
sigma
- the alphabet@Nullable public I lastSymbol()
@Nullable public I firstSymbol()
@Nonnull public Word<I> append(@Nullable I symbol)
symbol
- the symbol to append@Nonnull public Word<I> prepend(@Nullable I symbol)
symbol
- the symbol to prepend@SafeVarargs @Nonnull public final Word<I> concat(Word<? extends I>... words)
_concat(Word...)
method instead.words
- the words to concatenate with this word_concat(Word...)
@Nonnull protected Word<I> _concat(Word<? extends I>... words)
words
- the words to concatenatepublic boolean isPrefixOf(Word<?> other)
other
- the other word@Nonnull public Word<I> longestCommonPrefix(Word<?> other)
other
- the other wordpublic boolean isSuffixOf(Word<?> other)
other
- the other word@Nonnull public Word<I> longestCommonSuffix(Word<?> other)
other
- the other word@Nonnull public Word<I> flatten()
Word
is subclassed to allow representing, e.g., a concatenation
dynamically, but due to performance concerns not too many levels of indirection
should be introduced.public boolean isEmpty()
Copyright © 2015. All rights reserved.