public class MWHCFunction<T> extends AbstractObject2LongFunction<T> implements Serializable, Size64
Warning: With Sux4J 2.1, the semantics of a few constructors has changed significantly.
Instance of this class store a function from keys to values. Keys are provided by an iterable object (whose iterators
must return elements in a consistent order), whereas values are provided by a LongIterable.
A convenient constructor
automatically assigns to each key its rank (e.g., its position in iteration order).
As a commodity, this class provides a main method that reads from standard input a (possibly gzip'd) sequence of newline-separated strings, and writes a serialised function mapping each element of the list to its position.
This class provides a great amount of flexibility when creating a new function. This is reflected in the large number of constructors, which can be quite confusing. To exploit the various possibilities, you must understand some details of the construction.
In a first phase, we build a ChunkedHashStore containing hashes of the keys and associated values. Depending
on the constructor chosen, the store will associate each hash either with the ordinal position of the key, or with the
value that must be associated with the key. It is easy to tell the difference because in constructors associating the ordinal
position the store appears just after the keys and the transformation strategy as, for example, in
MWHCFunction(Iterable, TransformationStrategy, ChunkedHashStore, LongIterable, int) or
MWHCFunction(Iterable, TransformationStrategy, ChunkedHashStore). If the store appears after keys and values
(e.g., MWHCFunction(Iterable, TransformationStrategy, LongIterable, int, ChunkedHashStore)) the store associates
each key with its value instead.
It is important to understand this difference because many constructors makes it possible to pass a chunked hash store.
This class will try to use the store before building a new one (possibly because of a ChunkedHashStore.DuplicateException),
with obvious benefits in terms of performance. If the store is not checked, and a ChunkedHashStore.DuplicateException is
thrown, the constructors will try to rebuild the store, but this requires, of course, that the keys, and possibly the values, are available. Constructors
without keys (e.g., MWHCFunction(TransformationStrategy, ChunkedHashStore, int)) assume that the store is checked.
Note that it is your responsibility to pass a correct store.
The reason for this proliferation of constructors is that they are useful in different situations. The constructors
that store associations with values are extremely scalable because the values must just be a LongIterable that
will be scanned sequentially during the store construction. On the other hand, if you have already a store that
associates ordinal positions, and you want to build a new function for which a LongList of values needs little space (e.g.,
because it is described implicitly), you can use a constructor that accepts a LongList but uses the already built store.
Then, we measure whether it is favourable to compact the function, that is, to store nonzero values in a separate array, using a ranked marker array to record the positions of nonzero values.
A non-compacted, r-bit MWHCFunction on n elements requires γrn
bits, whereas the compacted version takes just (γ + r)n bits (plus the bits that are necessary for the
ranking structure; the current implementation uses Rank16). This class will transparently chose
the most space-efficient method.
| Modifier and Type | Field and Description |
|---|---|
protected LongBigList |
data
The final magick—the list of modulo-3 values that define the output of the minimal hash function.
|
protected long |
globalSeed
The seed used to generate the initial hash triple.
|
static int |
LOG2_CHUNK_SIZE
The logarithm of the desired chunk size.
|
protected long |
m
The number of vertices of the intermediate hypergraph.
|
protected LongArrayBitVector |
marker
|
protected long |
n
The number of elements.
|
protected long[] |
offset
The start offset of each block.
|
protected Rank16 |
rank
The ranking structure on
marker. |
protected long[] |
seed
The seed of the underlying 3-hypergraphs.
|
protected TransformationStrategy<? super T> |
transform
The transformation strategy to turn objects of type
T into bit vectors. |
protected int |
width
The data width.
|
defRetValue| Modifier | Constructor and Description |
|---|---|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform)
Creates a new function for the given elements that will map them to their ordinal position.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
ChunkedHashStore<T> chunkedHashStore)
Creates a new function for the given elements that will map them to their ordinal position.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
ChunkedHashStore<T> chunkedHashStore,
LongIterable values,
int width)
Creates a new function for the given elements using a chunked hash store containing ordinal positions.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
File tempDir)
Creates a new function for the given elements that will map them to their ordinal position.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
File tempDir,
ChunkedHashStore<T> chunkedHashStore)
Creates a new function for the given elements that will map them to their ordinal position.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
File tempDir,
ChunkedHashStore<T> chunkedHashStore,
LongIterable values,
int width)
Creates a new function for the given elements using a chunked hash store containing ordinal positions.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
LongIterable values,
int width)
Creates a new function for the given elements and values.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
LongIterable values,
int width,
ChunkedHashStore<T> chunkedHashStore)
Creates a new function for the given elements and values.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
LongIterable values,
int width,
File tempDir)
Creates a new function for the given elements and values.
|
|
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
LongIterable values,
int width,
File tempDir,
ChunkedHashStore<T> chunkedHashStore)
Creates a new function for the given elements and values.
|
protected |
MWHCFunction(Iterable<? extends T> elements,
TransformationStrategy<? super T> transform,
LongIterable values,
int width,
File tempDir,
ChunkedHashStore<T> chunkedHashStore,
boolean override)
Creates a new function for the given elements and values.
|
protected |
MWHCFunction(MWHCFunction<T> function)
Creates a new function by copying a given one; non-transient fields are (shallow) copied.
|
|
MWHCFunction(TransformationStrategy<? super T> transform,
ChunkedHashStore<T> chunkedHashStore,
int width)
Creates a new function using a given checked chunked hash store.
|
|
MWHCFunction(TransformationStrategy<? super T> transform,
ChunkedHashStore<T> chunkedHashStore,
LongIterable values,
int width)
Creates a new function using a given checked chunked hash store containing ordinal positions and a list of values.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
containsKey(Object o) |
long |
getLong(Object o) |
long |
getLongByTriple(long[] triple) |
static void |
main(String[] arg) |
long |
numBits()
Returns the number of bits used by this structure.
|
int |
size()
Deprecated.
|
long |
size64()
Returns the number of elements in the function domain.
|
clear, defaultReturnValue, defaultReturnValue, get, put, put, remove, removeLongpublic static final int LOG2_CHUNK_SIZE
protected final long n
protected final long m
protected final int width
protected final long globalSeed
protected final long[] seed
protected final long[] offset
protected final LongBigList data
protected final LongArrayBitVector marker
protected final TransformationStrategy<? super T> transform
T into bit vectors.public MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, ChunkedHashStore<T> chunkedHashStore) throws IOException
elements - the elements in the domain of the function.transform - a transformation strategy for the elements.chunkedHashStore - a (not necessarily checked) chunked hash store containing the elements, or null.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, File tempDir, ChunkedHashStore<T> chunkedHashStore) throws IOException
elements - the elements in the domain of the function.transform - a transformation strategy for the elements.tempDir - a temporary directory for the store files, or null for the standard temporary directory.chunkedHashStore - a (not necessarily checked) chunked hash store containing the elements, or null.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform) throws IOException
elements - the elements in the domain of the function.transform - a transformation strategy for the elements.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, File tempDir) throws IOException
elements - the elements in the domain of the function.transform - a transformation strategy for the elements.tempDir - a temporary directory for the store files, or null for the standard temporary directory.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, LongIterable values, int width, ChunkedHashStore<T> chunkedHashStore) throws IOException
Warning: if you were using this constructor before Sux4J 2.1, you should switch
to MWHCFunction(Iterable, TransformationStrategy, ChunkedHashStore, LongIterable, int), as the
semantics has changed (see the class documentation).
elements - the elements in the domain of the function, or null.transform - a transformation strategy for the elements.values - values to be assigned to each element, in the same order of the iterator returned by elements; if null, the
assigned value will the the ordinal number of each element.width - the bit width of the values, or -1 if values is null.chunkedHashStore - a chunked hash store containing the elements associated with their value, or null; the store
can be unchecked, but in this case elements and transform must be non-null.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, LongIterable values, int width, File tempDir, ChunkedHashStore<T> chunkedHashStore) throws IOException
elements - the elements in the domain of the function, or null.transform - a transformation strategy for the elements.values - values to be assigned to each element, in the same order of the iterator returned by elements; if null, the
assigned value will the the ordinal number of each element.width - the bit width of the values, or -1 if values is null.tempDir - a temporary directory for the store files, or null for the standard temporary directory.chunkedHashStore - a chunked hash store containing the elements associated with their value, or null; the store
can be unchecked, but in this case elements and transform must be non-null.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, LongIterable values, int width) throws IOException
elements - the elements in the domain of the function.transform - a transformation strategy for the elements.values - values to be assigned to each element, in the same order of the iterator returned by elements; if null, the
assigned value will the the ordinal number of each element.width - the bit width of the values.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, LongIterable values, int width, File tempDir) throws IOException
elements - the elements in the domain of the function.transform - a transformation strategy for the elements.values - values to be assigned to each element, in the same order of the iterator returned by elements; if null, the
assigned value will the the ordinal number of each element.width - the bit width of the values.tempDir - a temporary directory for the store files, or null for the standard temporary directory.IOExceptionpublic MWHCFunction(TransformationStrategy<? super T> transform, ChunkedHashStore<T> chunkedHashStore, int width) throws IOException
transform - a transformation strategy for the elements.chunkedHashStore - a checked chunked hash store containing the elements associated with their value.width - the bit width of the values contained in chunkedHashStore.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, ChunkedHashStore<T> chunkedHashStore, LongIterable values, int width) throws IOException
elements - the elements in the domain of the function, or null.transform - a transformation strategy for the elements.values - values to be assigned to each element, in the same order of the iterator returned by elements; if null, the
assigned value will the the ordinal number of each element.width - the bit width of the values.chunkedHashStore - a chunked hash store containing the elements associated with their ordinal position, or null; the store
can be unchecked, but in this case elements and transform must be non-null.IOExceptionpublic MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, File tempDir, ChunkedHashStore<T> chunkedHashStore, LongIterable values, int width) throws IOException
elements - the elements in the domain of the function, or null.transform - a transformation strategy for the elements.values - values to be assigned to each element, in the same order of the iterator returned by elements; if null, the
assigned value will the the ordinal number of each element.width - the bit width of the values.tempDir - a temporary directory for the store files, or null for the standard temporary directory.chunkedHashStore - a chunked hash store containing the elements associated with their ordinal position, or null; the store
can be unchecked, but in this case elements and transform must be non-null.IOExceptionpublic MWHCFunction(TransformationStrategy<? super T> transform, ChunkedHashStore<T> chunkedHashStore, LongIterable values, int width) throws IOException
transform - a transformation strategy for the elements.chunkedHashStore - a checked chunked hash store containing the elements associated with their ordinal position.values - values to be assigned to each element.width - the bit width of the values.IOExceptionprotected MWHCFunction(Iterable<? extends T> elements, TransformationStrategy<? super T> transform, LongIterable values, int width, File tempDir, ChunkedHashStore<T> chunkedHashStore, boolean override) throws IOException
elements - the elements in the domain of the function, or null.transform - a transformation strategy for the elements.values - values to be assigned to each element, in the same order of the iterator returned by elements; if null, the
assigned value will the the ordinal number of each element.width - the bit width of the values, or -1 if values is null.chunkedHashStore - a chunked hash store containing the elements associated with their ordinal position, or null; the store
can be unchecked, but in this case elements and transform must be non-null.override - if true, chunkedHashStore contains ordinal positions, and values is a LongIterable that
must be accessed to retrieve the actual values.IOExceptionprotected MWHCFunction(MWHCFunction<T> function)
function - the function to be copied.public long getLong(Object o)
getLong in interface Object2LongFunction<T>public long getLongByTriple(long[] triple)
public long size64()
@Deprecated public int size()
public long numBits()
public boolean containsKey(Object o)
containsKey in interface Function<T,Long>public static void main(String[] arg) throws NoSuchMethodException, IOException, JSAPException