File: PackedVector.h

package info (click to toggle)
libstatgen 1.0.15-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: cpp: 49,624; ansic: 1,408; makefile: 320; sh: 60
file content (126 lines) | stat: -rw-r--r-- 3,444 bytes parent folder | download | duplicates (4)
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
/*
 *  Copyright (C) 2010  Regents of the University of Michigan
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __PACKEDVECTOR_H
#define __PACKEDVECTOR_H


// STL:
#include <ostream>
#include <sstream>
#include <string>

#include "Generic.h"


//
// This file implements a packed vector template based on the
// getter/setter code used in MemoryMapArray.h
//


template <
uint32_t accessorFunc(std::vector<uint8_t> &base, uint32_t index),
void setterFunc(std::vector<uint8_t> &base, uint32_t index, uint32_t value),
size_t elementCount2BytesFunc(uint32_t elementCount)
>
class PackedVector
{
protected:
    std::vector<uint8_t> m_data;
    size_t              m_elementCount;
    double              m_growthRateMultiplier;
    double              m_growthRateAdder;
public:
    PackedVector() :
        m_elementCount(0),
        m_growthRateMultiplier(1.20),
        m_growthRateAdder(128) {;}

    // accessing
    inline uint32_t operator[](uint32_t i)
    {
        return accessorFunc(m_data, i);
    }
    inline void set(uint32_t i, uint32_t v)
    {
        setterFunc(m_data, i, v);
    }

    size_t getElementCount() const
    {
        return m_elementCount;
    }

    double getUtilization() {
        return elementCount2BytesFunc(m_elementCount) / (double) m_data.capacity();
    }

    void reserve(uint32_t reserveElements) {
        m_data.reserve(elementCount2BytesFunc(reserveElements));
    }

    size_t size() {return m_elementCount;}

    void resize(uint32_t newSize) {
        m_elementCount = newSize;
        m_data.resize(elementCount2BytesFunc(m_elementCount));
    }

    // it's a bit of a challenge to optimize this...
    void push_back(uint32_t value) {
        m_elementCount++;
        if(elementCount2BytesFunc(m_elementCount) >= m_data.size()) {

            if( (elementCount2BytesFunc(m_elementCount)) > m_data.capacity())
            {
                size_t newCapacity = (size_t) (m_data.capacity() * m_growthRateMultiplier);

                // for small capacities, small fractional multipliers don't work,
                // so we check and do a linear increase in those cases:
                if(newCapacity == m_data.capacity()) {
                    newCapacity = (size_t) (m_data.capacity() + m_growthRateAdder);
                }

                m_data.reserve(newCapacity);
            }

        }
        m_data.resize(elementCount2BytesFunc(m_elementCount));
        set(m_elementCount-1, value);
    }
};

typedef PackedVector<
PackedAccess_1Bit,
PackedAssign_1Bit,
Packed1BitElementCount2Bytes
> PackedVectorBool_t;

typedef PackedVector<
PackedAccess_2Bit,
PackedAssign_2Bit,
Packed2BitElementCount2Bytes
> PackedVector2Bit_t;

typedef PackedVector<
PackedAccess_4Bit,
PackedAssign_4Bit,
Packed4BitElementCount2Bytes
> PackedVector4Bit_t;

#endif