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

The following examples show how to use it.unimi.dsi.fastutil.ints.Int2IntMap. 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: fasten   Source File: LayeredLabelPropagation.java    License: Apache License 2.0 6 votes vote down vote up
public Iterator<Int2IntMap.Entry> entries() {
	return new ObjectIterator<>() {
		private int i;

		private final Entry entry = new Entry();

		@Override
		public boolean hasNext() {
			return i < n;
		}

		@Override
		public Entry next() {
			if (!hasNext()) throw new NoSuchElementException();
			final int l = location[i++];
			entry.setKey(key[l]);
			entry.setValue(count[l]);
			return entry;
		}
	};
}
 
Example 2
Source Project: RankSys   Source File: SetSimilarity.java    License: Mozilla Public License 2.0 6 votes vote down vote up
private Int2IntMap getFasterIntersectionMap(int uidx) {
    Int2IntOpenHashMap intersectionMap = new Int2IntOpenHashMap();
    intersectionMap.defaultReturnValue(0);

    IntIterator iidxs = data.getUidxIidxs(uidx);
    while (iidxs.hasNext()) {
        IntIterator vidxs = data.getIidxUidxs(iidxs.nextInt());
        while (vidxs.hasNext()) {
            intersectionMap.addTo(vidxs.nextInt(), 1);
        }
    }

    intersectionMap.remove(uidx);

    return intersectionMap;
}
 
Example 3
Source Project: WorldGrower   Source File: AbstractIdMap.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public final int findBestId(Predicate<WorldObject> predicate, World world) {
	int bestId = -1;
	int bestRelationshipValue = Integer.MIN_VALUE;
	for(Int2IntMap.Entry entry : idsToValue.int2IntEntrySet()) {
		int id = entry.getIntKey();
		int relationshipValue = entry.getIntValue();
		
		// id may not exist in world because it's filtered out by
		// WorldFacade, for example being invisible
		if (world.exists(id)) {
			WorldObject person = world.findWorldObjectById(id);
			
			if (relationshipValue > bestRelationshipValue && predicate.test(person)) {
				bestRelationshipValue = relationshipValue;
				bestId = id;
			}
		}
	}
	
	return bestId;
}
 
Example 4
Source Project: WorldGrower   Source File: AbstractIdMap.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public final int findWorstId(World world) {
	int worstId = -1;
	int worstRelationshipValue = Integer.MAX_VALUE;
	for(Int2IntMap.Entry entry : idsToValue.int2IntEntrySet()) {
		int id = entry.getIntKey();
		int relationshipValue = entry.getIntValue();
		
		if (relationshipValue < worstRelationshipValue) {
			worstRelationshipValue = relationshipValue;
			worstId = id;
		}
	}
	
	return worstId;
}
 
Example 5
Source Project: WorldGrower   Source File: AbstractIdMap.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public final int findBestId(Predicate<WorldObject> predicate, Comparator<WorldObject> comparator,  World world) {
	WorldObject bestPerson = null;
	for(Int2IntMap.Entry entry : idsToValue.int2IntEntrySet()) {
		int id = entry.getIntKey();
		// id may not exist in world because it's filtered out by
		// WorldFacade, for example being invisible
		if (world.exists(id)) {
			WorldObject person = world.findWorldObjectById(id);
			
			if (predicate.test(person)) {
				if (bestPerson == null || comparator.compare(bestPerson, person) < 0) {
					bestPerson = person;
				}
			}
		}
	}
	
	if (bestPerson != null) {
		return bestPerson.getProperty(Constants.ID);
	} else {
		return -1;
	}
}
 
Example 6
@Nonnull
@Override
public Iterator<GroupKey> iterator() {
  return new Iterator<GroupKey>() {
    private final ObjectIterator<Int2IntMap.Entry> _iterator = _rawKeyToGroupIdMap.int2IntEntrySet().fastIterator();
    private final GroupKey _groupKey = new GroupKey();

    @Override
    public boolean hasNext() {
      return _iterator.hasNext();
    }

    @Override
    public GroupKey next() {
      Int2IntMap.Entry entry = _iterator.next();
      _groupKey._groupId = entry.getIntValue();
      _groupKey._stringKey = getGroupKey(entry.getIntKey());
      return _groupKey;
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
  };
}
 
Example 7
/** {@inheritDoc} */
@Override
public IAST factorSmallPrimes(int numerator, int root) {
	// SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
	Int2IntMap map = new Int2IntRBTreeMap();
	IInteger b = this;
	boolean isNegative = false;
	if (sign() < 0) {
		b = b.negate();
		isNegative = true;
	}
	if (numerator != 1) {
		b = b.pow(numerator);
	}
	if (b.isLT(F.C8)) {
		return F.NIL;
	}

	BigInteger number = b.toBigNumerator();
	return factorBigInteger(number, isNegative, numerator, root, map);
}
 
Example 8
Source Project: fastjgame   Source File: JsonUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Int2IntMap data = new Int2IntOpenHashMap();
    data.put(1, 5);
    data.put(6, 7);

    String json = writeAsJson(data);
    System.out.println("json = " + json);

    Int2IntMap rData = readMapFromJson(json, Int2IntOpenHashMap.class, Integer.class, Integer.class);
    System.out.println("map = " + rData);

    System.out.println("equals = " + data.equals(rData));
}
 
Example 9
Source Project: jstarcraft-ai   Source File: HashInstance.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HashInstance iterateQualityFeatures(QualityAccessor accessor) {
    for (Int2IntMap.Entry term : this.qualityFeatures.int2IntEntrySet()) {
        accessor.accessorFeature(term.getIntKey(), term.getIntValue());
    }
    return this;
}
 
Example 10
public boolean union(CustomHashMap other){
boolean modified = false;
for(Int2IntMap.Entry otherEntry : other.int2IntEntrySet()){
    if(!this.containsKey(otherEntry.getIntKey())){
	this.put(otherEntry.getIntKey(), otherEntry.getIntValue());
	modified = true;
    }
}
return modified;
   }
 
Example 11
Source Project: tagme   Source File: RedirectMap.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Int2IntMap parseSet() throws IOException
{
	final Object2IntMap<String> titles = new TitlesToWIDMap(lang).getDataset();
	final Int2IntOpenHashMap map = new Int2IntOpenHashMap(3000000);
	SQLWikiParser parser = new SQLWikiParser(log, "Titles NF") {
		@Override
		public boolean compute(ArrayList<String> values) throws IOException
		{
			int ns = Integer.parseInt(values.get(SQLWikiParser.REDIRECT_NS));
			if (ns == SQLWikiParser.NS_ARTICLE)
			{
				int idFrom = Integer.parseInt(values.get(SQLWikiParser.REDIRECT_ID_FROM));
				int idTo = titles.getInt(cleanPageName(values.get(SQLWikiParser.REDIRECT_TITLE_TO)));
				if (idTo >= 0)
					map.put(idFrom, idTo);
				else this.updateItem(0);
				
				return true;
			} else return false;
		}
	};

	File input = WikipediaFiles.REDIRECTS.getSourceFile(lang);
	InputStreamReader in = new InputStreamReader(new FileInputStream(input), Charset.forName("UTF-8"));
	parser.compute(in);
	in.close();
	
	map.defaultReturnValue(-1);
	map.trim();
	
	return map;

}
 
Example 12
Source Project: RankSys   Source File: SetSimilarity.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private Int2IntMap getIntersectionMap(int idx1) {
    Int2IntOpenHashMap intersectionMap = new Int2IntOpenHashMap();
    intersectionMap.defaultReturnValue(0);

    data.getUidxPreferences(idx1)
            .forEach(ip -> data.getIidxPreferences(ip.v1)
                    .forEach(up -> intersectionMap.addTo(up.v1, 1)));

    intersectionMap.remove(idx1);

    return intersectionMap;
}
 
Example 13
Source Project: WorldGrower   Source File: AbstractIdMap.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public final int getSumOfAllValues() {
	int sumOfAllValues = 0;
	for(Int2IntMap.Entry entry : idsToValue.int2IntEntrySet()) {
		sumOfAllValues += entry.getIntValue();
	}
	return sumOfAllValues;
}
 
Example 14
/**
 * Helper method to create the group-key map, depending on the data type.
 * Uses primitive maps when possible.
 *
 * @param keyType DataType for the key
 * @return Map
 */
private Map createGroupKeyMap(FieldSpec.DataType keyType) {
  Map map;
  switch (keyType) {
    case INT:
      Int2IntMap intMap = new Int2IntOpenHashMap();
      intMap.defaultReturnValue(INVALID_ID);
      map = intMap;
      break;

    case LONG:
      Long2IntOpenHashMap longMap = new Long2IntOpenHashMap();
      longMap.defaultReturnValue(INVALID_ID);
      map = longMap;
      break;

    case FLOAT:
      Float2IntOpenHashMap floatMap = new Float2IntOpenHashMap();
      floatMap.defaultReturnValue(INVALID_ID);
      map = floatMap;
      break;

    case DOUBLE:
      Double2IntOpenHashMap doubleMap = new Double2IntOpenHashMap();
      doubleMap.defaultReturnValue(INVALID_ID);
      map = doubleMap;
      break;

    case STRING:
      Object2IntOpenHashMap<String> stringMap = new Object2IntOpenHashMap<>();
      stringMap.defaultReturnValue(INVALID_ID);
      map = stringMap;
      break;

    default:
      throw new IllegalArgumentException("Illegal data type for no-dictionary key generator: " + keyType);
  }
  return map;
}
 
Example 15
@SuppressWarnings("unchecked")
private int getKeyForValue(int value) {
  Int2IntMap map = (Int2IntMap) _groupKeyMap;
  int groupId = map.get(value);
  if (groupId == INVALID_ID) {
    if (_numGroups < _globalGroupIdUpperBound) {
      groupId = _numGroups;
      map.put(value, _numGroups++);
    }
  }
  return groupId;
}
 
Example 16
@Override
public IAST factorSmallPrimes(int numerator, int root) {
	BigInteger b = toBigNumerator();
	boolean isNegative = false;
	if (sign() < 0) {
		b = b.negate();
		isNegative = true;
	}
	if (numerator != 1) {
		b = b.pow(numerator);
	}
	BigInteger d = toBigDenominator();
	if (numerator != 1) {
		d = d.pow(numerator);
	}
	// SortedMap<Integer, Integer> bMap = new TreeMap<Integer, Integer>();
	Int2IntMap bMap = new Int2IntRBTreeMap();
	IAST bAST = AbstractIntegerSym.factorBigInteger(b, isNegative, numerator, root, bMap);
	// SortedMap<Integer, Integer> dMap = new TreeMap<Integer, Integer>();
	Int2IntMap dMap = new Int2IntRBTreeMap();
	IAST dAST = AbstractIntegerSym.factorBigInteger(d, false, numerator, root, dMap);
	if (bAST.isPresent()) {
		if (dAST.isPresent()) {
			return F.Times(bAST, F.Power(dAST, F.CN1));
		}
		return F.Times(bAST, F.Power(denominator(), F.QQ(-numerator, root)));
	} else if (dAST.isPresent()) {
		return F.Times(F.Power(numerator(), F.QQ(numerator, root)), F.Power(dAST, F.CN1));
	}
	return F.NIL;
}
 
Example 17
protected static IAST factorBigInteger(BigInteger number, boolean isNegative, int numerator, int denominator,
		Int2IntMap map) {
	if (number.compareTo(BigInteger.valueOf(7)) <= 0) {
		return F.NIL;
	}
	BigInteger rest = Primality.countPrimes32749(number, map);
	if (map.size() == 0) {
		return F.NIL;
	}
	IASTAppendable result = F.TimesAlloc(map.size() + 4);
	boolean evaled = false;
	for (Int2IntMap.Entry entry : map.int2IntEntrySet()) {
		int key = entry.getIntKey();
		int value = entry.getIntValue();
		int mod = value % denominator;
		int div = value / denominator;
		if (div != 0) {
			result.append(F.Power(valueOf(key), F.ZZ(div)));
			if (mod != 0) {
				result.append(F.Power(valueOf(key), F.QQ(mod, denominator)));
			}
			evaled = true;
		} else {
			result.append(F.Power(F.Power(valueOf(key), valueOf(value)), F.QQ(1, denominator)));
		}
	}
	if (evaled) {
		if (!rest.equals(BigInteger.ONE)) {
			result.append(F.Power(valueOf(rest), F.QQ(1, denominator)));
		}
		if (isNegative) {
			result.append(F.Power(F.CN1, F.QQ(numerator, denominator)));
		}
		return result;
	}
	return F.NIL;
}
 
Example 18
Source Project: metanome-algorithms   Source File: FindCoversGenerator.java    License: Apache License 2.0 4 votes vote down vote up
private IntList generateInitialOrdering(List<DifferenceSet> tempDiffSet) {

        IntList result = new IntArrayList();

        Int2IntMap counting = new Int2IntArrayMap();
        for (DifferenceSet ds : tempDiffSet) {

            int lastIndex = ds.getAttributes().nextSetBit(0);

            while (lastIndex != -1) {
                if (!counting.containsKey(lastIndex)) {
                    counting.put(lastIndex, 1);
                } else {
                    counting.put(lastIndex, counting.get(lastIndex) + 1);
                }
                lastIndex = ds.getAttributes().nextSetBit(lastIndex + 1);
            }
        }

        // TODO: Comperator und TreeMap --> Tommy
        while (true) {

            if (counting.size() == 0) {
                break;
            }

            int biggestAttribute = -1;
            int numberOfOcc = 0;
            for (int attr : counting.keySet()) {

                if (biggestAttribute < 0) {
                    biggestAttribute = attr;
                    numberOfOcc = counting.get(attr);
                    continue;
                }

                int tempOcc = counting.get(attr);
                if (tempOcc > numberOfOcc) {
                    numberOfOcc = tempOcc;
                    biggestAttribute = attr;
                } else if (tempOcc == numberOfOcc) {
                    if (biggestAttribute > attr) {
                        biggestAttribute = attr;
                    }
                }
            }

            if (numberOfOcc == 0) {
                break;
            }

            result.add(biggestAttribute);
            counting.remove(biggestAttribute);
        }

        return result;
    }
 
Example 19
Source Project: metanome-algorithms   Source File: FindCoversGenerator.java    License: Apache License 2.0 4 votes vote down vote up
private IntList generateNextOrdering(List<DifferenceSet> next, IntList currentOrdering, int attribute) {

        IntList result = new IntArrayList();

        Int2IntMap counting = new Int2IntArrayMap();
        boolean seen = false;
        for (int i = 0; i < currentOrdering.size(); i++) {

            if (!seen) {
                if (currentOrdering.getInt(i) == attribute) {
                    seen = true;
                }
            } else {

                counting.put(currentOrdering.getInt(i), 0);
                for (DifferenceSet ds : next) {

                    if (ds.getAttributes().get(currentOrdering.getInt(i))) {
                        counting.put(currentOrdering.getInt(i), counting.get(currentOrdering.getInt(i)) + 1);
                    }
                }
            }
        }

        // TODO: Comperator und TreeMap --> Tommy
        while (true) {

            if (counting.size() == 0) {
                break;
            }

            int biggestAttribute = -1;
            int numberOfOcc = 0;
            for (int attr : counting.keySet()) {

                if (biggestAttribute < 0) {
                    biggestAttribute = attr;
                    numberOfOcc = counting.get(attr);
                    continue;
                }

                int tempOcc = counting.get(attr);
                if (tempOcc > numberOfOcc) {
                    numberOfOcc = tempOcc;
                    biggestAttribute = attr;
                } else if (tempOcc == numberOfOcc) {
                    if (biggestAttribute > attr) {
                        biggestAttribute = attr;
                    }
                }
            }

            if (numberOfOcc == 0) {
                break;
            }

            result.add(biggestAttribute);
            counting.remove(biggestAttribute);
        }

        return result;
    }
 
Example 20
public CustomHashMap(Int2IntMap m){
super(m);
   }
 
Example 21
public CustomHashMap(Int2IntMap m, float f){
super(m, f);
   }
 
Example 22
Source Project: tagme   Source File: AnchorTernaryTrie.java    License: Apache License 2.0 4 votes vote down vote up
public static Anchor fake(String w){
	Int2IntMap links = new Int2IntArrayMap();
	links.put(0, w.length());
	return Anchor.build(0, links, w.length(), new IntArraySet());
}
 
Example 23
Source Project: tagme   Source File: WikipediaEdges.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void parseFile(File file) throws IOException
{

	final Int2IntMap redirects = DatasetLoader.get(new RedirectMap(lang));
	final IntSet disambiguations = DatasetLoader.get(new DisambiguationWIDs(lang));
	final IntSet listpages = DatasetLoader.get(new ListPageWIDs(lang));
	final IntSet ignores = DatasetLoader.get(new IgnoreWIDs(lang));
	final IntSet valids = new AllWIDs(lang).getDataset();//DatasetLoader.get(new AllWIDs(lang));
	valids.removeAll(redirects.keySet());
	//valids.removeAll(disambiguations);
	//valids.removeAll(listpages);
	valids.removeAll(ignores);
	final Object2IntMap<String> titles = DatasetLoader.get(new TitlesToWIDMap(lang));


	File tmp = Dataset.createTmpFile();
	final BufferedWriter out = new BufferedWriter(new FileWriter(tmp));
	SQLWikiParser parser = new 	SQLWikiParser(log) {
		@Override
		public boolean compute(ArrayList<String> values) throws IOException
		{
			int idFrom = Integer.parseInt(values.get(SQLWikiParser.PAGELINKS_ID_FROM));
			if (redirects.containsKey(idFrom)) idFrom = redirects.get(idFrom);
			
			int ns = Integer.parseInt(values.get(SQLWikiParser.PAGELINKS_NS));

			
			if (ns == SQLWikiParser.NS_ARTICLE && !redirects.containsKey(idFrom) && !ignores.contains(idFrom) &&
					//questo e' necessario perchè alcune pagine che sono delle liste, in inglese finiscono
					//tra le pagine di disambiguazione (per via della categoria All_set_index_articles)
					(listpages.contains(idFrom) || !disambiguations.contains(idFrom))
					//!listpages.contains(idFrom) && !disambiguations.contains(idFrom)
					&& valids.contains(idFrom)
			
			/**/ )
			{

				String titleTo = Dataset.cleanPageName(values.get(SQLWikiParser.PAGELINKS_TITLE_TO));

				int idTo = titles.getInt(titleTo);
				
				if (redirects.containsKey(idTo)) idTo = redirects.get(idTo);
				if (idTo >= 0 && !ignores.contains(idTo) && (listpages.contains(idFrom) || !disambiguations.contains(idFrom)) && valids.contains(idTo))
				{
					out.append(Integer.toString(idFrom));
					out.append(SEP_CHAR);
					out.append(Integer.toString(idTo));
					out.append('\n');
					return true;
				}
			}
			return false;
		}
	};

	File input = WikipediaFiles.PAGE_LINKS.getSourceFile(lang);
	parser.compute(input);
	out.close();

	log.info("Now sorting edges...");

	ExternalSort sorter = new ExternalSort();
	sorter.setUniq(true);
	sorter.setNumeric(true);
	sorter.setColumns(new int[]{0,1});
	sorter.setInFile(tmp.getAbsolutePath());
	sorter.setOutFile(file.getAbsolutePath());
	sorter.run();

	tmp.delete();

	log.info("Sorted. Done.");

}
 
Example 24
/**
 * Note that some readers might still access the oldest segment for a while -- we do NOT interrupt
 * readers accessing the old segment. The expected behavior is that after a while though, none of
 * them will reference it anymore (once their computation finishes) and the JVM can then
 * garbage-collect it. This lagging reading behavior is what makes it hard to explicitly recycle
 * the already allocated memory so we need memory for k+1 segments (where k is the number of
 * segments we'll maintain).
 *
 * @param numEdgesInLiveSegment          is the number of edges in the current live segment
 * @param numEdgesInNonLiveSegmentsMap   contains a map from segment id to number of edges in it
 * @param statsReceiver                  is where the stats are updated
 * @param bipartiteGraphSegmentProvider  provides the new segment to be added
 * @return the live segment that was added
 */
public T addNewSegment(
    int numEdgesInLiveSegment,
    Int2IntMap numEdgesInNonLiveSegmentsMap,
    StatsReceiver statsReceiver,
    BipartiteGraphSegmentProvider<T> bipartiteGraphSegmentProvider
) {
  final Int2ObjectMap<T> segments =
      new Int2ObjectOpenHashMap<T>(multiSegmentReaderAccessibleInfo.getSegments());
  numEdgesInNonLiveSegmentsMap.put(liveSegmentId, numEdgesInLiveSegment);
  int oldestSegmentId = multiSegmentReaderAccessibleInfo.oldestSegmentId;
  // remove a segment if we're at the limit
  if (multiSegmentReaderAccessibleInfo.getSegments().size() == maxNumSegments) {
    segments.remove(oldestSegmentId);
    numEdgesInNonLiveSegmentsMap.remove(oldestSegmentId);
    LOG.info("Removed segment " + oldestSegmentId);
    oldestSegmentId++;
  } else {
    statsReceiver.counter("numSegments").incr();
  }
  int newLiveSegmentId =  multiSegmentReaderAccessibleInfo.liveSegmentId + 1;
  // add a new segment
  T liveSegment =
      bipartiteGraphSegmentProvider.generateNewSegment(newLiveSegmentId, maxNumEdgesPerSegment);
  segments.put(newLiveSegmentId, liveSegment);
  // now make the switch for the readers -- this is immediately published and visible!
  multiSegmentReaderAccessibleInfo = new MultiSegmentReaderAccessibleInfo<T>(
          segments, oldestSegmentId, newLiveSegmentId);

  // flush the write
  liveSegmentId = newLiveSegmentId;

  numEdgesInNonLiveSegments = 0;
  for (int segmentEdgeCount : numEdgesInNonLiveSegmentsMap.values()) {
    numEdgesInNonLiveSegments += segmentEdgeCount;
  }
  LOG.info("Total number of edges in graph = " + numEdgesInNonLiveSegments);
  LOG.info("Created a new segment: oldestSegmentId = " + oldestSegmentId
      + ", and liveSegmentId = " + liveSegmentId);

  return liveSegment;
}
 
Example 25
Source Project: datafu   Source File: PageRankImpl.java    License: Apache License 2.0 4 votes vote down vote up
public Int2IntMap.FastEntrySet getNodeIds()
{
  return this.nodeIndices.int2IntEntrySet();
}