Java Code Examples for it.unimi.dsi.fastutil.ints.Int2ObjectMap

The following examples show how to use it.unimi.dsi.fastutil.ints.Int2ObjectMap. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: presto   Source File: FixedLifespanScheduler.java    License: Apache License 2.0 6 votes vote down vote up
public FixedLifespanScheduler(BucketNodeMap bucketNodeMap, List<ConnectorPartitionHandle> partitionHandles, OptionalInt concurrentLifespansPerTask)
{
    checkArgument(!partitionHandles.equals(ImmutableList.of(NOT_PARTITIONED)));
    checkArgument(partitionHandles.size() == bucketNodeMap.getBucketCount());

    Map<InternalNode, IntList> nodeToDriverGroupMap = new HashMap<>();
    Int2ObjectMap<InternalNode> driverGroupToNodeMap = new Int2ObjectOpenHashMap<>();
    for (int bucket = 0; bucket < bucketNodeMap.getBucketCount(); bucket++) {
        InternalNode node = bucketNodeMap.getAssignedNode(bucket).get();
        nodeToDriverGroupMap.computeIfAbsent(node, key -> new IntArrayList()).add(bucket);
        driverGroupToNodeMap.put(bucket, node);
    }

    this.driverGroupToNodeMap = driverGroupToNodeMap;
    this.nodeToDriverGroupsMap = nodeToDriverGroupMap.entrySet().stream()
            .collect(toImmutableMap(Map.Entry::getKey, entry -> entry.getValue().iterator()));
    this.partitionHandles = requireNonNull(partitionHandles, "partitionHandles is null");
    if (concurrentLifespansPerTask.isPresent()) {
        checkArgument(concurrentLifespansPerTask.getAsInt() >= 1, "concurrentLifespansPerTask must be great or equal to 1 if present");
    }
    this.concurrentLifespansPerTask = requireNonNull(concurrentLifespansPerTask, "concurrentLifespansPerTask is null");
}
 
Example 2
Source Project: fastjgame   Source File: FastCollectionsUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 移除map中符合条件的元素,并对删除的元素执行后续的操作
 *
 * @param map       必须是可修改的map
 * @param predicate 过滤条件,为真的删除
 * @param then      元素删除之后执行的逻辑
 * @param <V>       the type of value
 * @return 删除的元素数量
 */
public static <V> int removeIfAndThen(final Int2ObjectMap<V> map, final IntObjPredicate<? super V> predicate, IntObjConsumer<V> then) {
    if (map.size() == 0) {
        return 0;
    }

    ObjectIterator<Int2ObjectMap.Entry<V>> itr = map.int2ObjectEntrySet().iterator();
    int removeNum = 0;
    Int2ObjectMap.Entry<V> entry;
    int k;
    V v;
    while (itr.hasNext()) {
        entry = itr.next();
        k = entry.getIntKey();
        v = entry.getValue();
        if (predicate.test(k, v)) {
            itr.remove();
            removeNum++;
            then.accept(k, v);
        }
    }
    return removeNum;
}
 
Example 3
Source Project: Kettle   Source File: ArrayMap.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public ObjectSet<Int2ObjectMap.Entry<V>> int2ObjectEntrySet() {
    return entrySet != null ? entrySet : (entrySet = new AbstractObjectSet<Int2ObjectMap.Entry<V>>() {
        @Override
        @Nonnull
        public ObjectIterator<Int2ObjectMap.Entry<V>> iterator() {
            return new EntryIterator<Int2ObjectMap.Entry<V>>() {
                @Override
                public Int2ObjectMap.Entry<V> next() {
                    return nextEntry();
                }
            };
        }

        @Override
        public int size() {
            return size;
        }
    });
}
 
Example 4
Source Project: metanome-algorithms   Source File: HybridMDEnforcer.java    License: Apache License 2.0 6 votes vote down vote up
private MDEnforcer build() {
	DictionaryRecords leftRecords = preprocessed.getLeftRecords();
	DictionaryRecords rightRecords = preprocessed.getRightRecords();
	Map<ColumnMapping<?>, PreprocessedColumnPair> columnPairs = getColumnPairs();
	EnforcerFactory factory = EnforcerFactory.builder()
		.leftRecords(leftRecords)
		.rightRecords(rightRecords)
		.columnPairs(columnPairs)
		.build();
	List<Dictionary<?>> leftDictionaries = preprocessed.getLeftDictionaries();
	List<Int2ObjectMap<?>> leftInvertedDictionaries = invert(leftDictionaries);
	RecordInflater leftInflater = new RecordInflater(leftInvertedDictionaries);
	List<Dictionary<?>> rightDictionaries = preprocessed.getRightDictionaries();
	List<Int2ObjectMap<?>> rightInvertedDictionaries = invert(rightDictionaries);
	RecordInflater rightInflater = new RecordInflater(rightInvertedDictionaries);
	return new HybridMDEnforcer(factory, leftInflater, rightInflater);
}
 
Example 5
Source Project: metanome-algorithms   Source File: AgreeSetGenerator.java    License: Apache License 2.0 6 votes vote down vote up
private void calculateRelationship(StrippedPartition partitions, Int2ObjectMap<TupleEquivalenceClassRelation> relationships) {

        int partitionNr = 0;
        for (IntList partition : partitions.getValues()) {
            if (this.debugSysout)
                System.out.println(".");
            for (int index : partition) {
                if (!relationships.containsKey(index)) {
                    relationships.put(index, new TupleEquivalenceClassRelation());
                }
                relationships.get(index).addNewRelationship(partitions.getAttributeID(), partitionNr);
            }
            partitionNr++;
        }

    }
 
Example 6
Source Project: metanome-algorithms   Source File: AgreeSetGenerator.java    License: Apache License 2.0 6 votes vote down vote up
private void handleList(IntList list, Int2ObjectMap<Set<IntList>> maxSets, boolean firstStep) {

        for (int i = 0; i < list.size(); i++) {
        	int removedElement = list.removeInt(i);
            if (maxSets.containsKey(list.size()) && maxSets.get(list.size()).contains(list))
                maxSets.get(list.size()).remove(list);
            else {
                if (list.size() > 2) {
                    this.handleList(list, maxSets, false);
                }
            }
            list.add(i, removedElement);
        }

        if (firstStep)
            maxSets.get(list.size()).add(list);
    }
 
Example 7
protected void buildNonCompositeAggregatorIDMap(String aggregatorName, FieldsDescriptor inputDescriptor,
    IntArrayList aggIDList, Int2ObjectMap<FieldsDescriptor> inputMap, Int2ObjectMap<FieldsDescriptor> outputMap)
{
  IncrementalAggregator incrementalAggregator = aggregatorRegistry.getNameToIncrementalAggregator().get(
      aggregatorName);
  //don't need to build OTF aggregate
  if (incrementalAggregator == null) {
    return;
  }
  int aggregatorID = aggregatorRegistry.getIncrementalAggregatorNameToID().get(aggregatorName);
  mergeAggregatorID(aggIDList, aggregatorID);
  inputMap.put(aggregatorID, inputDescriptor);
  outputMap.put(aggregatorID,
      AggregatorUtils.getOutputFieldsDescriptor(inputDescriptor,
      incrementalAggregator));
}
 
Example 8
protected void calculateLayerArrays() {
    Int2ObjectOpenHashMap<char[]> colorLayerMap = new Int2ObjectOpenHashMap<>();
    for (int i = 0; i < validBlockIds.length; i++) {
        int color = validColors[i];
        int combined = validBlockIds[i];
        if (hasAlpha(color)) {
            for (int j = 0; j < validBlockIds.length; j++) {
                int colorOther = validColors[j];
                if (!hasAlpha(colorOther)) {
                    int combinedOther = validBlockIds[j];
                    int combinedColor = combineTransparency(color, colorOther);
                    colorLayerMap.put(combinedColor, new char[]{(char) combined, (char) combinedOther});
                }
            }
        }
    }
    this.validLayerColors = new int[colorLayerMap.size()];
    this.validLayerBlocks = new char[colorLayerMap.size()][];
    int index = 0;
    for (Int2ObjectMap.Entry<char[]> entry : colorLayerMap.int2ObjectEntrySet()) {
        validLayerColors[index] = entry.getIntKey();
        validLayerBlocks[index++] = entry.getValue();
    }
}
 
Example 9
Source Project: incubator-hivemall   Source File: SlimUDTF.java    License: Apache License 2.0 6 votes vote down vote up
private static double predict(final int user, final int itemI,
        @Nonnull final Int2ObjectMap<Int2FloatMap> knnItems, final int excludeIndex,
        @Nonnull final FloatMatrix weightMatrix) {
    final Int2FloatMap kNNu = knnItems.get(user);
    if (kNNu == null) {
        return 0.d;
    }

    double pred = 0.d;
    for (Int2FloatMap.Entry e : Fastutil.fastIterable(kNNu)) {
        final int itemK = e.getIntKey();
        if (itemK == excludeIndex) {
            continue;
        }
        float ruk = e.getFloatValue();
        pred += ruk * weightMatrix.get(itemI, itemK, 0.d);
    }
    return pred;
}
 
Example 10
Source Project: incubator-hivemall   Source File: SlimUDTF.java    License: Apache License 2.0 6 votes vote down vote up
private void replayTrain(@Nonnull final ByteBuffer buf) {
    final int itemI = buf.getInt();
    final int knnSize = buf.getInt();

    final Int2ObjectMap<Int2FloatMap> knnItems = new Int2ObjectOpenHashMap<>(1024);
    final IntSet pairItems = new IntOpenHashSet();
    for (int i = 0; i < knnSize; i++) {
        int user = buf.getInt();
        int ruSize = buf.getInt();
        Int2FloatMap ru = new Int2FloatOpenHashMap(ruSize);
        ru.defaultReturnValue(0.f);

        for (int j = 0; j < ruSize; j++) {
            int itemK = buf.getInt();
            pairItems.add(itemK);
            float ruk = buf.getFloat();
            ru.put(itemK, ruk);
        }
        knnItems.put(user, ru);
    }

    for (int itemJ : pairItems) {
        train(itemI, knnItems, itemJ);
    }
}
 
Example 11
Source Project: incubator-hivemall   Source File: SlimUDTF.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Int2ObjectMap<Int2FloatMap> kNNentries(@Nonnull final Object kNNiObj,
        @Nonnull final MapObjectInspector knnItemsOI,
        @Nonnull final PrimitiveObjectInspector knnItemsKeyOI,
        @Nonnull final MapObjectInspector knnItemsValueOI,
        @Nonnull final PrimitiveObjectInspector knnItemsValueKeyOI,
        @Nonnull final PrimitiveObjectInspector knnItemsValueValueOI,
        @Nullable Int2ObjectMap<Int2FloatMap> knnItems, @Nonnull final MutableInt nnzKNNi) {
    if (knnItems == null) {
        knnItems = new Int2ObjectOpenHashMap<>(1024);
    } else {
        knnItems.clear();
    }

    int numElementOfKNNItems = 0;
    for (Map.Entry<?, ?> entry : knnItemsOI.getMap(kNNiObj).entrySet()) {
        int user = PrimitiveObjectInspectorUtils.getInt(entry.getKey(), knnItemsKeyOI);
        Int2FloatMap ru = int2floatMap(knnItemsValueOI.getMap(entry.getValue()),
            knnItemsValueKeyOI, knnItemsValueValueOI);
        knnItems.put(user, ru);
        numElementOfKNNItems += ru.size();
    }

    nnzKNNi.setValue(numElementOfKNNItems);
    return knnItems;
}
 
Example 12
Source Project: Diorite   Source File: DioriteRandomUtils.java    License: MIT License 6 votes vote down vote up
@Nullable
public static <T> T getWeightedRandomReversed(Random random, Int2ObjectMap<T> choices)
{
    long i = 0;
    IntSet ints = choices.keySet();
    for (IntIterator iterator = ints.iterator(); iterator.hasNext(); )
    {
        int x = iterator.nextInt();
        i += x;
    }
    i = getRandomLong(random, 0, i);
    for (Int2ObjectMap.Entry<T> entry : choices.int2ObjectEntrySet())
    {
        i -= entry.getIntKey();
        if (i < 0)
        {
            return entry.getValue();
        }
    }
    return null;
}
 
Example 13
public Vector get(int index) {
    int count = 0;
    ObjectIterator<Int2ObjectMap.Entry<LocalBlockVectorSet>> iter = localSets.int2ObjectEntrySet().iterator();
    while (iter.hasNext()) {
        Int2ObjectMap.Entry<LocalBlockVectorSet> entry = iter.next();
        LocalBlockVectorSet set = entry.getValue();
        int size = set.size();
        int newSize = count + size;
        if (newSize > index) {
            int localIndex = index - count;
            Vector pos = set.getIndex(localIndex);
            if (pos != null) {
                int pair = entry.getIntKey();
                int cx = MathMan.unpairX(pair);
                int cz = MathMan.unpairY(pair);
                pos.mutX((cx << 11) + pos.getBlockX());
                pos.mutZ((cz << 11) + pos.getBlockZ());
                return pos;
            }
        }
        count += newSize;
    }
    return null;
}
 
Example 14
Source Project: fastjgame   Source File: EnumUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 根据枚举的values建立索引;
 *
 * @param values    枚举数组
 * @param fastQuery 是否追求极致的查询性能
 * @param <T>       枚举类型
 * @return unmodifiable
 */
public static <T extends IndexableEnum> IndexableEnumMapper<T> mapping(final T[] values, final boolean fastQuery) {
    if (values.length == 0) {
        @SuppressWarnings("unchecked") final IndexableEnumMapper<T> mapper = (IndexableEnumMapper<T>) EmptyMapper.INSTANCE;
        return mapper;
    }

    // 结果不一定用得上,存在一定的浪费,但必须检测重复
    final Int2ObjectMap<T> result = new Int2ObjectOpenHashMap<>(values.length);
    for (T t : values) {
        if (result.containsKey(t.getNumber())) {
            throw new IllegalArgumentException(t.getClass().getSimpleName() + " number:" + t.getNumber() + " is duplicate");
        }
        result.put(t.getNumber(), t);
    }

    final int minNumber = Arrays.stream(values)
            .mapToInt(IndexableEnum::getNumber)
            .min()
            .getAsInt();

    final int maxNumber = Arrays.stream(values)
            .mapToInt(IndexableEnum::getNumber)
            .max()
            .getAsInt();

    // 保护性拷贝,避免出现并发问题 - 不确定values()是否会被修改
    final T[] copiedValues = Arrays.copyOf(values, values.length);
    if (isArrayAvailable(minNumber, maxNumber, values.length, fastQuery)) {
        return new ArrayBasedMapper<>(copiedValues, minNumber, maxNumber);
    } else {
        return new MapBasedMapper<>(copiedValues, result);
    }
}
 
Example 15
Source Project: multiconnect   Source File: MixinIdList.java    License: MIT License 5 votes vote down vote up
@Inject(method = "iterator", at = @At("RETURN"), cancellable = true)
private void onIterator(CallbackInfoReturnable<Iterator<T>> ci) {
    ci.setReturnValue(Iterators.concat(ci.getReturnValue(),
            highIdsMap.int2ObjectEntrySet().stream()
                    .sorted(Comparator.comparingInt(Int2ObjectMap.Entry::getIntKey))
                    .map(Int2ObjectMap.Entry::getValue)
                    .iterator()));
}
 
Example 16
Source Project: Kettle   Source File: ArrayMap.java    License: GNU General Public License v3.0 5 votes vote down vote up
Int2ObjectMap.Entry<V> nextEntry() {
    AtomicReferenceArray<V> values = ArrayMap.this.values;
    while (index < values.length()) {
        int key = index++;
        V value = values.get(key);
        if (value != null) {
            return new Entry(key, value);
        }
    }
    throw new NoSuchElementException();
}
 
Example 17
Source Project: Kettle   Source File: FunctionalMaps.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static <V> void forEachPrimitive(Int2ObjectMap<V> map, ObjIntConsumer<V> action) {
    if (map instanceof ArrayMap) {
        ((ArrayMap<V>) checkNotNull(map, "Null map")).forEachPrimitive(action);
    } else {
        defaultForEachPrimitive(map, action);
    }
}
 
Example 18
Source Project: jstarcraft-ai   Source File: SparseTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SparseTable<T> setValues(T value) {
    for (Int2ObjectMap.Entry<T> term : cells.int2ObjectEntrySet()) {
        term.setValue(value);
    }
    return this;
}
 
Example 19
Source Project: metanome-algorithms   Source File: PositionListIndex.java    License: Apache License 2.0 5 votes vote down vote up
public PositionListIndex intersect(PositionListIndex otherPLI) {
	Int2IntOpenHashMap hashedPLI = otherPLI.asHashMap();
	Int2ObjectMap<Int2ObjectMap<IntArrayList>> intersectMap = this.buildIntersectMap(this, hashedPLI);
	
	List<IntArrayList> clusters = new ArrayList<>();
	for (Int2ObjectMap<IntArrayList> cluster1 : intersectMap.values())
		for (IntArrayList cluster2 : cluster1.values())
			if (cluster2.size() > 1)
				clusters.add(cluster2);
	
	return new PositionListIndex(-1, clusters);
}
 
Example 20
Source Project: Neo4jSNA   Source File: LouvainTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
    protected void initGraph() {
        Int2ObjectMap<Node> nodes = new Int2ObjectOpenHashMap<>();

        for (int i = 0; i < 9; i++) {
            Node n = db.createNode();
            n.setProperty("id", i);
            nodes.put(i, n);
        }

        for (int i = 0; i < 9; i++) {
            Node src = nodes.get(i);
            Node dst = (i + 1) % 3 != 0 ? nodes.get(i + 1) : nodes.get(i - 2);

            src.createRelationshipTo(dst, CommonsRelationshipTypes.KNOWS);
//            dst.createRelationshipTo(src, CommonsRelationshipTypes.KNOWS);
        }

        nodes.get(0).createRelationshipTo(nodes.get(3), CommonsRelationshipTypes.KNOWS);
        nodes.get(3).createRelationshipTo(nodes.get(6), CommonsRelationshipTypes.KNOWS);
        nodes.get(6).createRelationshipTo(nodes.get(0), CommonsRelationshipTypes.KNOWS);

//        for (int i = 0; i < 9; i += 3) {
//            Node src = nodes.get(i);
//            Node dst1 = nodes.get((i + 3) % 9);
//            Node dst2 = nodes.get((i + 6) % 9);
//            src.createRelationshipTo(dst1, CommonsRelationshipTypes.KNOWS);
// //            dst1.createRelationshipTo(src, CommonsRelationshipTypes.KNOWS);
//            src.createRelationshipTo(dst2, CommonsRelationshipTypes.KNOWS);
// //            dst2.createRelationshipTo(src, CommonsRelationshipTypes.KNOWS);
//        }

    }
 
Example 21
Source Project: metanome-algorithms   Source File: RecordInflater.java    License: Apache License 2.0 5 votes vote down vote up
Object[] inflate(int[] record) {
	Object[] inflated = new Object[record.length];
	for (int i = 0; i < record.length; i++) {
		int id = record[i];
		Int2ObjectMap<?> dictionary = dictionaries.get(i);
		inflated[i] = dictionary.get(id);
	}
	return inflated;
}
 
Example 22
@Test
public void test() {
	Double2ObjectSortedMap<String> row1 = new Double2ObjectRBTreeMap<>();
	row1.put(0.5, "foo");
	row1.put(0.6, "bar");
	Int2ObjectMap<Double2ObjectSortedMap<String>> map = Int2ObjectMaps.singleton(1, row1);
	Int2Double2ObjectSortedTable<String> table = create(map, 3);
	assertThat(table.getCeilingValue(1, 0.5)).hasValue("foo");
	assertThat(table.getCeilingValue(1, 0.55)).hasValue("bar");
	assertThat(table.getCeilingValue(1, 0.7)).isEmpty();
	assertThat(table.getCeilingValue(2, 0.5)).isEmpty();
}
 
Example 23
Source Project: metanome-algorithms   Source File: CollectionUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void forEachInt2ObjectMap() {
	Int2ObjectMap<String> map = new Int2ObjectOpenHashMap<>();
	Int2ObjectMap<String> target = new Int2ObjectOpenHashMap<>();
	map.put(1, "foo");
	map.put(2, "bar");
	forEach(map, target::put);
	assertThat(target).hasSize(2);
	assertThat(target).containsEntry(1, "foo");
	assertThat(target).containsEntry(2, "bar");
}
 
Example 24
@SuppressWarnings("unchecked")
private static <T> Double2ObjectSortedMap<T>[] toArray(
	Int2ObjectMap<Double2ObjectSortedMap<T>> map, int size) {
	Double2ObjectSortedMap<T>[] array = new Double2ObjectSortedMap[size];
	for (Entry<Double2ObjectSortedMap<T>> entry : map.int2ObjectEntrySet()) {
		int columnKey = entry.getIntKey();
		array[columnKey] = entry.getValue();
	}
	return array;
}
 
Example 25
public CustomTimeBucketRegistry(Int2ObjectMap<CustomTimeBucket> idToTimeBucket, int startingId)
{
  int tempId = initialize(idToTimeBucket);

  Preconditions.checkArgument(tempId < startingId, "The statingId " + startingId
      + " must be larger than the largest ID " + tempId + " in the given idToTimeBucket mapping");

  this.idToTimeBucket = Preconditions.checkNotNull(idToTimeBucket);
  this.currentId = startingId;
}
 
Example 26
Source Project: metanome-algorithms   Source File: AgreeSetGenerator.java    License: Apache License 2.0 5 votes vote down vote up
public Int2ObjectMap<TupleEquivalenceClassRelation> calculateRelationships(List<StrippedPartition> partitions) {

        if (this.debugSysout) {
            System.out.println("\tstartet calculation of relationships");
        }
        Int2ObjectMap<TupleEquivalenceClassRelation> relationships = new Int2ObjectOpenHashMap<TupleEquivalenceClassRelation>();
        for (StrippedPartition p : partitions) {
            this.calculateRelationship(p, relationships);
        }

        return relationships;
    }
 
Example 27
Source Project: metanome-algorithms   Source File: AgreeSetGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private Set<IntList> mergeResult(Int2ObjectMap<Set<IntList>> maxSets) {

        Set<IntList> max = new HashSet<IntList>();
        for (Set<IntList> set : maxSets.values()) {
            max.addAll(set);
        }
        return max;
    }
 
Example 28
Source Project: metanome-algorithms   Source File: AgreeSetGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private void handlePartition(IntList actuelList, int position, Int2ObjectMap<IntSet> index, Set<IntList> max) {

        if (!this.isSubset(actuelList, index)) {
            max.add(actuelList);
            for (int e : actuelList) {
                if (!index.containsKey(e)) {
                    index.put(e, new IntArraySet());
                }
                index.get(e).add(position);
            }
        }
    }
 
Example 29
Source Project: metanome-algorithms   Source File: AgreeSetGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private IntersectWithAndAddToAgreeSetTask(int i, int j, IntList maxEquiClass,
                                          Int2ObjectMap<TupleEquivalenceClassRelation> relationships, Map<AgreeSet, Object> agreeSets) {

    this.i = i;
    this.j = j;
    this.maxEquiClass = maxEquiClass;
    this.relationships = relationships;
    this.agreeSets = agreeSets;
}
 
Example 30
Source Project: metanome-algorithms   Source File: PositionListIndex.java    License: Apache License 2.0 5 votes vote down vote up
public PositionListIndex intersect(int[] otherPLI) {
	Int2ObjectMap<Int2ObjectMap<IntArrayList>> intersectMap = this.buildIntersectMap(otherPLI);
	
	List<IntArrayList> clusters = new ArrayList<>();
	for (Int2ObjectMap<IntArrayList> cluster1 : intersectMap.values())
		for (IntArrayList cluster2 : cluster1.values())
			if (cluster2.size() > 1)
				clusters.add(cluster2);
	
	return new PositionListIndex(-1, clusters);
}