public void writeSparseTo(DataOutputStream output) throws IOException { try { lock.readLock().lock(); super.writeTo(output); output.writeUTF(RowType.T_INT_SPARSE.toString()); output.writeInt(nnz); IntSet keys = sparseRep.keySet(); output.writeInt(keys.size()); for (int key : keys) { output.writeInt(key); output.writeInt(sparseRep.get(key)); } } finally { lock.readLock().unlock(); } }
private DebugLocalsChange createLocalsChange( Int2ReferenceMap<DebugLocalInfo> ending, Int2ReferenceMap<DebugLocalInfo> starting) { if (ending.isEmpty() && starting.isEmpty()) { return null; } if (ending.isEmpty() || starting.isEmpty()) { return new DebugLocalsChange(ending, starting); } IntSet unneeded = new IntArraySet(Math.min(ending.size(), starting.size())); for (Entry<DebugLocalInfo> entry : ending.int2ReferenceEntrySet()) { if (starting.get(entry.getIntKey()) == entry.getValue()) { unneeded.add(entry.getIntKey()); } } if (unneeded.size() == ending.size() && unneeded.size() == starting.size()) { return null; } IntIterator iterator = unneeded.iterator(); while (iterator.hasNext()) { int key = iterator.nextInt(); ending.remove(key); starting.remove(key); } return new DebugLocalsChange(ending, starting); }
@Override public int hashCode() { final int prime = 31; int result = 1; List<IntOpenHashSet> setCluster = this.convertClustersToSets(this.clusters); Collections.sort(setCluster, new Comparator<IntSet>() { @Override public int compare(IntSet o1, IntSet o2) { return o1.hashCode() - o2.hashCode(); } }); result = prime * result + (setCluster.hashCode()); return result; }
private Map<String, IntSet> groupAttributesByValue(final IntSet attributes) throws AlgorithmExecutionException { final Map<String, IntSet> attributesByValue = new HashMap<>(); for (final int attribute : attributes) { final Collection<String> values = getValues(attribute); if (configuration.isProcessEmptyColumns() && values.isEmpty()) { handleEmptyAttribute(attribute, attributes); } else { for (final String value : values) { attributesByValue.computeIfAbsent(value, k -> new IntOpenHashSet()).add(attribute); } } } return attributesByValue; }
public double greedySolve(Mat mat) { double sum = 0.0; IntSet available = new IntRBTreeSet(); for(int i = 0; i < mat.M(); i++) { available.add(i); } for(int i = 0; i < mat.N(); i++) { double best = Double.NEGATIVE_INFINITY; for(ME me : mat.row(i)) { if(available.contains(me.j) && me.v > best) { best = me.v; available.remove(me.j); } } sum += Math.max(best, 0.0); } return sum; }
public DepTree(TypedDependency root, Collection<TypedDependency> tds, List<? extends HasWord> sentence, int[] remapping, IntSet stack) { this.map = new HashMap<>(); int t = root.dep().index(); node = sentence.get(t - 1).word(); //tag = root.dep().tag(); tag = root.dep().label().tag(); this.idx = remapping[t - 1]; if (!stack.contains(t)) { IntSet stack2 = new IntRBTreeSet(stack); stack2.add(t); for (TypedDependency td : tds) { if (td.gov().index() == t && td.dep().index() != t) { map.put(td.reln().getShortName(), new DepTree(td, tds, sentence, remapping, stack2)); } } } }
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); } }
@Override public int hashCode() { final int prime = 31; int result = 1; List<IntOpenHashSet> setCluster = convertClustersToSets(clusters); Collections.sort(setCluster, new Comparator<IntSet>() { @Override public int compare(IntSet o1, IntSet o2) { return o1.hashCode() - o2.hashCode(); } }); result = prime * result + (setCluster.hashCode()); return result; }
public Attribute createNewAttribute(HashMap<String, Attribute> attributes, String attribute, int noMonths) { Attribute a = attributes.get(attribute); int ct = 0; IntSet keys = a.data.keySet(); IntIterator it = keys.iterator(); ArrayList<SpatioTemporalVal> arr = new ArrayList<SpatioTemporalVal>(); while(ct < noMonths) { if(!it.hasNext()) { Utilities.er("no. of months is greater than what is present"); } int month = it.nextInt(); arr.addAll(a.data.get(month)); ct++; } Collections.sort(arr); Attribute na = new Attribute(); na.data.put(0, arr); na.nodeSet = a.nodeSet; return na; }
public static KeyTestInfo generateRandomKeys(Random random, int maxNumKeys) { int maxKeyOrValue = maxNumKeys << 2; int[] keysAndValues = new int[maxNumKeys * 3]; int[] nonKeys = new int[maxNumKeys]; IntSet keySet = new IntOpenHashBigSet(maxNumKeys); for (int i = 0; i < maxNumKeys; i++) { int entry; do { entry = random.nextInt(maxKeyOrValue); } while (keySet.contains(entry)); keysAndValues[i * 3] = entry; keysAndValues[i * 3 + 1] = random.nextInt(maxKeyOrValue); keysAndValues[i * 3 + 2] = random.nextInt(maxKeyOrValue); keySet.add(entry); } for (int i = 0; i < maxNumKeys; i++) { int nonKey; do { nonKey = random.nextInt(maxKeyOrValue); } while (keySet.contains(nonKey)); nonKeys[i] = nonKey; } return new KeyTestInfo(keysAndValues, nonKeys); }
public IntMapGraph(Int2ObjectMap<IntSet> map) { this.map = map; if (map.defaultReturnValue() == null || !map.defaultReturnValue().equals(IntSets.EMPTY_SET)) { LOGGER.warn("It is necessary to set default return value of the map as the empty set."); map.defaultReturnValue(IntSets.EMPTY_SET); } int maxNodeIndex = 0, numArcs = 0; for (Entry<IntSet> x : map.int2ObjectEntrySet()) { if (x.getIntKey() > maxNodeIndex) maxNodeIndex = x.getIntKey(); for (int succ : x.getValue()) { if (succ > maxNodeIndex) maxNodeIndex = succ; numArcs++; } } this.numArcs = numArcs; this.numNodes = maxNodeIndex+1; }
public LatentMatrixEstimator(ImmutableGraph graph, Int2ObjectMap<IntSet> node2cat, String output, Matrix initialMatrix) { rnd = RandomSingleton.get(); pl = new ProgressLogger(LOGGER, "node couples"); this.graph = graph; this.node2cat = node2cat; if (graph.numNodes() != node2cat.size()) { LOGGER.warn("node2cat file and graph file have a different number of nodes: " + "respectively, " + node2cat.size() + " and " + graph.numNodes()); } numNodes = graph.numNodes(); classifier = new PAClassifier(initialMatrix); this.output = output; nArcsLearned = 0; }
protected void learnNode(int node, IntSet successors ) throws IOException { for (int successor : successors) { classifier.learn(node2cat.get(node), node2cat.get(successor), true); anArcHasBeenLearned(); pl.lightUpdate(); } int nonSucc; for (int i = successors.size()-1; i >= 0; i--) { do { nonSucc = rnd.nextInt(numNodes); } while (successors.contains(nonSucc)); classifier.learn(node2cat.get(node), node2cat.get(nonSucc), false); anArcHasBeenLearned(); pl.lightUpdate(); } }
public int[] createTestingSet(int numOfSamples) { numOfSamples = Math.min(numOfSamples, numNodes); if (verbose) LOGGER.info("Creating test set with "+numOfSamples+" nodes..."); if (numOfSamples >= (numNodes/2)) { final Random rnd = RandomSingleton.get(); int[] samples = MathArrays.natural(numNodes); IntArrays.shuffle(samples, rnd); return IntArrays.trim(samples, numOfSamples); } else { IntSet set = new IntOpenHashSet(); while (set.size() < numOfSamples) { set.add(rnd.nextInt(numNodes)); } int[] r = set.toIntArray(); return r; } }
private IntSet parseCategories(Document wikiPage) throws IOException { String categoryString = IOUtils.toString((Reader) wikiPage.content(CATEGORY_FIELD)); IntSet categoryIds = new IntOpenHashSet(); int pipeIndex; for (String category : categoryString.split(SEPARATOR_REGEX)) { if ((pipeIndex = category.indexOf('|')) > -1) category = category.substring(0, pipeIndex); category = StringUtils.strip(category); if (category.length() > 0) categoryIds.add(getCategoryId(category)); } return categoryIds; }
@Override public double computeForQuery(int query, UnexpectednessScorer retriever) { IntSet evaluatedDocs = groundtruth.getEvaluatedDocs(query); double R = groundtruth.getRelevants(query).size(); double N = evaluatedDocs.size() - R; double minNR = Math.min(N, R); int[] retrieved = retriever.results(query); double bpref = 0, nonRelevantRankedFirst = 0; for (int doc : retrieved) if (evaluatedDocs.contains(doc)) { if (groundtruth.isRelevant(query, doc)) { bpref += 1.0 - (nonRelevantRankedFirst / minNR); } else { if (nonRelevantRankedFirst < R) nonRelevantRankedFirst ++; } } bpref /= R; printResult(this + "\t" + retriever + "\t" + query(query) + "\t" + bpref); return bpref; }
public void removeIfNotOneRelevantAndOneIrrelevant() { IntSet queriesToRemove = new IntOpenHashSet(); for (int query : queries()) { int nEval = query2evaluateddocs.get(query).size(); int nRelev = query2relevantdocs.get(query).size(); int nIrrelev = nEval - nRelev; if (nRelev < 1 || nIrrelev < 1) queriesToRemove.add(query); } int originalNQueries = queries().size(); for (int q : queriesToRemove) { query2doc2relevance.remove(q); query2evaluateddocs.remove(q); query2relevantdocs.remove(q); } LOGGER.info(queriesToRemove.size() + " queries were without " + "relevant or irrilevant results and were removed. " + "Number of queries went from " + originalNQueries + " to " + queries().size() + "."); }
public void compute() { ProgressLogger pl = new ProgressLogger(LOGGER, "pages"); pl.expectedUpdates = page2cat.size(); pl.start("Moving old categories to closest milestones..."); for (IntSet entry : page2cat.values()) { IntSet newCategories = new IntOpenHashSet(); int milestone; for (int cat : entry) { milestone = closestMilestones[cat]; if (milestone != -1) newCategories.add(milestone); } entry.clear(); entry.addAll(newCategories); pl.lightUpdate(); } pl.done(); }
@Override public Stream<? extends FMInstance> stream() { return uidxs.stream() .flatMap(uidx -> { IntSet uidxIidxs = new IntOpenHashSet(); prefs.getUidxIidxs(uidx).forEachRemaining(uidxIidxs::add); List<FMInstance> instances = new ArrayList<>(); // adding positive examples uidxIidxs .forEach(iidx -> instances.add(getInstance(uidx, iidx, 1.0))); // adding negative examples rnd.ints(iidxs.size(), 0, iidxs.size()).map(iidxs::getInt) .filter(jidx -> !uidxIidxs.contains(jidx)) .distinct() .limit((int) (negativeProp * uidxIidxs.size())) .forEach(jidx -> instances.add(getInstance(uidx, jidx, 0.0))); Collections.shuffle(instances); return instances.stream(); }); }
@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; }
/** * Baseline implementation. Augments the "standard" list with alternatives. * * @param l1 * @param l2 * @return */ public static <TK,FV> List<RichTranslation<TK,FV>> mergeAndDedup(List<RichTranslation<TK,FV>> standard, List<RichTranslation<TK,FV>> alt, int maxAltItems) { IntSet hashCodeSet = new IntOpenHashSet(standard.size()); for (RichTranslation<TK,FV> s : standard) { hashCodeSet.add(derivationHashCode(s.getFeaturizable().derivation)); } List<RichTranslation<TK,FV>> returnList = new ArrayList<>(standard); for (int i = 0, sz = Math.min(maxAltItems, alt.size()); i < sz; ++i) { RichTranslation<TK,FV> t = alt.get(i); int hashCode = derivationHashCode(t.getFeaturizable().derivation); if (! hashCodeSet.contains(hashCode)) returnList.add(t); } Collections.sort(returnList); return returnList; }
private static void bbSingleTreeSearch(Ball parent, double[] q, IntDoubleTopN topN, IntSet excluded) { if (!topN.isEmpty() && topN.peek().getDoubleValue() > parent.mip(q)) { return; } if (parent.isLeaf()) { linearSearch(parent, q, topN, excluded); } else { double mipL = parent.getLeftChild().mip(q); double minR = parent.getRightChild().mip(q); if (mipL < minR) { bbSingleTreeSearch(parent.getRightChild(), q, topN, excluded); bbSingleTreeSearch(parent.getLeftChild(), q, topN, excluded); } else { bbSingleTreeSearch(parent.getLeftChild(), q, topN, excluded); bbSingleTreeSearch(parent.getRightChild(), q, topN, excluded); } } }
/** * Collect statistics for the given entry. * - Add it to the passed in set (which could be raw or aggregated) * - Update maximum number of values for Multi-valued entries * - Update Total number of entries * - Check if entry is sorted. * @param entry * @param set */ private void collectEntry(Object entry, IntSet set) { if (entry instanceof Object[]) { for (Object e : (Object[]) entry) { set.add(((Number) e).intValue()); } if (maxNumberOfMultiValues < ((Object[]) entry).length) { maxNumberOfMultiValues = ((Object[]) entry).length; } updateTotalNumberOfEntries((Object[]) entry); } else { int value = ((Number) entry).intValue(); addressSorted(value); updatePartition(value); set.add(value); totalNumberOfEntries++; } }
int calcRandomHits(int _size, int _seed) { IntSet _cache = new IntOpenHashSet(); IntList _list = new IntArrayList(); Random _random = new Random(_seed); int _hitCnt = 0; for (int v : getTrace()) { if(_cache.contains(v)) { _hitCnt++; } else { if (_cache.size() == _size) { int cnt = _random.nextInt(_cache.size()); _cache.remove(_list.get(cnt)); _list.remove(cnt); } _cache.add(v); _list.add(v); } } return _hitCnt; }
void intersectReferenced(final IntSet attributes, final Attribute[] attributeIndex) { final IntIterator referencedIterator = referenced.iterator(); while (referencedIterator.hasNext()) { final int ref = referencedIterator.nextInt(); if (attributes.contains(ref)) { continue; } referencedIterator.remove(); attributeIndex[ref].removeDependent(id); } }
private void initializeRoles() { final IntSet allIds = allIds(); for (final Attribute attribute : attributeIndex) { attribute.addDependent(allIds); attribute.removeDependent(attribute.getId()); attribute.addReferenced(allIds); attribute.removeReferenced(attribute.getId()); } }
private IntSet allIds() { final IntSet ids = new IntOpenHashSet(attributeIndex.length); for (int index = 0; index < attributeIndex.length; ++index) { ids.add(index); } return ids; }
public void execute(final Configuration configuration) throws AlgorithmExecutionException { this.configuration = configuration; final List<TableInfo> tables = tableInfoFactory .create(configuration.getRelationalInputGenerators(), configuration.getTableInputGenerators()); attributeIndex = new Attribute[getTotalColumnCount(tables)]; fillAttributeIndex(tables); final Map<String, IntSet> attributesByType = groupAttributesByType(); for (final IntSet attributes : attributesByType.values()) { handleDomain(attributes); } }
private Map<String, IntSet> groupAttributesByType() { final Map<String, IntSet> attributesByType = new HashMap<>(); for (final Attribute attribute : attributeIndex) { attributesByType .computeIfAbsent(attribute.getType(), k -> new IntOpenHashSet()) .add(attribute.getId()); } return attributesByType; }
private void handleEmptyAttribute(final int attribute, final IntSet attributes) throws AlgorithmExecutionException { for (int other : attributes) { if (attribute != other) { receiveIND(attributeIndex[attribute], attributeIndex[other]); } } }
private IntSet[] computeClosures(final Map<String, IntSet> attributesByValue) { final IntSet[] closures = new IntSet[attributeIndex.length]; for (Map.Entry<String, IntSet> entry : attributesByValue.entrySet()) { for (int attribute : entry.getValue()) { if (closures[attribute] == null) { closures[attribute] = new IntOpenHashSet(entry.getValue()); } else { closures[attribute].retainAll(entry.getValue()); } } } return closures; }
private void computeInclusionDependencies(final IntSet[] closures) throws AlgorithmExecutionException { for (final Attribute attribute : attributeIndex) { final IntSet closure = closures[attribute.getId()]; if (closure == null) { continue; } for (final int rhs : closure) { if (attribute.getId() != rhs) { receiveIND(attribute, attributeIndex[rhs]); } } } }
/** */ public Int2ObjectArrayMap<IntSet> indexesMap() { Int2ObjectArrayMap<IntSet> res = new Int2ObjectArrayMap<>(); for (Integer row : sto.keySet()) res.put(row.intValue(), (IntSet)sto.get(row).keySet()); return res; }
private String buildInputDataPartitionSchema(List<JCL_result> r, int numOfJCLThreads){ IntSet sorted = new IntAVLTreeSet(); long totalF=0; Int2LongMap map = new Int2LongOpenHashMap(); for(JCL_result oneR:r){ try{ @SuppressWarnings("unchecked") List<String> l = (List<String>) oneR.getCorrectResult(); for(String s : l){ String[] args = s.split(":"); int key = Integer.parseInt(args[0]); long freq = Long.parseLong(args[1]); sorted.add(key); if(map.containsKey(key)){ freq+=map.get(key); totalF+=map.get(key); } else totalF+=freq; map.put(key, freq); } }catch(Exception e){} } long load=0; int b; String result = ""; for(int ac:sorted){ load += map.get(ac); if(load > (totalF/(numOfJCLThreads))){ b=ac; result += b + ":"; load=0; } } return result; }
@Override public int countUnique() { IntSet ints = new IntOpenHashSet(size()); for (int i = 0; i < size(); i++) { ints.add(data.getInt(i)); } return ints.size(); }
@Override public DateColumn unique() { IntSet ints = new IntOpenHashSet(data.size()); for (int i = 0; i < size(); i++) { ints.add(data.getInt(i)); } return new DateColumn(name() + " Unique values", IntArrayList.wrap(ints.toIntArray())); }
@SuppressWarnings("unchecked") public LlamaFurScorer(String graphBasename, String matrixPath, String page2catPath, String name) throws ClassNotFoundException, IOException { this( ImmutableGraph.load(graphBasename), (Matrix) SerializationUtils.read(matrixPath), (Int2ObjectMap<IntSet>) SerializationUtils.read(page2catPath), name ); }
private double expectedness(IntSet nodeI, IntSet nodeJ) { double sum = 0; for (int a : nodeI) for (int b : nodeJ) sum += w.get(a, b); return sum; }
@Override public Int2DoubleMap scores(int docI) { IntSet successors = successors(docI); Int2DoubleMap results = new Int2DoubleOpenHashMap(successors.size()); IntSet catI = page2cat.get(docI); for (int docJ : successors) { IntSet catJ = page2cat.get(docJ); results.put(docJ, -expectedness(catI, catJ)); } return results; }