summaryrefslogtreecommitdiff
path: root/src/packedintarray.cpp
blob: b91358e24812903947ad4f3ac1f725717185c91b (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#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)*StoreBits)/iBitWidth ),
    iCount( iCount ),
    uMask( 0 )
{
    int iSize = StoreCount(iCapacity);
    aData = new Store[iSize];
    memset( aData, 0, iSize*sizeof(Store));
    for( int j = 0; j < iBitWidth; j++ )
        uMask |= (1<<j);
}

PackedIntArray::PackedIntArray( const PackedIntArray &rSrc ) :
    iBitWidth( rSrc.iBitWidth ),
    aData( NULL ),
    iCapacity( rSrc.iCapacity ),
    iCount( rSrc.iCount ),
    uMask( rSrc.uMask )
{
    int iSize = StoreCount(iCapacity);
    aData = new Store[iSize];
    memcpy( aData, rSrc.aData, iSize*sizeof(Store) );
}

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

void PackedIntArray::clear()
{
    iCount = 0;
}

void PackedIntArray::zero()
{
    int iSize = StoreCount(iCapacity);
    memset( aData, 0, iSize*sizeof(Store));
}

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

void PackedIntArray::remove()
{
    if( iCount > 0 )
        iCount--;
}

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;
}

void 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;
    }
}

void PackedIntArray::insert( int idx, Unit i )
{
    iCount++;
    checkCapacity();
    for( int j = iCount-1; j > idx; j-- )
    {
        set( j, get( j-1 ) );
    }
    set( idx, i );
}

void PackedIntArray::set( const PackedIntArray &rSrc, int iStart, int iSize )
{
    iCount = iSize;
    checkCapacity();

    for( int j = 0; j < iSize; j++ )
    {
        set( j, rSrc.get( iStart+j ) );
    }
}

void PackedIntArray::set( const PackedIntArray &rSrc )
{
    delete[] aData;

    iBitWidth = rSrc.iBitWidth;
    iCapacity = rSrc.iCapacity;
    iCount = rSrc.iCount;
    uMask = rSrc.uMask;
    
    int iSize = StoreCount(iCapacity);
    aData = new Store[iSize];
    memcpy( aData, rSrc.aData, iSize*sizeof(Store) );
}

void PackedIntArray::copy( int iDest, const PackedIntArray &rSrc, int iStart,
        int iSize )
{
    iCount = iSize;
    checkCapacity();

    for( int j = 0; j < iSize; j++ )
    {
        set( j+iDest, rSrc.get( iStart+j ) );
    }
}

void PackedIntArray::trim()
{
    while( iCount > 0 && get( iCount-1 ) == 0 )
        iCount--;
}

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 = (iCapacity==0)?(bitsizeof(Store)/iBitWidth):(iCapacity=StoreCount(iCapacity*2));
        while( iNewSize < iCount )
            iNewSize *= 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 = (iNewSize*bitsizeof(Store))/iBitWidth;
        delete[] aOldData;
    }
}