All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.simiacryptus.bitset.RunLengthBitsCollection Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package com.simiacryptus.bitset;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;

import com.simiacryptus.binary.BitInputStream;
import com.simiacryptus.binary.BitOutputStream;
import com.simiacryptus.binary.Bits;

public class RunLengthBitsCollection extends
    BitsCollection>
{
  public RunLengthBitsCollection(final int bitDepth)
  {
    super(bitDepth, new HashMap());
  }
  
  @Override
  public void read(final BitInputStream in) throws IOException
  {
    final int size = (int) in.read(32).toLong();
    for (int i = 0; i < size; i++)
    {
      final Bits bits = in.read(this.bitDepth);
      final int count = (int) in.read(32).toLong();
      this.map.put(bits, new AtomicInteger(count));
    }
  }
  
  @Override
  public void write(final BitOutputStream out) throws IOException
  {
    out.write(new Bits(this.getList().size(), 32));
    for (final Entry e : this.map.entrySet())
    {
      out.write(e.getKey());
      out.write(new Bits(e.getValue().get(), 32));
    }
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy