I
- symbol type@ParametersAreNonnullByDefault public abstract class Word<I> extends AbstractPrintable implements ArrayWritable<I>, Iterable<I>, Serializable
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 |
---|---|
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.
|
protected Word<I> |
concatInternal(Word<? extends I>... words)
Realizes the concatenation of this word with several other words.
|
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 Word<Character> |
fromCharSequence(CharSequence cs) |
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(Collection<? extends Word<? extends I>> words) |
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.
|
Stream<I> |
parallelStream() |
Word<I> |
prefix(int prefixLen)
Retrieves a prefix of the given length.
|
List<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.
|
Spliterator<I> |
spliterator() |
Stream<I> |
stream() |
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.
|
protected Word<I> |
subWordInternal(int fromIndex,
int toIndex)
Internal subword operation implementation.
|
Word<I> |
suffix(int suffixLen)
Retrieves a suffix of the given length.
|
List<Word<I>> |
suffixes(boolean longestFirst)
Retrieves the list of all suffixes of this word.
|
int[] |
toIntArray(ToIntFunction<? super I> toInt)
Transforms this word into an array of integers, using the specified function for translating an individual symbol
to an integer.
|
<T> Word<T> |
transform(Function<? super I,? extends T> transformer)
Transforms a word symbol-by-symbol, using the specified transformation function.
|
Word<I> |
trimmed() |
static <I> Word<I> |
upcast(Word<? extends I> word)
Performs an upcast of the generic type parameter of the word.
|
void |
writeToArray(int offset,
Object[] array,
int tgtOffset,
int length)
Writes the contents of this container to an array.
|
toString
public static <I> Comparator<Word<? extends I>> canonicalComparator(Comparator<? super I> symComparator)
@SafeVarargs @Nonnull public static <I> Word<I> fromSymbols(I... symbols)
symbols
- the symbol array@Nonnull public static <I> Word<I> epsilon()
Collections.emptyList()
@Nonnull public static <I> Word<I> fromLetter(@Nullable I letter)
letter
- the letter@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@Nonnull public static Word<Character> fromCharSequence(CharSequence cs)
@SafeVarargs public static <I> Word<I> fromWords(Word<? extends I>... words)
public static <I> Word<I> fromWords(Collection<? extends Word<? extends I>> words)
public abstract int length()
public static <I> Word<I> upcast(Word<? extends I> word)
<I>
is covariant (even though it is not possible to express this in Java), making this a safe operation.word
- the word to upcastword
)public Spliterator<I> spliterator()
spliterator
in interface Iterable<I>
public void print(Appendable a) throws IOException
Printable
print
in interface Printable
a
- the appendable.IOException
- if an error occurs during appending.public boolean isEmpty()
true
if this word is empty, false
otherwise.@Nonnull public final Word<I> subWord(int fromIndex)
w.subWord(fromIndex, w.length())
fromIndex
- the first index, inclusive@Nonnull public final Word<I> subWord(int fromIndex, int toIndex)
fromIndex
- the first index, inclusive.toIndex
- the last index, exclusive.@Nonnull protected Word<I> subWordInternal(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.@Nullable public abstract I getSymbol(int index)
index
- the positionnull
if no such symbol existspublic final int size()
ArrayWritable
size
in interface ArrayWritable<I>
@Nonnull public List<I> asList()
List
view on the contents of this word.@Nonnull public List<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<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)
k
alphabet
symbols, one can think of a word of length n
as an n
-digit radix-k
representation of
the number. The next word in canonical order is the representation for the number represented by this word plus
one.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)
Note that this method cannot be overridden. Implementing classes need to override the concatInternal(Word...)
method instead.
words
- the words to concatenate with this wordconcatInternal(Word...)
@Nonnull protected Word<I> concatInternal(Word<? extends I>... words)
words
- the words to concatenatepublic boolean isPrefixOf(Word<?> other)
other
- the other wordtrue
if this word is a prefix of the other word, false
otherwise.@Nonnull public Word<I> longestCommonPrefix(Word<?> other)
other
- the other 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).public boolean isSuffixOf(Word<?> other)
other
- the other wordtrue
if this word is a suffix of the other word, false
otherwise.@Nonnull public Word<I> longestCommonSuffix(Word<?> other)
other
- the other word@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 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 int[] toIntArray(ToIntFunction<? super I> toInt)
toInt
- the function for translating symbols to integersCopyright © 2019. All rights reserved.