Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / C#

Word Aligned Hybrid (WAH) Compression for BitArrays

Rate me:
Please Sign up or sign in to vote.
4.89/5 (35 votes)
28 Feb 2015CPOL6 min read 129.8K   2.8K   75  
Word Aligned Hybrid (WAH) compression for BitArrays
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)


Written By
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 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions