Java Code Examples for gnu.trove.TIntArrayList#add()

The following examples show how to use gnu.trove.TIntArrayList#add() . 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 File: TreeNode.java    From semafor-semantic-parser with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param alevel Level (depth) of the desired ancestor
 * @param ancPath A list to be populated with indices of nodes searched (starting with the current node), provided that an ancestor node at the specified level exists; or null.
 * @param alreadySearched Indices of nodes which have already been searched. If non-null, 'this' will be returned if (and only if) a member of this set is encountered in the ancestor path. 
 * @return Ancestor node of the current node whose level is 'alevel', or null if no such ancestor exists. A node is not considered to be its own ancestor.
 * @author Nathan Schneider (nschneid)
 */
@SuppressWarnings("unchecked")
public T getAncestorAtLevel(int alevel, TIntArrayList ancPath, TIntHashSet alreadySearched) {
	if (alevel < 0 || alevel >= this.depth)	// A node at this level is not strictly an ancestor
		return null;
	
	TreeNode<T> node = this;
	for (int d=this.depth; d>alevel; d--) {
		if (ancPath!=null)
			ancPath.add(node.index);
		if (alreadySearched!=null && alreadySearched.contains(node.index))
			return (T)this;
		node = node.getParent();
	}
	return (T)node;
}
 
Example 2
Source File: BestKNNNegativesChooser.java    From jatecs with GNU General Public License v3.0 6 votes vote down vote up
public TIntArrayListIterator selectNegatives(String category) {

        short catID = _index.getCategoryDB().getCategory(category);

        TreeSet<Item> best = _best.get(catID);
        assert (best != null);

        TIntArrayList neg = new TIntArrayList();
        Iterator<Item> it = best.iterator();
        while (it.hasNext()) {
            Item docS = it.next();
            assert (!neg.contains(docS.docID));
            neg.add(docS.docID);
        }

        neg.sort();

        return new TIntArrayListIterator(neg);
    }
 
Example 3
Source File: Reindexer.java    From consulo with Apache License 2.0 6 votes vote down vote up
private int[] discard(int[] needed, int[] toDiscard, int arrayIndex) {
  myOriginalLengths[arrayIndex] = toDiscard.length;
  int[] sorted1 = createSorted(needed);
  TIntArrayList discarded = new TIntArrayList(toDiscard.length);
  TIntArrayList oldIndecies = new TIntArrayList(toDiscard.length);
  for (int i = 0; i < toDiscard.length; i++) {
    int index = toDiscard[i];
    if (Arrays.binarySearch(sorted1, index) >= 0) {
      discarded.add(index);
      oldIndecies.add(i);
    }
  }
  myOldIndecies[arrayIndex] = oldIndecies.toNativeArray();
  myDiscardedLengths[arrayIndex] = discarded.size();
  return discarded.toNativeArray();
}
 
Example 4
Source File: KMeansDocumentClusterizer.java    From jatecs with GNU General Public License v3.0 6 votes vote down vote up
protected void computeInitialCentroids(DocumentCentroid[] centroids,
                                       IIntIterator documents, IIndex index) {
    TIntArrayList initial = new TIntArrayList();
    documents.begin();
    while (documents.hasNext()) {
        initial.add(documents.next());
    }

    Random r = new Random();
    for (int i = 0; i < centroids.length; i++) {
        // Choose random document.
        int nextDoc = initial.get(r.nextInt(initial.size()));

        TIntArrayList ar = new TIntArrayList();
        ar.add(nextDoc);
        centroids[i].features = Clustering.computeDocumentCentroid(
                new TIntArrayListIterator(ar), index);
    }
}
 
Example 5
Source File: TroveContentDB.java    From jatecs with GNU General Public License v3.0 5 votes vote down vote up
public IIntIterator getFeatureDocuments(int feature) {
    IIntIterator docIt = _documentsDB.getDocuments();
    int document = 0;
    TIntArrayList documents = new TIntArrayList();
    while (docIt.hasNext()) {
        document = docIt.next();
        if (hasDocumentFeature(document, feature))
            documents.add(document);
    }
    return new TIntArrayListIterator(documents);
}
 
Example 6
Source File: AbstractRecordsTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
private TIntArrayList scanForFreeRecords() throws IOException {
  final TIntArrayList result = new TIntArrayList();
  for (int i = 1; i <= getRecordsCount(); i++) {
    if (getSize(i) == -1) {
      result.add(i);
    }
  }
  return result;
}
 
Example 7
Source File: ByChar.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CharOffsets getPunctuationChars(@Nonnull CharSequence text) {
  TIntArrayList chars = new TIntArrayList(text.length());
  TIntArrayList offsets = new TIntArrayList(text.length());

  for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    if (isPunctuation(c)) {
      chars.add(c);
      offsets.add(i);
    }
  }

  return new CharOffsets(chars.toNativeArray(), offsets.toNativeArray());
}
 
Example 8
Source File: TreeBoostLearner.java    From jatecs with GNU General Public License v3.0 5 votes vote down vote up
protected IIndex selectPositives(short catID, IIndex training) {
    // First create a new index.
    IIndex idx = training.cloneIndex();

    IShortIterator childCats = getAllChildsCategoriesFor(idx, catID);
    short nextCatID = Short.MIN_VALUE;
    if (childCats.hasNext())
        nextCatID = childCats.next();

    // Remove unwanted categories.
    TShortArrayList toRemove = new TShortArrayList();
    for (short i = 0; i < training.getCategoryDB().getCategoriesCount(); i++) {
        if (i == nextCatID) {
            if (childCats.hasNext())
                nextCatID = childCats.next();
            continue;
        }

        toRemove.add(i);
    }
    toRemove.sort();
    idx.removeCategories(new TShortArrayListIterator(toRemove));

    // Remove unwanted documents.
    TIntArrayList docsToRemove = new TIntArrayList();
    IIntIterator docs = idx.getDocumentDB().getDocuments();
    while (docs.hasNext()) {
        int docID = docs.next();
        IShortIterator curCats = idx.getClassificationDB()
                .getDocumentCategories(docID);
        if (!curCats.hasNext())
            docsToRemove.add(docID);
    }

    docsToRemove.sort();
    idx.removeDocuments(new TIntArrayListIterator(docsToRemove), false);

    return idx;
}
 
Example 9
Source File: TreeBoostCMCLearner.java    From jatecs with GNU General Public License v3.0 5 votes vote down vote up
protected IIndex selectPositives(short catID, IIndex training) {
    // First create a new index.
    IIndex idx = training.cloneIndex();

    IShortIterator childCats = getAllChildsCategoriesFor(idx, catID);
    short nextCatID = Short.MIN_VALUE;
    if (childCats.hasNext())
        nextCatID = childCats.next();

    // Remove unwanted categories.
    TShortArrayList toRemove = new TShortArrayList();
    for (short i = 0; i < training.getCategoryDB().getCategoriesCount(); i++) {
        if (i == nextCatID) {
            if (childCats.hasNext())
                nextCatID = childCats.next();
            continue;
        }

        toRemove.add(i);
    }
    toRemove.sort();
    idx.removeCategories(new TShortArrayListIterator(toRemove));

    // Remove unwanted documents.
    TIntArrayList docsToRemove = new TIntArrayList();
    IIntIterator docs = idx.getDocumentDB().getDocuments();
    while (docs.hasNext()) {
        int docID = docs.next();
        IShortIterator curCats = idx.getClassificationDB()
                .getDocumentCategories(docID);
        if (!curCats.hasNext())
            docsToRemove.add(docID);
    }

    docsToRemove.sort();
    idx.removeDocuments(new TIntArrayListIterator(docsToRemove), false);

    return idx;
}
 
Example 10
Source File: StringSearcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public int[] findAllOccurrences(@Nonnull CharSequence text) {
  int end = text.length();
  TIntArrayList result = new TIntArrayList();
  for (int index = 0; index < end; index++) {
    //noinspection AssignmentToForLoopParameter
    index = scan(text, index, end);
    if (index < 0) break;
    result.add(index);
  }
  return result.toNativeArray();
}
 
Example 11
Source File: LineSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static LineSet createLineSet(@Nonnull CharSequence text, boolean markModified) {
  TIntArrayList starts = new TIntArrayList();
  TByteArrayList flags = new TByteArrayList();

  LineTokenizer lineTokenizer = new LineTokenizer(text);
  while (!lineTokenizer.atEnd()) {
    starts.add(lineTokenizer.getOffset());
    flags.add((byte)(lineTokenizer.getLineSeparatorLength() | (markModified ? MODIFIED_MASK : 0)));
    lineTokenizer.advance();
  }
  return new LineSet(starts.toNativeArray(), flags.toNativeArray(), text.length());
}
 
Example 12
Source File: DFSTBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
public Frame(int nodeI) {
  this.nodeI = nodeI;
  Iterator<Node> outNodes = myGraph.getOut(myAllNodes[nodeI]);
  TIntArrayList list = new TIntArrayList();

  while (outNodes.hasNext()) {
    Node node = outNodes.next();
    list.add(nodeIndex.get(node));
  }
  out = list.toNativeArray();
}
 
Example 13
Source File: MergeModelBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredUIAccess
private TIntArrayList collectAffectedChanges(@Nonnull TIntArrayList directChanges) {
  TIntArrayList result = new TIntArrayList(directChanges.size());

  int directArrayIndex = 0;
  int otherIndex = 0;
  while (directArrayIndex < directChanges.size() && otherIndex < getChangesCount()) {
    int directIndex = directChanges.get(directArrayIndex);

    if (directIndex == otherIndex) {
      result.add(directIndex);
      otherIndex++;
      continue;
    }

    int directStart = getLineStart(directIndex);
    int directEnd = getLineEnd(directIndex);
    int otherStart = getLineStart(otherIndex);
    int otherEnd = getLineEnd(otherIndex);

    if (otherEnd < directStart) {
      otherIndex++;
      continue;
    }
    if (otherStart > directEnd) {
      directArrayIndex++;
      continue;
    }

    result.add(otherIndex);
    otherIndex++;
  }

  LOG.assertTrue(directChanges.size() <= result.size());
  return result;
}
 
Example 14
Source File: GlobalThresholdTSR.java    From jatecs with GNU General Public License v3.0 5 votes vote down vote up
public void computeTSR(IIndex index) {
    TextualProgressBar bar = new TextualProgressBar(
            "Compute global threshold ("
                    + Os.generateDoubleString(_threshold, 3)
                    + ") TSR with " + _function.getClass().getName());
    int total = index.getFeatureDB().getFeaturesCount();
    int step = 0;

    TIntArrayList toRemove = new TIntArrayList();

    IIntIterator it = index.getFeatureDB().getFeatures();
    while (it.hasNext()) {
        int featID = it.next();

        double score = _function.compute((short) 0, featID, index);

        if (score < _threshold)
            toRemove.add(featID);

        step++;
        bar.signal((step * 100) / total);
    }

    bar.signal(100);

    toRemove.sort();

    // Remove the worst features.
    JatecsLogger.status().print(
            "Removing " + toRemove.size() + " features...");
    index.removeFeatures(new TIntArrayListIterator(toRemove));
    JatecsLogger.status().println(
            "done. Now the DB contains "
                    + index.getFeatureDB().getFeaturesCount()
                    + " feature(s).");
}
 
Example 15
Source File: TroveContentDB.java    From jatecs with GNU General Public License v3.0 5 votes vote down vote up
public IIntIterator getUnusedFeatures() {
    TIntArrayList zeroFeatures = new TIntArrayList();
    IIntIterator it = _featuresDB.getFeatures();
    while (it.hasNext()) {
        int feat = it.next();
        if (getFeatureDocumentsCount(feat) == 0)
            zeroFeatures.add(feat);
    }

    return new TIntArrayListIterator(zeroFeatures);
}
 
Example 16
Source File: IndexPatternSearcher.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean collectPatternMatches(IndexPattern[] allIndexPatterns,
                                             IndexPattern indexPattern,
                                             CharSequence chars,
                                             List<? extends CommentRange> commentRanges,
                                             int commentNum,
                                             PsiFile file,
                                             TextRange range,
                                             Processor<? super IndexPatternOccurrence> consumer,
                                             TIntArrayList matches,
                                             boolean multiLine) {
  CommentRange commentRange = commentRanges.get(commentNum);
  int commentStart = commentRange.startOffset;
  int commentEnd = commentRange.endOffset;
  int commentPrefixLength = commentRange.prefixLength;
  int commentSuffixLength = commentRange.suffixLength;
  Pattern pattern = indexPattern.getPattern();
  if (pattern != null) {
    ProgressManager.checkCanceled();

    CharSequence input = StringPattern.newBombedCharSequence(new CharSequenceSubSequence(chars, commentStart - commentPrefixLength, commentEnd + commentSuffixLength));
    Matcher matcher = pattern.matcher(input);
    while (true) {
      //long time1 = System.currentTimeMillis();
      boolean found = matcher.find();
      //long time2 = System.currentTimeMillis();
      //System.out.println("scanned text of length " + (lexer.getTokenEnd() - lexer.getTokenStart() + " in " + (time2 - time1) + " ms"));

      if (!found) break;
      int suffixStartOffset = input.length() - commentSuffixLength;
      int start = fitToRange(matcher.start(), commentPrefixLength, suffixStartOffset) + commentStart - commentPrefixLength;
      int end = fitToRange(matcher.end(), commentPrefixLength, suffixStartOffset) + commentStart - commentPrefixLength;
      if (start != end) {
        if ((range == null || range.getStartOffset() <= start && end <= range.getEndOffset()) && matches.indexOf(start) == -1) {
          List<TextRange> additionalRanges = multiLine ? findContinuation(start, chars, allIndexPatterns, commentRanges, commentNum) : Collections.emptyList();
          if (range != null && !additionalRanges.isEmpty() && additionalRanges.get(additionalRanges.size() - 1).getEndOffset() > range.getEndOffset()) {
            continue;
          }
          matches.add(start);
          IndexPatternOccurrenceImpl occurrence = new IndexPatternOccurrenceImpl(file, start, end, indexPattern, additionalRanges);
          if (!consumer.process(occurrence)) {
            return false;
          }
        }
      }

      ProgressManager.checkCanceled();
    }
  }
  return true;
}
 
Example 17
Source File: WidgetPerfTable.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void showStats(ArrayList<SlidingWindowStatsSummary> entries) {
  if (entries == null) {
    entries = new ArrayList<>();
  }
  idle = false;
  widgetNameColumnInfo.setIdle(false);
  final ArrayList<SlidingWindowStatsSummary> oldEntries = this.entries;
  sortByMetric(entries);
  this.entries = entries;
  int selectionIndex = -1;
  Location lastSelectedLocation = null;

  if (statsChanged(oldEntries, entries)) {
    final int selectedRowIndex = getSelectedRow();
    if (selectedRowIndex != -1) {
      final Object selectedRow = getTreeModel().getRowValue(selectedRowIndex);
      if (selectedRow instanceof DefaultMutableTreeNode) {
        final DefaultMutableTreeNode selectedRowNode = (DefaultMutableTreeNode)selectedRow;
        final SlidingWindowStatsSummary selectedStats = (SlidingWindowStatsSummary)selectedRowNode.getUserObject();
        if (selectedStats != null) {
          lastSelectedLocation = selectedStats.getLocation();
        }
      }
    }
    final boolean previouslyEmpty = root.getChildCount() == 0;
    int childIndex = 0;
    final TIntArrayList indicesChanged = new TIntArrayList();
    final TIntArrayList indicesInserted = new TIntArrayList();
    for (SlidingWindowStatsSummary entry : entries) {
      if (entry.getLocation().equals(lastSelectedLocation)) {
        selectionIndex = childIndex;
      }
      if (childIndex >= root.getChildCount()) {
        root.add(new DefaultMutableTreeNode(entry, false));
        indicesInserted.add(childIndex);
      }
      else {
        final DefaultMutableTreeNode existing = (DefaultMutableTreeNode)root.getChildAt(childIndex);
        final SlidingWindowStatsSummary existingEntry = (SlidingWindowStatsSummary)existing.getUserObject();
        if (displayChanged(entry, existingEntry)) {
          model.nodeChanged(existing);
          indicesChanged.add(childIndex);
        }
        existing.setUserObject(entry);
      }
      childIndex++;
    }
    final int endChildIndex = childIndex;
    final ArrayList<TreeNode> nodesRemoved = new ArrayList<>();
    final TIntArrayList indicesRemoved = new TIntArrayList();
    // Gather nodes to remove.
    for (int j = endChildIndex; j < root.getChildCount(); j++) {
      nodesRemoved.add(root.getChildAt(j));
      indicesRemoved.add(j);
    }
    // Actuallly remove nodes.
    while (endChildIndex < root.getChildCount()) {
      // Removing the last element is slightly more efficient.
      final int lastChild = root.getChildCount() - 1;
      root.remove(lastChild);
    }

    if (previouslyEmpty) {
      // TODO(jacobr): I'm not clear why this event is needed in this case.
      model.nodeStructureChanged(root);
    }
    else {
      // Report events for all the changes made to the table.
      if (indicesChanged.size() > 0) {
        model.nodesChanged(root, indicesChanged.toNativeArray());
      }
      if (indicesInserted.size() > 0) {
        model.nodesWereInserted(root, indicesInserted.toNativeArray());
      }
      if (indicesRemoved.size() > 0) {
        model.nodesWereRemoved(root, indicesRemoved.toNativeArray(), nodesRemoved.toArray());
      }
    }

    if (selectionIndex >= 0) {
      getSelectionModel().setSelectionInterval(selectionIndex, selectionIndex);
      currentSelection = (DefaultMutableTreeNode)root.getChildAt(selectionIndex);
    }
    else {
      getSelectionModel().clearSelection();
    }
  }
}
 
Example 18
Source File: DCSSingleLabelKnnFoldValidator.java    From jatecs with GNU General Public License v3.0 4 votes vote down vote up
public static Pair<IIndex, IIndex> splitIndex(int step, IIndex index,
                                              int numValidationSteps) {
    int numPositives = index.getDocumentDB().getDocumentsCount();

    int numSteps = Math.min(numPositives, numValidationSteps);
    if (step >= numSteps)
        return null;

    TIntArrayList tr = new TIntArrayList();
    TIntArrayList va = new TIntArrayList();

    int numPositivesInValidation = numPositives / numSteps;
    int numPositivesInTraining = numPositives - numPositivesInValidation;
    int startTrainingID = (numPositives / numSteps) * step;
    int endTrainingID = (startTrainingID + numPositivesInTraining - 1);
    TIntIntHashMap map = new TIntIntHashMap();
    for (int i = startTrainingID; i <= endTrainingID; i++) {
        int v = i % numPositives;
        map.put(v, v);
    }

    int curDoc = 0;
    IIntIterator docs = index.getDocumentDB().getDocuments();
    while (docs.hasNext()) {
        int docID = docs.next();
        if (map.containsKey(curDoc)) {
            tr.add(docID);
        } else {
            va.add(docID);
        }
        curDoc++;
    }

    tr.sort();
    va.sort();

    IIndex trIndex = index.cloneIndex();
    trIndex.removeDocuments(new TIntArrayListIterator(va), false);

    IIndex vaIndex = index.cloneIndex();
    vaIndex.removeDocuments(new TIntArrayListIterator(tr), false);

    JatecsLogger.status().println(
            "done. The training contains " + tr.size()
                    + " document(s) and the validation contains "
                    + va.size() + " document(s).");

    Pair<IIndex, IIndex> ret = new Pair<IIndex, IIndex>(trIndex, vaIndex);
    return ret;

}
 
Example 19
Source File: TroveWeightingDB.java    From jatecs with GNU General Public License v3.0 4 votes vote down vote up
public void removeFeatures(IIntIterator removedFeatures) {
    for (int i = 0; i < _documentsWeights.size(); ++i) {
        TIntDoubleHashMap weigs = _documentsWeights.get(i);
        TIntArrayList feats = new TIntArrayList(weigs.size());
        TDoubleArrayList weigths = new TDoubleArrayList(weigs.size());
        TIntDoubleIterator wit = weigs.iterator();
        while (wit.hasNext()) {
            wit.advance();
            feats.add(wit.key());
            weigths.add(wit.value());
        }
        int j = 0;
        int shift = 0;
        int feat;
        int rem;
        if (j < feats.size() && removedFeatures.hasNext()) {
            feat = feats.getQuick(j);
            rem = removedFeatures.next();

            while (true) {
                if (feat == rem) {
                    feats.remove(j);
                    weigths.remove(j);
                    if (j < feats.size() && removedFeatures.hasNext()) {
                        feat = feats.getQuick(j);
                        rem = removedFeatures.next();
                        ++shift;
                    } else
                        break;
                } else if (feat > rem) {
                    if (removedFeatures.hasNext()) {
                        rem = removedFeatures.next();
                        ++shift;
                    } else
                        break;
                } else {
                    feats.setQuick(j, feat - shift);
                    ++j;
                    if (j < feats.size())
                        feat = feats.getQuick(j);
                    else
                        break;
                }
            }
            ++shift;
        }
        while (j < feats.size()) {
            feats.setQuick(j, feats.getQuick(j) - shift);
            ++j;
        }

        weigs.clear();
        for (j = 0; j < feats.size(); ++j)
            weigs.put(feats.getQuick(j), weigths.getQuick(j));

        removedFeatures.begin();
    }
}
 
Example 20
Source File: SingleLabelKnnFoldValidator.java    From jatecs with GNU General Public License v3.0 4 votes vote down vote up
public static Pair<IIndex, IIndex> splitIndex(int step, IIndex index,
                                              int numValidationSteps) {
    int numPositives = index.getDocumentDB().getDocumentsCount();

    int numSteps = Math.min(numPositives, numValidationSteps);
    if (step >= numSteps)
        return null;

    TIntArrayList tr = new TIntArrayList();
    TIntArrayList va = new TIntArrayList();

    int numPositivesInValidation = numPositives / numSteps;
    int numPositivesInTraining = numPositives - numPositivesInValidation;
    int startTrainingID = (numPositives / numSteps) * step;
    int endTrainingID = (startTrainingID + numPositivesInTraining - 1);
    TIntIntHashMap map = new TIntIntHashMap();
    for (int i = startTrainingID; i <= endTrainingID; i++) {
        int v = i % numPositives;
        map.put(v, v);
    }

    int curDoc = 0;
    IIntIterator docs = index.getDocumentDB().getDocuments();
    while (docs.hasNext()) {
        int docID = docs.next();
        if (map.containsKey(curDoc)) {
            tr.add(docID);
        } else {
            va.add(docID);
        }
        curDoc++;
    }

    tr.sort();
    va.sort();

    IIndex trIndex = index.cloneIndex();
    trIndex.removeDocuments(new TIntArrayListIterator(va), false);

    IIndex vaIndex = index.cloneIndex();
    vaIndex.removeDocuments(new TIntArrayListIterator(tr), false);

    JatecsLogger.status().println(
            "done. The training contains " + tr.size()
                    + " document(s) and the validation contains "
                    + va.size() + " document(s).");

    Pair<IIndex, IIndex> ret = new Pair<IIndex, IIndex>(trIndex, vaIndex);
    return ret;

}