Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Word Aligned Hybrid (WAH) Compression for BitArrays

, 6 Oct 2013 CPOL
Word Aligned Hybrid (WAH) compression for BitArrays
WAHBitarray.zip
WAHBitarray_v1.1.zip
WAHBitarray_v1.2.zip
WAHBitarray_v1.3.zip
WAHBitarray_v2.0.zip
WAHBitarray_v2.5.zip
WAHBitarray_v2.6.1.zip
UnitTests
UnitTests.nunit
Properties
WAHBitarray_v2.6.2.zip
UnitTests.nunit
WAHBitarray_v2.6.zip
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace WAHBitArray
{
    public class WAHBitArray 
    {
        public WAHBitArray(int size)
        {
            _ba = new BitArray(size);
            _size = size;
        }

        public WAHBitArray(BitArray bitarray)
        {
            _ba = bitarray;
            _size = bitarray.Length;
        }

        public WAHBitArray(int size, uint[] CompressedInts)
        {
            _compressed.AddRange(CompressedInts);
            _ba = new BitArray(size);
            _size = size;
        }

        private List<uint> _compressed = new List<uint>();
        private BitArray _ba;
        private int _size;
       
        public BitArray And(BitArray op)
        {
            CheckBitArray(op);

            return _ba.And(op);
        }

        public BitArray Or(BitArray op)
        {
            CheckBitArray(op);

            return _ba.Or(op);
        }

        public BitArray Not()
        {
            CheckBitArray(null);

            return _ba.Not();
        }

        public BitArray Xor(BitArray op)
        {
            CheckBitArray(op);

            return _ba.Xor(op);
        }

        public void FreeMemory()
        {
            Compress();
            _ba = null;
        }

        public uint[] GetCompressed()
        {
            Compress();
            return _compressed.ToArray();
        }

        #region [  P R I V A T E  ]

        private void CheckBitArray(BitArray op)
        {

            if (_ba == null)
                Uncompress();

            if (op != null)
            {
                int L1 = _ba.Length;
                int L2 = op.Length;
                if (L1 != L2)
                {
                    if (L1 > L2)
                        op.Length = L1;
                    else
                        _ba.Length = L2;
                }
                _size = _ba.Length;
            }
        }

        private void Compress()
        {
            if (_ba == null)
                return;
            _compressed = new List<uint>();
            uint zeros = 0;
            uint ones = 0;
            int mc = _ba.Count;
            for (int i = 0; i < _ba.Count; )
            {
                uint num = 0;
                for (int k = 0; k < 31; k++)
                {
                    num <<= 1;
                    if (i + k >= mc)
                        break;
                    if (_ba.Get(i + k))
                        num++;
                }
                i += 31;
                if (num == 0)
                {
                    zeros += 31;
                    if (ones > 0)
                    {
                        uint n = 0xc0000000 + ones;
                        ones = 0;
                        _compressed.Add(n);
                    }
                }
                else if (num == 0x7fffffff)
                {
                    ones += 31;
                    if (zeros > 0)
                    {
                        uint n = 0x80000000 + zeros;
                        zeros = 0;
                        _compressed.Add(n);
                    }
                }
                else
                {
                    if (ones > 0)
                    {
                        uint n = 0xc0000000 + ones;
                        ones = 0;
                        _compressed.Add(n);
                    }
                    if (zeros > 0)
                    {
                        uint n = 0x80000000 + zeros;
                        zeros = 0;
                        _compressed.Add(n);
                    }
                    _compressed.Add(num);
                }
            }
            if (ones > 0)
            {
                uint n = 0xc0000000 + ones;
                ones = 0;
                _compressed.Add(n);
            }
            if (zeros > 0)
            {
                uint n = 0x80000000 + zeros;
                zeros = 0;
                _compressed.Add(n);
            }
        }

        private void Uncompress()
        {
            int bit = 0;
            _ba = new BitArray(_size);
            int mc = _size;
            foreach (uint ci in _compressed)
            {
                if ((ci & 0x80000000) == 0)
                {
                    for (int j = 30; j >= 0; j--)
                    {
                        uint mask = (uint)1 << j;

                        if ((ci & mask) > 0)
                            _ba[bit] = true;
                        bit++;
                        if (bit >= mc)
                            break;

                    }
                }
                else
                {
                    uint c = ci & 0x3ffffff;
                    if ((ci & 0x40000000) > 0)
                    {
                        for (int j = (int)c; j >= 0; j--)
                        {
                            _ba.Set(bit, true);
                            //ba2[bit] = true;
                            bit++;
                            if (bit >= mc)
                                break;
                        }
                    }
                    else
                        bit += (int)c;
                }
            }
        }
#endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mehdi Gholam
Architect
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.
 
* Mehdi is the 5th person to get 6 out of 7 Platinums on CodeProject (13th Jan'12)

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 6 Oct 2013
Article Copyright 2011 by Mehdi Gholam
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid