public static void main(String[] args) {

    // Initialized to false by default
    BitSet bitmap = new BitSet(10000000);
    BufferedReader br = null;
    String line = null;

    try {

      br = new BufferedReader(new FileReader(args[0]));

      // Setting the bitmap for the input values
      while ((line = br.readLine()) != null) {
        int num = Integer.parseInt(line);
        bitmap.set(num);
      }

      // Outputting the sorted value
      for (int i = 0; i < bitmap.length(); i++) {

        if (bitmap.get(i)) {
          System.out.println(bitmap.get(i));
        }
      }
    } catch (Exception e) {

      e.printStackTrace();
    }
  }
  /**
   * Updates the interal bitset from <code>iterator</code>. This will set <code>validMask</code> to
   * true if <code>iterator</code> is non-null.
   */
  private void updateMask(AttributedCharacterIterator iterator) {
    if (iterator != null) {
      validMask = true;
      this.iterator = iterator;

      // Update the literal mask
      if (literalMask == null) {
        literalMask = new BitSet();
      } else {
        for (int counter = literalMask.length() - 1; counter >= 0; counter--) {
          literalMask.clear(counter);
        }
      }

      iterator.first();
      while (iterator.current() != CharacterIterator.DONE) {
        Map attributes = iterator.getAttributes();
        boolean set = isLiteral(attributes);
        int start = iterator.getIndex();
        int end = iterator.getRunLimit();

        while (start < end) {
          if (set) {
            literalMask.set(start);
          } else {
            literalMask.clear(start);
          }
          start++;
        }
        iterator.setIndex(start);
      }
    }
  }
Esempio n. 3
0
 public TOE(String s, int Nc, int Mc) {
   this(Nc, Mc);
   char[] C = s.toCharArray();
   for (int i = 0; i < N; i++)
     for (int j = 0; j < M; j++)
       if (C[i * M + j] == '1') G.set(i * M + j);
       else G.clear(i * M + j);
 }
Esempio n. 4
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;
 }
Esempio n. 5
0
  /**
   * setup BitMap
   *
   * @exception ISOException
   */
  public void recalcBitMap() throws ISOException {
    if (!dirty) return;

    int mf = Math.min(getMaxField(), 192);

    BitSet bmap = new BitSet(((mf + 62) >> 6) << 6);
    for (int i = 1; i <= mf; i++) if ((fields.get(i)) != null) bmap.set(i);
    set(new ISOBitMap(-1, bmap));
    dirty = false;
  }
 protected void doDamageRemoveTest(SimulatedArchivalUnit sau) throws Exception {
   /* Cache the file again; this time the damage should be gone */
   String file = sau.getUrlRoot() + DAMAGED_CACHED_URL;
   UrlCacher uc = sau.makeUrlCacher(file);
   BitSet fetchFlags = new BitSet();
   fetchFlags.set(UrlCacher.REFETCH_FLAG);
   uc.setFetchFlags(fetchFlags);
   uc.cache();
   checkUrlContent(sau, DAMAGED_CACHED_URL, 2, 2, 2, false, false);
 }
  @Test
  public void flipTest1() {
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();

    rb.flip(100000, 200000); // in-place on empty bitmap
    final int rbcard = rb.getCardinality();
    Assert.assertEquals(100000, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 100000; i < 200000; ++i) bs.set(i);
    Assert.assertTrue(equals(bs, rb));
  }
 /**
  * Update the piece availabilities for a given peer
  *
  * @param peerID String
  * @param has BitSet
  */
 public synchronized void peerAvailability(String peerID, BitSet has) {
   this.peerAvailabilies.put(peerID, has);
   BitSet interest = (BitSet) (has.clone());
   interest.andNot(this.isComplete);
   DownloadTask dt = this.task.get(peerID);
   if (dt != null) {
     if (interest.cardinality() > 0 && !dt.peer.isInteresting()) {
       dt.ms.addMessageToQueue(new Message_PP(PeerProtocol.INTERESTED, 2));
       dt.peer.setInteresting(true);
     }
   }
   dt = null;
 }
  @Test
  public void flipTest1A() {
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();

    final MutableRoaringBitmap rb1 = MutableRoaringBitmap.flip(rb, 100000, 200000);
    final int rbcard = rb1.getCardinality();
    Assert.assertEquals(100000, rbcard);
    Assert.assertEquals(0, rb.getCardinality());

    final BitSet bs = new BitSet();
    Assert.assertTrue(equals(bs, rb)); // still empty?
    for (int i = 100000; i < 200000; ++i) bs.set(i);
    Assert.assertTrue(equals(bs, rb1));
  }
 public void compactEnd() {
   final int compactSize = compactIndexUsed.cardinality();
   if (compactSize == map.size()) {
     return;
   }
   for (int i = 1; i < list.size(); i++) {
     if (compactIndexUsed.get(i)) continue;
     // to be removed
     T t = list.get(i);
     list.set(i, null);
     map.remove(t);
     if (addPosition > i) addPosition = i;
   }
 }
  @Test
  public void flipTest6A() {
    System.out.println("FlipTest6A");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    final MutableRoaringBitmap rb1 = MutableRoaringBitmap.flip(rb, 100000, 132000);
    final MutableRoaringBitmap rb2 = MutableRoaringBitmap.flip(rb1, 99000, 2 * 65536);
    final int rbcard = rb2.getCardinality();

    Assert.assertEquals(1928, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 99000; i < 100000; ++i) bs.set(i);
    for (int i = 2 * 65536; i < 132000; ++i) bs.set(i);
    Assert.assertTrue(equals(bs, rb2));
  }
  @Test
  public void flipTest3() {
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();

    rb.flip(100000, 200000); // got 100k-199999
    rb.flip(100000, 199991); // give back 100k-199990
    final int rbcard = rb.getCardinality();

    Assert.assertEquals(9, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 199991; i < 200000; ++i) bs.set(i);

    Assert.assertTrue(equals(bs, rb));
  }
  @Test
  public void flipTest3A() {
    System.out.println("FlipTest3A");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    final MutableRoaringBitmap rb1 = MutableRoaringBitmap.flip(rb, 100000, 200000);
    final MutableRoaringBitmap rb2 = MutableRoaringBitmap.flip(rb1, 100000, 199991);
    final int rbcard = rb2.getCardinality();

    Assert.assertEquals(9, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 199991; i < 200000; ++i) bs.set(i);

    Assert.assertTrue(equals(bs, rb2));
  }
  @Test
  public void flipTest2A() {
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();

    final MutableRoaringBitmap rb1 = MutableRoaringBitmap.flip(rb, 100000, 100000);
    rb.add(1); // will not affect rb1 (no shared container)
    final int rbcard = rb1.getCardinality();
    Assert.assertEquals(0, rbcard);
    Assert.assertEquals(1, rb.getCardinality());

    final BitSet bs = new BitSet();
    Assert.assertTrue(equals(bs, rb1));
    bs.set(1);
    Assert.assertTrue(equals(bs, rb));
  }
 @Override
 public void prob1step(
     BitSet subset, BitSet u, BitSet v, boolean forall1, boolean forall2, BitSet result) {
   boolean b1, b2, b3;
   for (int i : new IterableStateSet(subset, numStates)) {
     b1 = forall1; // there exists or for all player 1 choices
     for (DistributionSet distrs : trans.get(i)) {
       b2 = forall2; // there exists or for all player 2 choices
       for (Distribution distr : distrs) {
         b3 = distr.containsOneOf(v) && distr.isSubsetOf(u);
         if (forall2) {
           if (!b3) b2 = false;
         } else {
           if (b3) b2 = true;
         }
       }
       if (forall1) {
         if (!b2) b1 = false;
       } else {
         if (b2) b1 = true;
       }
     }
     result.set(i, b1);
   }
 }
Esempio n. 16
0
 static int get(BitSet e, int n, int m) {
   if (e.last == n * m) {
     return e.prevSetBit(e.last);
   } else {
     return e.last;
   }
 }
 @Override
 public void checkForDeadlocks(BitSet except) throws PrismException {
   for (int i = 0; i < numStates; i++) {
     if (trans.get(i).isEmpty() && (except == null || !except.get(i)))
       throw new PrismException("STPG has a deadlock in state " + i);
   }
   // TODO: Check for empty distributions sets too?
 }
  @Test
  public void flipTest7A() { // within 1 word, first container
    System.out.println("FlipTest7A");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    final MutableRoaringBitmap rb1 = MutableRoaringBitmap.flip(rb, 650, 132000);
    final MutableRoaringBitmap rb2 = MutableRoaringBitmap.flip(rb1, 648, 651);
    final int rbcard = rb2.getCardinality();

    // 648, 649, 651-131999

    Assert.assertEquals(132000 - 651 + 2, rbcard);

    final BitSet bs = new BitSet();
    bs.set(648);
    bs.set(649);
    for (int i = 651; i < 132000; ++i) bs.set(i);
    Assert.assertTrue(equals(bs, rb2));
  }
  @Test
  public void flipTest6() { // fits evenly on big end, multiple containers
    System.out.println("FlipTest6");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    rb.flip(100000, 132000);
    rb.flip(99000, 2 * 65536);
    final int rbcard = rb.getCardinality();

    // 99000 to 99999 are 1000 1s
    // 131072 to 131999 are 928 1s

    Assert.assertEquals(1928, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 99000; i < 100000; ++i) bs.set(i);
    for (int i = 2 * 65536; i < 132000; ++i) bs.set(i);
    Assert.assertTrue(equals(bs, rb));
  }
  // The default constructor
  public CrownCounselIndexGetList_Access() {
    String traceProp = null;
    if ((traceProp = System.getProperty(HOSTPUBLISHER_ACCESSBEAN_TRACE)) != null) {
      try {
        Class c = Class.forName(HOSTPUBLISHER_ACCESSTRACE_OBJECT);
        Method m = c.getMethod("getInstance", null);
        o = m.invoke(null, null);
        Class parmTypes[] = {String.class};
        m = c.getMethod("initTrace", parmTypes);
        Object initTraceArgs[] = {traceProp};
        BitSet tracingBS = (BitSet) m.invoke(o, initTraceArgs);
        Class parmTypes2[] = {Object.class, String.class};
        traceMethod = c.getMethod("trace", parmTypes2);
        tracing = tracingBS.get(HOSTPUBLISHER_ACCESSBEAN_TRACING);
        Class parmTypes3[] = {String.class, HttpSession.class};
        auditMethod = c.getMethod("audit", parmTypes3);
        auditing = tracingBS.get(HOSTPUBLISHER_ACCESSBEAN_AUDITING);
      } catch (Exception e) {
        // no tracing will be done
        System.err.println(
            (new Date(System.currentTimeMillis())).toString()
                + " hostpublisher.accessbean.trace="
                + traceProp
                + ", Exception="
                + e.toString());
      }
    }

    if (tracing == true) {
      traceArgs[0] = this;
      traceArgs[1] = " CrownCounselIndexGetList_Access()";
      try {
        traceMethod.invoke(o, traceArgs);
      } catch (Exception x) {
        System.err.println(
            (new Date(System.currentTimeMillis())).toString()
                + " traceMethod.invoke(null, traceArgs)"
                + ", Exception="
                + x.toString());
      }
    }
    inputProps = new CrownCounselIndexGetList_Properties();
    return;
  }
  @Test
  public void flipTest5() { // fits evenly on small end, multiple
    // containers
    System.out.println("FlipTest5");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    rb.flip(100000, 132000);
    rb.flip(65536, 120000);
    final int rbcard = rb.getCardinality();

    // 65536 to 99999 are 1s
    // 120000 to 131999

    Assert.assertEquals(46464, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 65536; i < 100000; ++i) bs.set(i);
    for (int i = 120000; i < 132000; ++i) bs.set(i);
    Assert.assertTrue(equals(bs, rb));
  }
  @Test
  public void flipTest4() { // fits evenly on both ends
    System.out.println("FlipTest4");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    rb.flip(100000, 200000); // got 100k-199999
    rb.flip(65536, 4 * 65536);
    final int rbcard = rb.getCardinality();

    // 65536 to 99999 are 1s
    // 200000 to 262143 are 1s: total card

    Assert.assertEquals(96608, rbcard);

    final BitSet bs = new BitSet();
    for (int i = 65536; i < 100000; ++i) bs.set(i);
    for (int i = 200000; i < 262144; ++i) bs.set(i);

    Assert.assertTrue(equals(bs, rb));
  }
Esempio n. 23
0
 public String toString() {
   String s = "";
   for (int i = 0; i < N; i++) {
     for (int j = 0; j < M; j++)
       if (G.get(i * M + j)) s += "1";
       else s += "0";
     s += "\n";
   }
   s += (mx + "," + my + "\n");
   return s;
 }
Esempio n. 24
0
File: A.java Progetto: t8m8/AtCoder
  static void solve() {
    int r = in.nextInt();
    int c = in.nextInt();
    int sy = in.nextInt() - 1;
    int sx = in.nextInt() - 1;
    int gy = in.nextInt() - 1;
    int gx = in.nextInt() - 1;
    char[][] t = new char[r][c];
    for (int i = 0; i < r; i++) {
      t[i] = in.next().toCharArray();
    }

    ArrayDeque<int[]> que = new ArrayDeque<>();
    BitSet visited = new BitSet();

    que.add(new int[] {sy, sx, 0});
    visited.set(sy * c + sx);

    int[] dx = {0, 1, 0, -1};
    int[] dy = {1, 0, -1, 0};
    int[][] dist = new int[r][c];

    while (!que.isEmpty()) {
      int[] p = que.pollFirst();
      int cy = p[0];
      int cx = p[1];
      int d = p[2];
      dist[cy][cx] = d;

      for (int i = 0; i < 4; i++) {
        int ny = cy + dy[i];
        int nx = cx + dx[i];
        if (ny < 0 || nx < 0 || r <= ny || c <= nx) continue;
        if (visited.get(ny * c + nx) || t[ny][nx] == '#') continue;
        que.add(new int[] {ny, nx, d + 1});
        visited.set(ny * c + nx);
      }
    }

    out.println(dist[gy][gx]);
  }
Esempio n. 25
0
 public List<TOE> next() {
   List<TOE> ret = new LinkedList<TOE>();
   for (int i = 0; i < N; i++)
     for (int j = 0; j < M; j++)
     // if(G.get(i*M+j))
     {
       BitSet Gnew = (BitSet) (G.clone());
       int[] x = {0, 1, -1, 0, 0};
       int[] y = {0, 0, 0, 1, -1};
       for (int k = 0; k < 5; k++) {
         int xi = x[k];
         int yi = y[k];
         if ((i + xi) >= 0 && (i + xi) < N && (j + yi) >= 0 && (j + yi) < M) {
           Gnew.flip((i + xi) * M + (j + yi));
         }
       }
       TOE to = new TOE(N, M, Gnew);
       to.mx = i;
       to.my = j;
       ret.add(to);
     }
   return ret;
 }
Esempio n. 26
0
  /**
   * It adds a sequence from an array of string that we have to interpret
   *
   * @param integers
   * @param sequenceID
   */
  public void addSequence(String[] integers, int sequenceID) {
    long timestamp = -1;
    Sequence sequence = new Sequence(sequences.size());
    sequence.setID(sequenceID);
    Itemset itemset = new Itemset();
    int inicio = 0;
    Map<Item, Boolean> counted = new HashMap<Item, Boolean>();

    for (int i = inicio; i < integers.length; i++) {
      if (integers[i].codePointAt(0) == '<') { // Timestamp
        String value = integers[i].substring(1, integers[i].length() - 1);
        timestamp = Long.parseLong(value);
        itemset.setTimestamp(timestamp);
      } else if (integers[i].equals("-1")) { // end of an itemset
        long time = itemset.getTimestamp() + 1;
        sequence.addItemset(itemset);
        itemset = new Itemset();
        itemset.setTimestamp(time);
      } else if (integers[i].equals("-2")) { // end of a sequence
        sequences.add(sequence);
      } else {
        // extract the value for an item
        Item item = itemFactory.getItem(Integer.parseInt(integers[i]));
        if (counted.get(item) == null) {
          counted.put(item, Boolean.TRUE);
          BitSet appearances = frequentItems.get(item);
          if (appearances == null) {
            appearances = new BitSet();
            frequentItems.put(item, appearances);
          }
          appearances.set(sequence.getId());
        }
        itemset.addItem(item);
      }
    }
  }
  @Test
  public void flipTestBigA() {
    final int numCases = 1000;
    final BitSet bs = new BitSet();
    final Random r = new Random(3333);
    int checkTime = 2;
    MutableRoaringBitmap rb1 = new MutableRoaringBitmap(), rb2 = null; // alternate
    // between
    // them

    for (int i = 0; i < numCases; ++i) {
      final int start = r.nextInt(65536 * 20);
      int end = r.nextInt(65536 * 20);
      if (r.nextDouble() < 0.1) end = start + r.nextInt(100);

      if ((i & 1) == 0) {
        rb2 = MutableRoaringBitmap.flip(rb1, start, end);
        // tweak the other, catch bad sharing
        rb1.flip(r.nextInt(65536 * 20), r.nextInt(65536 * 20));
      } else {
        rb1 = MutableRoaringBitmap.flip(rb2, start, end);
        rb2.flip(r.nextInt(65536 * 20), r.nextInt(65536 * 20));
      }

      if (start < end) bs.flip(start, end); // throws exception
      // otherwise
      // insert some more ANDs to keep things sparser
      if (r.nextDouble() < 0.2 && (i & 1) == 0) {
        final MutableRoaringBitmap mask = new MutableRoaringBitmap();
        final BitSet mask1 = new BitSet();
        final int startM = r.nextInt(65536 * 20);
        final int endM = startM + 100000;
        mask.flip(startM, endM);
        mask1.flip(startM, endM);
        mask.flip(0, 65536 * 20 + 100000);
        mask1.flip(0, 65536 * 20 + 100000);
        rb2.and(mask);
        bs.and(mask1);
      }

      if (i > checkTime) {
        System.out.println("check after " + i + ", card = " + rb2.getCardinality());
        final MutableRoaringBitmap rb = (i & 1) == 0 ? rb2 : rb1;
        final boolean status = equals(bs, rb);
        Assert.assertTrue(status);
        checkTime *= 1.5;
      }
    }
  }
Esempio n. 28
0
  /**
   * @return an iterator over all tuples on this page (calling remove on this iterator throws an
   *     UnsupportedOperationException) (note that this iterator shouldn't return tuples in empty
   *     slots!)
   */
  public Iterator<Tuple> iterator() {
    // hdj
    final BitSet bitset = BitSet.valueOf(header);
    Iterator<Tuple> it =
        new Iterator<Tuple>() {
          int currentIndex = -1;

          public boolean hasNext() {
            if (bitset.nextSetBit(currentIndex + 1) == -1) return false;
            else return true;
          }

          public Tuple next() {
            currentIndex = bitset.nextSetBit(currentIndex + 1);
            return tuples[currentIndex];
          }

          public void remove() {
            throw new UnsupportedOperationException();
          }
        };

    return it;
  }
  @Test
  public void flipTestBig() {
    final int numCases = 1000;
    System.out.println("flipTestBig for " + numCases + " tests");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    final BitSet bs = new BitSet();
    final Random r = new Random(3333);
    int checkTime = 2;

    for (int i = 0; i < numCases; ++i) {
      final int start = r.nextInt(65536 * 20);
      int end = r.nextInt(65536 * 20);
      if (r.nextDouble() < 0.1) end = start + r.nextInt(100);
      rb.flip(start, end);
      if (start < end) bs.flip(start, end); // throws exception
      // otherwise
      // insert some more ANDs to keep things sparser
      if (r.nextDouble() < 0.2) {
        final MutableRoaringBitmap mask = new MutableRoaringBitmap();
        final BitSet mask1 = new BitSet();
        final int startM = r.nextInt(65536 * 20);
        final int endM = startM + 100000;
        mask.flip(startM, endM);
        mask1.flip(startM, endM);
        mask.flip(0, 65536 * 20 + 100000);
        mask1.flip(0, 65536 * 20 + 100000);
        rb.and(mask);
        bs.and(mask1);
      }
      // see if we can detect incorrectly shared containers
      if (r.nextDouble() < 0.1) {
        final MutableRoaringBitmap irrelevant = MutableRoaringBitmap.flip(rb, 10, 100000);
        irrelevant.flip(5, 200000);
        irrelevant.flip(190000, 260000);
      }
      if (i > checkTime) {
        Assert.assertTrue(equals(bs, rb));
        checkTime *= 1.5;
      }
    }
  }
  public void testFormat() throws Exception {
    JobConf job = new JobConf(conf);
    FileSystem fs = FileSystem.getLocal(conf);
    Path dir = new Path(System.getProperty("test.build.data", ".") + "/mapred");
    Path file = new Path(dir, "test.seq");

    Reporter reporter = Reporter.NULL;

    int seed = new Random().nextInt();
    // LOG.info("seed = "+seed);
    Random random = new Random(seed);

    fs.delete(dir, true);

    FileInputFormat.setInputPaths(job, dir);

    // for a variety of lengths
    for (int length = 0; length < MAX_LENGTH; length += random.nextInt(MAX_LENGTH / 10) + 1) {

      // LOG.info("creating; entries = " + length);

      // create a file with length entries
      SequenceFile.Writer writer =
          SequenceFile.createWriter(fs, conf, file, IntWritable.class, BytesWritable.class);
      try {
        for (int i = 0; i < length; i++) {
          IntWritable key = new IntWritable(i);
          byte[] data = new byte[random.nextInt(10)];
          random.nextBytes(data);
          BytesWritable value = new BytesWritable(data);
          writer.append(key, value);
        }
      } finally {
        writer.close();
      }

      // try splitting the file in a variety of sizes
      InputFormat<IntWritable, BytesWritable> format =
          new SequenceFileInputFormat<IntWritable, BytesWritable>();
      IntWritable key = new IntWritable();
      BytesWritable value = new BytesWritable();
      for (int i = 0; i < 3; i++) {
        int numSplits = random.nextInt(MAX_LENGTH / (SequenceFile.SYNC_INTERVAL / 20)) + 1;
        // LOG.info("splitting: requesting = " + numSplits);
        InputSplit[] splits = format.getSplits(job, numSplits);
        // LOG.info("splitting: got =        " + splits.length);

        // check each split
        BitSet bits = new BitSet(length);
        for (int j = 0; j < splits.length; j++) {
          RecordReader<IntWritable, BytesWritable> reader =
              format.getRecordReader(splits[j], job, reporter);
          try {
            int count = 0;
            while (reader.next(key, value)) {
              // if (bits.get(key.get())) {
              // LOG.info("splits["+j+"]="+splits[j]+" : " +
              // key.get());
              // LOG.info("@"+reader.getPos());
              // }
              assertFalse("Key in multiple partitions.", bits.get(key.get()));
              bits.set(key.get());
              count++;
            }
            // LOG.info("splits["+j+"]="+splits[j]+" count=" +
            // count);
          } finally {
            reader.close();
          }
        }
        assertEquals("Some keys in no partition.", length, bits.cardinality());
      }
    }
  }