summaryrefslogtreecommitdiff
path: root/src/packedintarray.cpp
blob: 817a7ab376c3642fece21e89c57fa722421f309a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "packedintarray.h"

#include <bu/sio.h>

#define bitsizeof( x )      ((sizeof(x))*8)
#define StoreBits           ((bitsizeof(PackedIntArray::Store)))
#define StoreCount( x )     (((x*iBitWidth)/StoreBits)+(((x*iBitWidth)%StoreBits)?1:0))

PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth ) :
    iBitWidth( iBitWidth ),
    aData( NULL ),
    iCapacity( 0 ),
    iCount( 0 ),
    uMask( 0 )
{
    aData = new Store[4];
    iCapacity = (StoreBits*4)/iBitWidth;
    if( iBitWidth < StoreBits )
    {
        if( (StoreBits%iBitWidth) == 0 )
            iMaxSpan = 1;
        else
        {
            iMaxSpan = (iBitWidth/StoreBits)+1;
        }
    }
    for( int j = 0; j < iBitWidth; j++ )
        uMask |= (1<<j);
}

PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth, int iCount ):
    iBitWidth( iBitWidth ),
    aData( NULL ),
    iCapacity( StoreCount(iCount) ),
    iCount( iCount ),
    uMask( 0 )
{
    aData = new Store[StoreCount(iCapacity)];
    memset( aData, 0, StoreCount(iCapacity));
    for( int j = 0; j < iBitWidth; j++ )
        uMask |= (1<<j);
}

PackedIntArray::~PackedIntArray()
{
    delete[] aData;
}

void PackedIntArray::append( PackedIntArray::Unit i )
{
    iCount++;
    checkCapacity();
    set( iCount-1, i );
}

PackedIntArray::Unit PackedIntArray::get( int idx ) const
{
    int iStore = idx*iBitWidth/StoreBits;
    int iBit = (idx*iBitWidth)%StoreBits;
//    Bu::println("idx = %1, iStore = %2, iBit = %3").
//        arg( idx ).arg( iStore ).arg( iBit );

    Unit ret = (aData[iStore]>>iBit)&uMask;

    for( iBit = StoreBits-iBit; iBit < iBitWidth; )
    {
        iStore++;
//        Bu::println("  ==> iStore = %2, iBit = %3").
//            arg( idx ).arg( iStore ).arg( iBit );
        ret |= (aData[iStore]&((Store)uMask)>>iBit)<<iBit;
        iBit += StoreBits;
    }
    return ret;
}

PackedIntArray::Unit PackedIntArray::set( int idx, PackedIntArray::Unit i )
{
    int iStore = idx*iBitWidth/StoreBits;
    int iBit = (idx*iBitWidth)%StoreBits;
//    Bu::println("idx = %1, iStore = %2, iBit = %3").
//        arg( idx ).arg( iStore ).arg( iBit );

    aData[iStore] = (aData[iStore]& ~(((Store)uMask)<<iBit)) |
        ((Store)i)<<iBit;
    for( iBit = StoreBits-iBit; iBit < iBitWidth; )
    {
        iStore++;
//        Bu::println("  ==> iStore = %2, iBit = %3").
//            arg( idx ).arg( iStore ).arg( iBit );
        aData[iStore] = (aData[iStore]& ~(((Store)uMask)>>iBit)) |
            ((Store)i)>>iBit;
        iBit += StoreBits;
    }
}

Bu::String PackedIntArray::toBitString() const
{
    Bu::String sRet;
    for( int j = iCount*iBitWidth-1; j >= 0; j-- )
    {
        sRet += (aData[j/StoreBits] & (1<<(j%StoreBits)))
            ? "1" : "0";
        if( j > 0 && j%iBitWidth == 0 )
            sRet += " ";
    }
    return sRet;
}

Bu::String PackedIntArray::toString() const
{
    Bu::String sRet;
    for( int j = iCount-1; j >= 0; j-- )
    {
        sRet += (char)(get(j))+'0';
    }

    return sRet;
}

void PackedIntArray::checkCapacity()
{
    if( iCount > iCapacity )
    {
        Bu::println("!!! Resizing !!!");
        Store *aOldData = aData;
        int iNewSize = StoreCount(iCapacity*2);
        int iSize = StoreCount(iCapacity);
        Bu::println("  %1 => %2 (%3 bit words)").arg( iSize ).arg( iNewSize )
            .arg( StoreBits );
        aData = new Store[iNewSize];
        memset( aData, 0, iNewSize*sizeof(Store) );
        memcpy( aData, aOldData, iSize*sizeof(Store) );
        iCapacity *= 2;
        delete[] aOldData;
    }
}