예제 #1
0
 /**
  * Converts a BitSet to a Byte[]
  *
  * @param bs
  * @return
  */
 public static byte[] bitSetToByteArray(BitSet bs) {
   byte[] bytes = new byte[(int) Math.ceil(bs.size() / 8)];
   for (int i = 0; i < bs.size(); i++) {
     if (bs.get(i) == true) {
       bytes[i / 8] |= 1 << i;
     }
   }
   return bytes;
 }
 private byte[] makePersistedFingerPrint(BitSet fingerPrint) {
   if (fingerPrint == null) {
     return new byte[0];
   }
   byte[] persistedFingerPrint = new byte[fingerPrint.size()];
   for (int i = 0; i < fingerPrint.size(); i++) {
     persistedFingerPrint[i] = (byte) (fingerPrint.get(i) ? 1 : 0);
   }
   return persistedFingerPrint;
 }
예제 #3
0
 // 处理数据,找到素数
 public static void findSushuBitSet(BitSet bs) {
   for (int i = 0; i < bs.size(); i++) {
     if (bs.get(i)) {
       // 内循环遍历
       for (int j = 2 * i; j < bs.size(); j += i) {
         bs.set(j, false);
       }
     }
   }
 }
예제 #4
0
    void setExecuted(ThreadInfo ti, Instruction insn) {
      int idx = ti.getId();

      if (covered == null) {
        covered = new BitSet[idx + 1];
      } else if (idx >= covered.length) {
        BitSet[] a = new BitSet[idx + 1];
        System.arraycopy(covered, 0, a, 0, covered.length);
        covered = a;
      }

      if (covered[idx] == null) {
        covered[idx] = new BitSet(mi.getInstructions().length);
      }

      int off = insn.getInstructionIndex();
      covered[idx].set(off);

      if (showBranchCoverage && (insn instanceof IfInstruction)) {
        if (branchTrue == null) {
          branchTrue = new BitSet(mi.getInstructions().length);
          branchFalse = new BitSet(branchTrue.size());
        }
        if (!((IfInstruction) insn).getConditionValue()) {
          branchTrue.set(off);
        } else {
          branchFalse.set(off);
        }
      }
    }
예제 #5
0
 public BitSet mutarBitSet(BitSet bitset, int tamMaximo) {
   BitSet bs = new BitSet(bitset.size());
   Random rnd = new Random();
   bs.clear();
   System.out.println("Largo del bitset: " + tamMaximo);
   for (int i = 0; i < tamMaximo; i++) if (rnd.nextInt(2) == 1) bs.set(i);
   return bs;
 }
예제 #6
0
 // 第0,1位置成false,其余全部是true.
 public static void initBitSet(BitSet bs) {
   for (int i = 0; i < bs.size(); i++) {
     if (i == 0 || i == 1) {
       bs.set(i, false);
     } else {
       bs.set(i, true);
     }
   }
 }
예제 #7
0
  public void print() {
    int i;

    System.out.print("use[" + index + "] = { ");
    for (i = 0; i < use.size(); ++i) if (use.get(i)) System.out.print("" + i + " ");
    System.out.println("}");
    System.out.print("def[" + index + "] = { ");
    for (i = 0; i < def.size(); ++i) if (def.get(i)) System.out.print("" + i + " ");
    System.out.println("}\n");

    System.out.print("in[" + index + "] = { ");
    for (i = 0; i < in.size(); ++i) if (in.get(i)) System.out.print("" + i + " ");
    System.out.println("}");

    System.out.print("out[" + index + "] = { ");
    for (i = 0; i < out.size(); ++i) if (out.get(i)) System.out.print("" + i + " ");
    System.out.println("}\n");
  }
예제 #8
0
 public int toInt() {
   int size = 1;
   int ret = 0;
   for (int i = 0; i < G.size(); i++) {
     if (G.get(i)) ret += size;
     size <<= 1;
   }
   return ret;
 }
  /** Add all local variables of interest from the provided bitset. */
  public Set<LocalVariable> addLiveLocalVars(Set<LocalVariable> list, BitSet living) {
    for (int j = 0; j < living.size(); j++) {
      if (!living.get(j)) continue;

      Variable v = getVariable(j);
      if (v instanceof LocalVariable) list.add((LocalVariable) v);
    }

    return list;
  }
 @Test
 public void testFingerprinterBitSetSize() throws Exception {
   ShortestPathFingerprinter fingerprinter = new ShortestPathFingerprinter(1024);
   Assert.assertNotNull(fingerprinter);
   IAtomContainer mol = TestMoleculeFactory.makeIndole();
   AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
   BitSet bs = fingerprinter.getBitFingerprint(mol).asBitSet();
   Assert.assertEquals(1024, bs.length()); // highest set bit
   Assert.assertEquals(1024, bs.size()); // actual bit set size
 }
예제 #11
0
  void _followPosition(BitSet[] follow, SyntaxNode[] nodes) {
    BitSet last, first;
    int size;

    _left._followPosition(follow, nodes);

    last = _lastPosition();
    first = _firstPosition();
    size = last.size();

    while (0 < size--) if (last.get(size)) follow[size].or(first);
  }
  /**
   * Calling this with deferEvents(true) will queue all property change events until a subsequent
   * call to deferEvents(false). This should be used at the beginning of a batch of related changes
   * to prevent duplicate property change events from being sent.
   *
   * @param shouldQueue
   */
  private void deferEvents(boolean shouldQueue) {
    queueEvents = shouldQueue;

    if (queueEvents == false) {
      // do not use nextSetBit, to allow compilation against JCL Foundation (bug 80053)
      for (int i = 0, n = queuedEvents.size(); i < n; ++i) {
        if (queuedEvents.get(i)) {
          firePropertyChange(i);
          queuedEvents.clear(i);
        }
      }
    }
  }
  /**
   * Test of ShortestPathFingerprinter method
   *
   * @throws InvalidSmilesException
   * @throws CDKException
   */
  @Test
  public void testGenerateFingerprint() throws InvalidSmilesException, CDKException {

    String smiles = "CCCCC1C(=O)N(N(C1=O)C1=CC=CC=C1)C1=CC=CC=C1";
    SmilesParser smilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    IAtomContainer molecule = smilesParser.parseSmiles(smiles);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
    Aromaticity.cdkLegacy().apply(molecule);
    ShortestPathFingerprinter fingerprint = new ShortestPathFingerprinter(1024);
    BitSet fingerprint1;
    fingerprint1 = fingerprint.getBitFingerprint(molecule).asBitSet();
    org.junit.Assert.assertEquals(125, fingerprint1.cardinality());
    org.junit.Assert.assertEquals(1024, fingerprint1.size());
  }
  /**
   * Get variables that are live on entry to the cfg. This is the case for closures which access
   * variables from the parent scope.
   *
   * <p>sum = 0; a.each { |i| sum += i }; return sum
   *
   * <p>In the code snippet above, 'sum' is live on entry to the closure
   */
  public List<Variable> getVarsLiveOnScopeEntry() {
    List<Variable> liveVars = new ArrayList<Variable>();
    BitSet liveIn = getFlowGraphNode(getScope().cfg().getEntryBB()).getLiveOutBitSet();

    for (int i = 0; i < liveIn.size(); i++) {
      if (!liveIn.get(i)) continue;

      Variable v = getVariable(i);
      liveVars.add(v);
      // System.out.println("variable " + v + " is live on entry!");
    }

    return liveVars;
  }
예제 #15
0
  BitSet singleBitDiff(BitSet x, BitSet y) {
    BitSet t = new BitSet(x.length());
    t.or(x);
    t.flip(0, t.size());
    t.and(y);

    switch (t.cardinality()) {
      case 0:
        return t;
      case 1:
        return t;
      default:
        return new BitSet();
    }
  }
예제 #16
0
  public void print() {
    try {
      PrintWriter writer = new PrintWriter(new FileOutputStream(new File("new.txt"), true));
      int i;

      writer.print("use[" + index + "] = { ");
      for (i = 0; i < use.size(); ++i) if (use.get(i)) writer.print("" + i + " ");
      writer.println("}");
      writer.print("def[" + index + "] = { ");
      for (i = 0; i < def.size(); ++i) if (def.get(i)) writer.print("" + i + " ");
      writer.println("}\n");

      writer.print("in[" + index + "] = { ");
      for (i = 0; i < in.size(); ++i) if (in.get(i)) writer.print("" + i + " ");
      writer.println("}");

      writer.print("out[" + index + "] = { ");
      for (i = 0; i < out.size(); ++i) if (out.get(i)) writer.print("" + i + " ");
      writer.println("}\n");
      writer.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #17
0
    Coverage getCoveredBasicBlocks() {
      BitSet bExec = getExecutedInsn();
      BitSet bb = getBasicBlocks();
      int nCov = 0;

      if (excludeHandlers) {
        BitSet handlers = getHandlers();
        bb.and(handlers);
      }

      if (bExec != null) {
        BitSet bCov = new BitSet(bb.size());
        bCov.or(bb);
        bCov.and(bExec);
        nCov = bCov.cardinality();
      }

      return new Coverage(bb.cardinality(), nCov);
    }
예제 #18
0
    Coverage getCoveredBranches() {
      BitSet b = getBranches();
      int nTotal = b.cardinality();
      int nCovered = 0;

      if (branchTrue != null) {
        int n = branchTrue.size();

        for (int i = 0; i < n; i++) {
          boolean cTrue = branchTrue.get(i);
          boolean cFalse = branchFalse.get(i);

          if (cTrue && cFalse) {
            nCovered++;
          }
        }
      }

      return new Coverage(nTotal, nCovered);
    }
예제 #19
0
 void doPrevSetBit(BitSet a, FixedBitSet b) {
   int aa = a.size() + random.nextInt(100);
   int bb = aa;
   do {
     // aa = a.prevSetBit(aa-1);
     aa--;
     while ((aa >= 0) && (!a.get(aa))) {
       aa--;
     }
     if (b.length() == 0) {
       bb = -1;
     } else if (bb > b.length() - 1) {
       bb = b.prevSetBit(b.length() - 1);
     } else if (bb < 1) {
       bb = -1;
     } else {
       bb = bb >= 1 ? b.prevSetBit(bb - 1) : -1;
     }
     assertEquals(aa, bb);
   } while (aa >= 0);
 }
예제 #20
0
  /**
   * Removing a subscription from the structure.
   *
   * @param subscription The subscription to remove
   */
  @Override
  public void removeSubscription(AndesSubscription subscription) {
    int subscriptionIndex = subscriptionList.indexOf(subscription);

    if (subscriptionIndex > -1) {
      for (Map<String, BitSet> constituentTable : constituentTables) {
        for (Map.Entry<String, BitSet> constituentRow : constituentTable.entrySet()) {
          // For every row create a new BitSet with the values for the removed subscription removed
          String constituent = constituentRow.getKey();
          BitSet bitSet = constituentRow.getValue();
          BitSet newBitSet = new BitSet();

          int bitIndex = 0;

          for (int i = 0; i < bitSet.size(); i++) {
            if (bitIndex == i) {
              // If the this is the index to remove then skip this round
              bitIndex++;
            }
            newBitSet.set(i, bitSet.get(bitIndex));
            bitIndex++;
          }

          constituentTable.put(constituent, newBitSet);
        }
      }

      // Remove the subscription from subscription list
      subscriptionList.remove(subscriptionIndex);
    } else {
      log.warn(
          "Subscription for destination : "
              + subscription.getSubscribedDestination()
              + " is not found to "
              + "remove");
    }
  }
예제 #21
0
  public void testItCreatesA2024BitFingerprint() {
    BitSet fingerprint = fingerprinter.getFingerprint(Molecules.createBenzene());

    assertEquals(1024, fingerprint.size());
  }
예제 #22
0
 void foo(BitSet o) {
   BitSet o2 = o;
   int foo = 0;
   foo = o2.size();
 }
  /**
   * Add exceptions from the BitSet array to the exceptions list. Assumes that the BitSet[0]
   * corresponds to this.bitSetVersion. This scans the bitset looking for gaps that are recorded as
   * RVV exceptions. The scan terminates at numBits or when the last set bit is found. The bitSet is
   * adjusted and a new bitSetVersion is established.
   *
   * @param newVersion the desired new bitSetVersion, which may be > the max representable in the
   *     bitset
   * @param numBits the desired number of bits to flush from the bitset
   */
  private void addBitSetExceptions(int numBits, long newVersion) {
    final boolean isDebugEnabled_RVV = logger.isTraceEnabled(LogMarker.RVV);
    int lastSetIndex = -1;

    if (isDebugEnabled_RVV) {
      logger.trace(LogMarker.RVV, "addBitSetExceptions({},{})", numBits, newVersion);
    }

    for (int idx = 0; idx < numBits; ) {
      int nextMissingIndex = this.bitSet.nextClearBit(idx);
      if (nextMissingIndex < 0) {
        break;
      }

      lastSetIndex = nextMissingIndex - 1;

      int nextReceivedIndex = this.bitSet.nextSetBit(nextMissingIndex + 1);
      long nextReceivedVersion = -1;
      if (nextReceivedIndex > 0) {
        lastSetIndex = nextReceivedIndex;
        nextReceivedVersion = (long) (nextReceivedIndex) + this.bitSetVersion;
        idx = nextReceivedIndex + 1;
        if (isDebugEnabled_RVV) {
          logger.trace(
              LogMarker.RVV,
              "found gap in bitSet: missing bit at index={}; next set index={}",
              nextMissingIndex,
              nextReceivedIndex);
        }
      } else {
        // We can't flush any more bits from the bit set because there
        // are no more received versions
        if (isDebugEnabled_RVV) {
          logger.trace(
              LogMarker.RVV,
              "terminating flush at bit {} because of missing entries",
              lastSetIndex);
        }
        this.bitSetVersion += lastSetIndex;
        this.bitSet.clear();
        if (lastSetIndex != -1) {
          this.bitSet.set(0);
        }
        return;
      }
      long nextMissingVersion = Math.max(1, nextMissingIndex + this.bitSetVersion);
      if (nextReceivedVersion > nextMissingVersion) {
        addException(nextMissingVersion - 1, nextReceivedVersion);
        if (isDebugEnabled_RVV) {
          logger.trace(
              LogMarker.RVV,
              "Added rvv exception e<rv{} - rv{}>",
              (nextMissingVersion - 1),
              nextReceivedVersion);
        }
      }
    }
    this.bitSet = this.bitSet.get(lastSetIndex, Math.max(lastSetIndex + 1, bitSet.size()));
    if (lastSetIndex > 0) {
      this.bitSetVersion = this.bitSetVersion + (long) lastSetIndex;
    }
  }
예제 #24
0
파일: Warehouse.java 프로젝트: cnhans/tdw
 static boolean needsEscaping(char c) {
   return c >= 0 && c < charToEscape.size() && charToEscape.get(c);
 }
예제 #25
0
  private BlockNode processSwitch(
      IRegion currentRegion, BlockNode block, SwitchNode insn, RegionStack stack) {
    SwitchRegion sw = new SwitchRegion(currentRegion, block);
    currentRegion.getSubBlocks().add(sw);

    int len = insn.getTargets().length;
    // sort by target
    Map<Integer, List<Object>> casesMap = new LinkedHashMap<Integer, List<Object>>(len);
    for (int i = 0; i < len; i++) {
      Object key = insn.getKeys()[i];
      int targ = insn.getTargets()[i];
      List<Object> keys = casesMap.get(targ);
      if (keys == null) {
        keys = new ArrayList<Object>(2);
        casesMap.put(targ, keys);
      }
      keys.add(key);
    }

    Map<BlockNode, List<Object>> blocksMap = new LinkedHashMap<BlockNode, List<Object>>(len);
    for (Map.Entry<Integer, List<Object>> entry : casesMap.entrySet()) {
      BlockNode c = getBlockByOffset(entry.getKey(), block.getSuccessors());
      assert c != null;
      blocksMap.put(c, entry.getValue());
    }
    BlockNode defCase = getBlockByOffset(insn.getDefaultCaseOffset(), block.getSuccessors());
    if (defCase != null) {
      blocksMap.remove(defCase);
    }
    LoopInfo loop = mth.getLoopForBlock(block);

    Map<BlockNode, BlockNode> fallThroughCases = new LinkedHashMap<BlockNode, BlockNode>();

    List<BlockNode> basicBlocks = mth.getBasicBlocks();
    BitSet outs = new BitSet(basicBlocks.size());
    outs.or(block.getDomFrontier());
    for (BlockNode s : block.getCleanSuccessors()) {
      BitSet df = s.getDomFrontier();
      // fall through case block
      if (df.cardinality() > 1) {
        if (df.cardinality() > 2) {
          LOG.debug("Unexpected case pattern, block: {}, mth: {}", s, mth);
        } else {
          BlockNode first = basicBlocks.get(df.nextSetBit(0));
          BlockNode second = basicBlocks.get(df.nextSetBit(first.getId() + 1));
          if (second.getDomFrontier().get(first.getId())) {
            fallThroughCases.put(s, second);
            df = new BitSet(df.size());
            df.set(first.getId());
          } else if (first.getDomFrontier().get(second.getId())) {
            fallThroughCases.put(s, first);
            df = new BitSet(df.size());
            df.set(second.getId());
          }
        }
      }
      outs.or(df);
    }
    outs.clear(block.getId());
    if (loop != null) {
      outs.clear(loop.getStart().getId());
    }

    stack.push(sw);
    stack.addExits(BlockUtils.bitSetToBlocks(mth, outs));

    // check cases order if fall through case exists
    if (!fallThroughCases.isEmpty()) {
      if (isBadCasesOrder(blocksMap, fallThroughCases)) {
        LOG.debug("Fixing incorrect switch cases order, method: {}", mth);
        blocksMap = reOrderSwitchCases(blocksMap, fallThroughCases);
        if (isBadCasesOrder(blocksMap, fallThroughCases)) {
          LOG.error("Can't fix incorrect switch cases order, method: {}", mth);
          mth.add(AFlag.INCONSISTENT_CODE);
        }
      }
    }

    // filter 'out' block
    if (outs.cardinality() > 1) {
      // remove exception handlers
      BlockUtils.cleanBitSet(mth, outs);
    }
    if (outs.cardinality() > 1) {
      // filter loop start and successors of other blocks
      for (int i = outs.nextSetBit(0); i >= 0; i = outs.nextSetBit(i + 1)) {
        BlockNode b = basicBlocks.get(i);
        outs.andNot(b.getDomFrontier());
        if (b.contains(AFlag.LOOP_START)) {
          outs.clear(b.getId());
        } else {
          for (BlockNode s : b.getCleanSuccessors()) {
            outs.clear(s.getId());
          }
        }
      }
    }

    if (loop != null && outs.cardinality() > 1) {
      outs.clear(loop.getEnd().getId());
    }
    if (outs.cardinality() == 0) {
      // one or several case blocks are empty,
      // run expensive algorithm for find 'out' block
      for (BlockNode maybeOut : block.getSuccessors()) {
        boolean allReached = true;
        for (BlockNode s : block.getSuccessors()) {
          if (!isPathExists(s, maybeOut)) {
            allReached = false;
            break;
          }
        }
        if (allReached) {
          outs.set(maybeOut.getId());
          break;
        }
      }
    }
    BlockNode out = null;
    if (outs.cardinality() == 1) {
      out = basicBlocks.get(outs.nextSetBit(0));
      stack.addExit(out);
    } else if (loop == null && outs.cardinality() > 1) {
      LOG.warn("Can't detect out node for switch block: {} in {}", block, mth);
    }
    if (loop != null) {
      // check if 'continue' must be inserted
      BlockNode end = loop.getEnd();
      if (out != end && out != null) {
        insertContinueInSwitch(block, out, end);
      }
    }

    if (!stack.containsExit(defCase)) {
      sw.setDefaultCase(makeRegion(defCase, stack));
    }
    for (Entry<BlockNode, List<Object>> entry : blocksMap.entrySet()) {
      BlockNode caseBlock = entry.getKey();
      if (stack.containsExit(caseBlock)) {
        // empty case block
        sw.addCase(entry.getValue(), new Region(stack.peekRegion()));
      } else {
        BlockNode next = fallThroughCases.get(caseBlock);
        stack.addExit(next);
        Region caseRegion = makeRegion(caseBlock, stack);
        stack.removeExit(next);
        if (next != null) {
          next.add(AFlag.FALL_THROUGH);
          caseRegion.add(AFlag.FALL_THROUGH);
        }
        sw.addCase(entry.getValue(), caseRegion);
        // 'break' instruction will be inserted in RegionMakerVisitor.PostRegionVisitor
      }
    }

    stack.pop();
    return out;
  }
예제 #26
0
 public int getNrWeeks() {
   return getNrWeeks(0, iWeekCode.size() - 1);
 }