public void testToArrayWithParams() {
    int no_entry_value = Integer.MIN_VALUE;
    TIntSet set = new TIntHashSet(10, 0.5f, no_entry_value);
    assertEquals(no_entry_value, set.getNoEntryValue());

    int[] ints = {42, 1138, 13, 86, 99};
    set.addAll(ints);

    int[] sink = new int[ints.length + 2];
    sink[sink.length - 1] = -1;
    sink[sink.length - 2] = -2;

    int[] res = set.toArray(sink);
    assertEquals(set.getNoEntryValue(), res[set.size()]);

    Set<Integer> copy = new HashSet<Integer>();
    for (int element : sink) {
      copy.add(Integer.valueOf(element));
    }

    Set<Integer> bogey = new HashSet<Integer>();
    for (int element : ints) {
      bogey.add(Integer.valueOf(element));
    }
    bogey.add(-1);
    bogey.add(no_entry_value);
    assertEquals(bogey, copy);
  }
 public void testToArray() {
   TIntSet set = new TIntHashSet();
   int[] ints = {42, 1138, 13, 86, 99};
   set.addAll(ints);
   int[] res = set.toArray();
   Arrays.sort(ints);
   Arrays.sort(res);
   assertTrue(Arrays.equals(ints, res));
 }
Пример #3
0
 private int[] pick(int n, double p) {
   TIntSet set = new TIntHashSet();
   // Pick at least one of the numbers
   set.add(random.nextInt(n));
   // And add some additional ones randomly
   for (int i = 0; i < n; i++) {
     if (random.nextDouble() > p) {
       set.add(i);
     }
   }
   return set.toArray();
 }
  public void testToArrayMatchesIteratorOrder() {
    TIntSet set = new TIntHashSet();
    int[] ints = {42, 1138, 13, 86, 99};
    set.addAll(ints);
    int[] toarray_ints = set.toArray();

    int[] iter_ints = new int[5];
    TIntIterator iter = set.iterator();

    int index = 0;
    while (iter.hasNext()) {
      iter_ints[index++] = iter.next();
    }

    assertTrue(Arrays.equals(iter_ints, toarray_ints));
  }
Пример #5
0
  @Override
  public SRResultList predictMostSimilar(
      List<SRResultList> scores, int maxResults, TIntSet validIds) {
    if (2 * scores.size() + 1 != mostSimilarCoefficients.size()) {
      throw new IllegalStateException();
    }
    TIntSet allIds = new TIntHashSet(); // ids returned by at least one metric
    for (SRResultList resultList : scores) {
      if (resultList != null) {
        for (SRResult result : resultList) {
          allIds.add(result.getId());
        }
      }
    }

    TIntDoubleHashMap scoreMap = new TIntDoubleHashMap();
    for (int id : allIds.toArray()) {
      scoreMap.put(id, mostSimilarCoefficients.get(0));
    }
    int i = 1;
    for (SRResultList resultList : scores) {
      TIntSet unknownIds = new TIntHashSet(allIds);
      double c1 = mostSimilarCoefficients.get(i); // score coeff
      double c2 = mostSimilarCoefficients.get(i + 1); // rank coefficient
      if (resultList != null) {
        for (int j = 0; j < resultList.numDocs(); j++) {
          int rank = j + 1;
          // expand or contract ranks proportionately
          if (validIds != null) {
            double k = 1.0 * numTrainingCandidateArticles / validIds.size();
            rank = (int) (rank * k);
          }
          SRResult result = resultList.get(j);
          unknownIds.remove(result.getId());
          double value = c1 * result.getScore() + c2 * Math.log(rank);
          if (debug) {
            System.err.format(
                "%s %d. %.3f (id=%d), computing %.3f * %.3f + %.3f * (log(%d) = %.3f)\n",
                "m" + i, j, value, result.getId(), c1, result.getScore(), c2, rank, Math.log(rank));
          }
          scoreMap.adjustValue(result.getId(), value);
        }
      }

      // interpolate scores for unknown ids
      double value =
          c1 * mostSimilarInterpolator.getInterpolatedScore(i / 2)
              + c2 * Math.log(mostSimilarInterpolator.getInterpolatedRank(i / 2));
      for (int id : unknownIds.toArray()) {
        scoreMap.adjustValue(id, value);
      }
      i += 2;
    }
    List<SRResult> resultList = new ArrayList<SRResult>();
    for (int id : scoreMap.keys()) {
      resultList.add(new SRResult(id, scoreMap.get(id)));
    }
    Collections.sort(resultList);
    Collections.reverse(resultList);
    int size = maxResults > resultList.size() ? resultList.size() : maxResults;
    SRResultList result = new SRResultList(size);
    for (i = 0; i < size; i++) {
      result.set(i, resultList.get(i));
    }
    return result;
  }
Пример #6
0
  private static SDGNode convertNode(SDGBuilder sdg, PDGNode node) {
    Operation op = null;
    Kind kind = null;
    int[] allocNodes = null;

    switch (node.getKind()) {
      case ACTUAL_IN:
        op = Operation.ACTUAL_IN;
        kind = Kind.ACTUAL_IN;
        break;
      case ACTUAL_OUT:
        op = Operation.ACTUAL_OUT;
        kind = Kind.ACTUAL_OUT;
        break;
      case CALL:
        op = Operation.CALL;
        kind = Kind.CALL;
        if (sdg.cfg.computeInterference) {
          TIntSet allocNodesAsSet = sdg.getAllocationNodes(node);
          if (allocNodesAsSet != null) {
            allocNodes = allocNodesAsSet.toArray();
          }
        }
        break;
      case ENTRY:
        op = Operation.ENTRY;
        kind = Kind.ENTRY;
        break;
      case EXIT:
        op = Operation.EXIT;
        kind = Kind.EXIT;
        break;
      case EXPRESSION:
        op = Operation.ASSIGN;
        kind = Kind.EXPRESSION;
        break;
      case FOLDED:
        op = Operation.COMPOUND;
        kind = Kind.FOLDED;
        break;
      case FORMAL_IN:
        op = Operation.FORMAL_IN;
        kind = Kind.FORMAL_IN;
        break;
      case FORMAL_OUT:
        op = Operation.FORMAL_OUT;
        kind = Kind.FORMAL_OUT;
        break;
      case HREAD:
        op = Operation.REFERENCE;
        kind = Kind.EXPRESSION;
        break;
      case HWRITE:
        op = Operation.MODIFY;
        kind = Kind.EXPRESSION;
        break;
      case JOIN:
        op = Operation.COMPOUND;
        kind = Kind.JOIN;
        break;
      case NEW:
        op = Operation.DECLARATION;
        kind = Kind.NORMAL;
        break;
      case NORMAL:
        op = Operation.COMPOUND;
        kind = Kind.NORMAL;
        break;
      case PHI:
        op = Operation.ASSIGN;
        kind = Kind.EXPRESSION;
        break;
      case PREDICATE:
        op = Operation.IF;
        kind = Kind.PREDICATE;
        break;
      case SYNCHRONIZATION:
        op = Operation.MONITOR;
        kind = Kind.SYNCHRONIZATION;
        break;
      default:
        throw new IllegalStateException("Unknown node kind: " + node.getKind().name());
    }
    SourceLocation sloc = node.getSourceLocation();

    SDGNode sn =
        new SecurityNode(
            node.getId(),
            op,
            node.getLabel(),
            node.getPdgId(),
            node.getType(),
            sloc.getSourceFile(),
            sloc.getStartRow(),
            sloc.getStartColumn(),
            sloc.getEndRow(),
            sloc.getEndColumn(),
            node.getBytecodeName(),
            node.getBytecodeIndex());

    if (node.getKind() == PDGNode.Kind.ENTRY) {
      PDG pdg = sdg.getPDGforId(node.getPdgId());
      IMethod im = pdg.getMethod();

      if (im != null) {
        IClass cls = im.getDeclaringClass();

        if (cls != null) {
          String clsLoader = cls.getClassLoader().toString();
          sn.setClassLoader(clsLoader);
        }
      }
    }

    if (allocNodes != null) {
      sn.setAllocationSites(allocNodes);
    }

    if (node.getAliasDataSources() != null) {
      sn.setAliasDataSources(node.getAliasDataSources());
    }

    assert sn.getKind() == kind;

    return sn;
  }