Search in sources :

Example 1 with Long2FloatOpenHashMap

use of it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap in project elki by elki-project.

the class FileBasedSparseFloatDistanceFunction method loadCache.

/**
 * Fill cache from an input stream.
 *
 * @param size Expected size
 * @param in Input stream
 * @throws IOException
 */
protected void loadCache(int size, InputStream in) throws IOException {
    // Expect a sparse matrix here
    cache = new Long2FloatOpenHashMap(size * 20);
    cache.defaultReturnValue(Float.POSITIVE_INFINITY);
    min = Integer.MAX_VALUE;
    max = Integer.MIN_VALUE;
    parser.parse(in, new DistanceCacheWriter() {

        @Override
        public void put(int id1, int id2, double distance) {
            if (id1 < id2) {
                min = id1 < min ? id1 : min;
                max = id2 > max ? id2 : max;
            } else {
                min = id2 < min ? id2 : min;
                max = id1 > max ? id1 : max;
            }
            cache.put(makeKey(id1, id2), (float) distance);
        }
    });
    if (min != 0) {
        LOG.verbose("Distance matrix is supposed to be 0-indexed. Choosing offset " + min + " to compensate.");
    }
    if (max + 1 - min != size) {
        LOG.warning("ID range is not consistent with relation size.");
    }
}
Also used : Long2FloatOpenHashMap(it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap)

Example 2 with Long2FloatOpenHashMap

use of it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap in project angel by Tencent.

the class StreamSerdeUtils method deserializeLongFloatVector.

private static LongFloatVector deserializeLongFloatVector(DataInputStream in, long dim) throws IOException {
    int storageType = deserializeInt(in);
    switch(storageType) {
        case SPARSE_STORAGE_TYPE:
            int len = deserializeInt(in);
            Long2FloatOpenHashMap idToValueMap = new Long2FloatOpenHashMap(len);
            for (int i = 0; i < len; i++) {
                idToValueMap.put(deserializeInt(in), deserializeFloat(in));
            }
            return new LongFloatVector((int) dim, new LongFloatSparseVectorStorage((int) dim, idToValueMap));
        case SORTED_STORAGE_TYPE:
            return VFactory.sortedLongKeyFloatVector((int) dim, deserializeLongs(in), deserializeFloats(in));
        default:
            throw new UnsupportedOperationException("Unsupport storage type " + storageType);
    }
}
Also used : Long2FloatOpenHashMap(it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)

Example 3 with Long2FloatOpenHashMap

use of it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap in project angel by Tencent.

the class ByteBufSerdeUtils method deserializeLongFloatVector.

private static LongFloatVector deserializeLongFloatVector(ByteBuf in, long dim) {
    int storageType = deserializeInt(in);
    switch(storageType) {
        case SPARSE_STORAGE_TYPE:
            int len = deserializeInt(in);
            Long2FloatOpenHashMap idToValueMap = new Long2FloatOpenHashMap(len);
            for (int i = 0; i < len; i++) {
                idToValueMap.put(deserializeInt(in), deserializeFloat(in));
            }
            return new LongFloatVector((int) dim, new LongFloatSparseVectorStorage((int) dim, idToValueMap));
        case SORTED_STORAGE_TYPE:
            return VFactory.sortedLongKeyFloatVector((int) dim, deserializeLongs(in), deserializeFloats(in));
        default:
            throw new UnsupportedOperationException("Unsupport storage type " + storageType);
    }
}
Also used : Long2FloatOpenHashMap(it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)

Aggregations

Long2FloatOpenHashMap (it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap)3 LongFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)2 LongFloatVector (com.tencent.angel.ml.math2.vector.LongFloatVector)2