I
- input symbol typepublic class SymbolHidingAlphabet<I> extends Object implements GrowingAlphabet<I>, Serializable
Alphabet.containsSymbol(Object)
) return false
even though the symbol is contained in the wrapped
alphabet. Adding the hidden symbol to this alphabet will return the correct return values for the corresponding
methods (e.g. its index for GrowingAlphabet.addSymbol(Object)
) but not add the symbol to the backing alphabet
if the backing alphabet already contained the symbol.
Note: For now, this only holds for containment checks and add-methods. Other methods, such as
Collection.iterator()
or Alphabet.getSymbol(int)
) access the underlying alphabet directly and do not
hide symbols.
The purpose of this class is to help management of mutable (e.g. growing
) alphabets that are
shared across several components (automata, learning algorithms) that use the alphabet state to skip redundant work
(e.g. allocate memory only for every unique symbol). Since updates to the wrapped alphabet are directly present at
the components that hold a reference, additional update-steps of the components may be wrongfully skipped.
Constructor and Description |
---|
SymbolHidingAlphabet(GrowingAlphabet<I> alphabet) |
Modifier and Type | Method and Description |
---|---|
boolean |
add(I i) |
boolean |
addAll(Collection<? extends I> c) |
int |
addSymbol(I a)
Adds a new symbol to the alphabet.
|
I |
apply(int index) |
int |
applyAsInt(I symbol) |
void |
clear() |
int |
compare(I o1,
I o2) |
boolean |
contains(Object o) |
boolean |
containsAll(Collection<?> c) |
boolean |
containsSymbol(I symbol)
Checks whether the given symbol is part of the alphabet.
|
GrowingAlphabet<I> |
getDelegate()
Returns the wrapped alphabet.
|
I |
getHiddenSymbol()
Returns the hidden symbol.
|
I |
getSymbol(int index)
Returns the symbol with the given index in this alphabet.
|
int |
getSymbolIndex(I symbol)
Returns the index of the given symbol in the alphabet.
|
void |
hideSymbol(I symbolToHide)
Hides the given symbols, i.e. after invoking this method, calls to containment methods (e.g.
|
boolean |
isEmpty() |
boolean |
isHiding()
Returns whether this wrapper is currently hiding the symbol returned by
getHiddenSymbol() . |
Iterator<I> |
iterator() |
boolean |
remove(Object o) |
boolean |
removeAll(Collection<?> c) |
boolean |
removeIf(Predicate<? super I> filter) |
boolean |
retainAll(Collection<?> c) |
static <I> void |
runWhileHiding(Alphabet<I> alphabet,
I symbol,
Runnable block)
If the given alphabet is a
SymbolHidingAlphabet , the given code block is executed while hiding the given
symbol. |
int |
size()
The size of this container.
|
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
<I2> Mapping<I2,I> |
translateFrom(Alphabet<I2> other) |
void |
unhide()
Unhides the previously hidden symbol, i.e. after invoking this methods all invocations on
this object
directly delegate to the wrapped alphabet. |
static <I> Alphabet<I> |
wrapIfMutable(Alphabet<I> alphabet)
Checks if the given alphabet is a mutable (i.e.
|
void |
writeToArray(int offset,
Object[] array,
int tgtOfs,
int num)
Writes the contents of this container to an array.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals, hashCode, parallelStream, spliterator, stream
comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
public SymbolHidingAlphabet(GrowingAlphabet<I> alphabet)
public void hideSymbol(I symbolToHide)
Alphabet.containsSymbol(Object)
) will return false
even if the symbol is contained in the wrapped
alphabet.symbolToHide
- the symbol to hidepublic void unhide()
this
object
directly delegate to the wrapped alphabet.public static <I> Alphabet<I> wrapIfMutable(Alphabet<I> alphabet)
GrowingAlphabet
) instance and wraps it in a
SymbolHidingAlphabet
instance. Otherwise, the given alphabet is returned as-is.I
- input symbol typealphabet
- the alphabet to wrapSymbolHidingAlphabet
wrapping the given alphabet if the given alphabet is a
GrowingAlphabet
, the original alphabet otherwisepublic static <I> void runWhileHiding(Alphabet<I> alphabet, I symbol, Runnable block)
SymbolHidingAlphabet
, the given code block is executed while hiding the given
symbol. Otherwise, the code block is run as-is.I
- input symbol typealphabet
- the alphabetsymbol
- the symbol to hideblock
- the block to executepublic GrowingAlphabet<I> getDelegate()
public I getHiddenSymbol()
public boolean isHiding()
getHiddenSymbol()
.true
if this
alphabet is currently hiding the symbol returned by
getHiddenSymbol()
, false
otherwise.public int addSymbol(@Nullable I a)
GrowingAlphabet
addSymbol
in interface GrowingAlphabet<I>
public I apply(int index)
@Nullable public I getSymbol(int index) throws IllegalArgumentException
Alphabet
getSymbol
in interface Alphabet<I>
index
- the index of the requested symbol.IllegalArgumentException
- if there is no symbol with this index.public int applyAsInt(I symbol)
applyAsInt
in interface ToIntFunction<I>
applyAsInt
in interface Alphabet<I>
public int getSymbolIndex(@Nullable I symbol) throws IllegalArgumentException
Alphabet
getSymbolIndex
in interface Alphabet<I>
IllegalArgumentException
- if the provided symbol does not belong to the alphabet.public void writeToArray(int offset, Object[] array, int tgtOfs, int num)
ArrayWritable
System.arraycopy(this.toArray(), offset, array, tgtOfs, num);
writeToArray
in interface ArrayWritable<I>
writeToArray
in interface Alphabet<I>
offset
- how many elements of this container to skip.array
- the array in which to store the elements.tgtOfs
- the starting offset in the target array.num
- the maximum number of elements to copy.public <I2> Mapping<I2,I> translateFrom(Alphabet<I2> other)
translateFrom
in interface Alphabet<I>
public boolean containsSymbol(I symbol)
Alphabet
Caution: the default implementation is rather inefficient and should be overridden, if possible.
containsSymbol
in interface Alphabet<I>
symbol
- the symbol to checktrue
iff the symbol is part of the alphabetpublic int size()
ArrayWritable
size
in interface Collection<I>
size
in interface ArrayWritable<I>
public boolean isEmpty()
isEmpty
in interface Collection<I>
public boolean contains(Object o)
contains
in interface Collection<I>
public Object[] toArray()
toArray
in interface Collection<I>
public <T> T[] toArray(T[] a)
toArray
in interface Collection<I>
public boolean add(I i)
add
in interface Collection<I>
public boolean remove(Object o)
remove
in interface Collection<I>
public boolean containsAll(Collection<?> c)
containsAll
in interface Collection<I>
public boolean addAll(Collection<? extends I> c)
addAll
in interface Collection<I>
public boolean removeAll(Collection<?> c)
removeAll
in interface Collection<I>
public boolean removeIf(Predicate<? super I> filter)
removeIf
in interface Collection<I>
public boolean retainAll(Collection<?> c)
retainAll
in interface Collection<I>
public void clear()
clear
in interface Collection<I>
Copyright © 2018. All rights reserved.