File: data_frame.hpp

package info (click to toggle)
sra-sdk 3.0.3%2Bdfsg-6~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 165,852 kB
  • sloc: ansic: 374,775; cpp: 232,734; perl: 8,959; java: 6,253; sh: 6,032; python: 3,890; makefile: 1,046; yacc: 703; xml: 310; lex: 235
file content (233 lines) | stat: -rw-r--r-- 7,248 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
#ifndef __DATA_FRAME_HPP_
#define __DATA_FRAME_HPP_

/*  
 * ===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 * Authors:  Andrei Shkeda
 *
 * File Description:
 *
 */

#include <bm/bm.h>
#include <bm/bmsparsevec.h>
#include <bm/bmstrsparsevec.h>
#include <bm/bmsparsevec_serial.h>
#include <bm/bmsparsevec_compr.h>
#include <bm/bmintervals.h>
#include <vector>
#include <ostream>
#include <memory>
#include <algorithm>
#include <variant>

using namespace std;

typedef enum {
    eDF_String,
    eDF_Uint16,
    eDF_Uint32,
    eDF_Uint64,
    eDF_Bit
} EDF_ColumnType;

 

/*!
    CDataFrame implements succinct columnar data frame 
    It supports three data types : String, Uint and Bit 
    The data types correspond to BitMagic's vector types:
        str_sparse_vector, sparse_vector<unsigned>, bvector
    
    Template parameter MAX_SIZE defines the max size for string columns

    Constructor's initializer_list describes the number and the types of columns 

    get()
    GetColumns() 
    provide access to the columns

    Column is a union structure with pointer to the corresponding data types
    The pointer provides access to underlying vectors for further manipulations
    The caller is responsible for invoking correct pointer to the corresponding data type


 */

typedef bm::sparse_vector<uint16_t, bm::bvector<> > svector_u16;
typedef bm::sparse_vector<unsigned, bm::bvector<> > svector_u32;
typedef bm::sparse_vector<uint64_t, bm::bvector<> > svector_u64;

typedef svector_u16 u16_t; 
typedef svector_u32 u32_t;
typedef svector_u64 u64_t;
typedef bm::bvector<> bit_t;
typedef typename bm::str_sparse_vector<char,  bm::bvector<>, 32> str_t;


class CDataFrame 
{

public:



    /**
     * Column provides access to the vector data via union of pointers 
     * to the specific data type
     * Caller is responsible for accesing the correct pointer 
     * Usage:
     * df.get<str_t>(0)->set(0, "1"); // access to str_sparse_vector
     * df.get<u32_t>(1)->set(0, 1);  // access to sparse_vector<unsigned>
     * df.get<bit_t>(1)->set(0);      // access to bit vector 
     * 
    */
   
    using Column = std::variant<str_t, u16_t, u32_t, u64_t, bit_t>;
    typedef vector<Column> TColumns;

    /** Default constructor */
    CDataFrame() {
    }
    
    /** Constructor with column initiazlization
     * @param[in] columns initializer list of column types
     *   Usage:
     *   CDataFrame df({eDF_String, eDF_Uint, eDF_Bit});
    */
    CDataFrame(initializer_list<EDF_ColumnType>& columns) 
    {
        CreateColumns(columns);
    }

    CDataFrame(const CDataFrame&) = delete;
    CDataFrame& operator=(const CDataFrame&) = delete;
    virtual ~CDataFrame() = default;

    void CreateColumns(initializer_list<EDF_ColumnType> columns) 
    {
        for (const auto& t : columns) {
            switch (t) {
            case eDF_String: {
                m_Columns.emplace_back().emplace<str_t>();
                break;
            }
            case eDF_Uint16: {
                m_Columns.emplace_back().emplace<u16_t>();
                break;
            }
            case eDF_Uint32: {
                m_Columns.emplace_back().emplace<u32_t>();
                break;
            }
            case eDF_Uint64: {
                m_Columns.emplace_back().emplace<u64_t>();
                break;
            }
            case eDF_Bit: {
                m_Columns.emplace_back().emplace<bit_t>();
                break;
            }
            }
        }
    }

    template<typename T>
    vector<typename T::value_type> gather(const vector<typename T::size_type>& rows, unsigned col_idx, unsigned num_rows) 
    {
        vector<typename T::size_type> tmp_buf;
        tmp_buf.resize(num_rows, 0);
        vector<typename T::value_type> values;
        values.resize(num_rows, 0);
        auto& data = *get<T>(col_idx);
        data.gather(values.data(), rows.data(), tmp_buf.data(), num_rows, bm::BM_UNSORTED);
        return values;
    }

    /** 
     * Access to a column by type and index with no boundary checks
    */
    template<typename T> 
    T& get(unsigned idx) 
    {
        return std::get<T>(m_Columns[idx]);
    }


    /** 
     * Access to all columns 
    */
    const TColumns& GetColumns() const noexcept
    {
        return m_Columns;
    }
    TColumns& GetColumns() noexcept
    {
        return m_Columns;
    }

    /** 
     * Optimize internal data after adding or updating new rows 
    */
    size_t Optimize() {
        size_t total_memory = 0;
        BM_DECLARE_TEMP_BLOCK(TB)
        for (auto& col : m_Columns) {
            std::visit([&total_memory, &TB](auto&& c) {
                using T = std::decay_t<decltype(c)>;
                if constexpr (std::is_same<str_t, T>::value) {
                    str_t::statistics st;
                    c.optimize(TB, bm::bvector<>::opt_compress, &st);
                    total_memory += st.memory_used;
                } else if constexpr (std::is_same<u16_t, T>::value) {
                    u16_t::statistics st;
                    c.optimize(TB, bm::bvector<>::opt_compress, &st);
                    total_memory += st.memory_used;
                } else if constexpr (std::is_same<u32_t, T>::value) {
                    u32_t::statistics st;
                    c.optimize(TB, bm::bvector<>::opt_compress, &st);
                    total_memory += st.memory_used;
                } else if constexpr (std::is_same<u64_t, T>::value) {
                    u64_t::statistics st;
                    c.optimize(TB, bm::bvector<>::opt_compress, &st);
                    total_memory += st.memory_used;
                } else if constexpr (std::is_same<bit_t, T>::value) {
                    bit_t::statistics st;
                    c.optimize(TB, bm::bvector<>::opt_compress, &st);
                    total_memory += st.memory_used;

                }
            }, col);        
        }
        return total_memory;
    }

protected:
    TColumns m_Columns;
};

#endif /* __DATA_FRAME_HPP_ */