Health-GPS  1.2.2.0
Global Health Policy Simulation model (Health-GPS)
column_builder.h
Go to the documentation of this file.
1 #pragma once
2 #include <exception>
3 #include <limits>
4 #include <memory>
5 #include <vector>
6 
7 #include "column.h"
8 
9 namespace hgps::core {
10 
13 template <typename ColumnType> class PrimitiveDataTableColumnBuilder {
14 
15  public:
16  using value_type = typename ColumnType::value_type;
17 
19 
22  explicit PrimitiveDataTableColumnBuilder(std::string name) : name_{name} {
23  if (name_.length() < 2 || !std::isalpha(name_.front())) {
24  throw std::invalid_argument(
25  "Invalid column name: minimum length of two and start with alpha character.");
26  }
27  }
28 
31  std::string name() const { return name_; }
32 
35  std::size_t size() const { return data_.size(); }
36 
39  std::size_t null_count() const { return null_count_; }
40 
43  std::size_t capacity() const { return data_.capacity(); }
44 
47  void reserve(std::size_t capacity) { return data_.reserve(capacity); }
48 
50  void append_null() { append_null_internal(1); }
51 
54  void append_null(const std::size_t count) { append_null_internal(count); }
55 
56  void append(const value_type value) { append_internal(value); }
57 
61  value_type value(std::size_t index) const { return data_[index]; }
62 
66  value_type operator[](std::size_t index) const { return value(index); }
67 
71  value_type &operator[](std::size_t index) { return data_[index]; }
72 
74  void reset() {
75  data_.clear();
76  null_bitmap_.clear();
77  null_count_ = 0;
78  }
79 
82  [[nodiscard]] std::unique_ptr<ColumnType> build() {
83  data_.shrink_to_fit();
84  null_bitmap_.shrink_to_fit();
85 
86  if (null_count_ > 0) {
87 
88  // Contains null data, use null bitmap
89  return std::make_unique<ColumnType>(std::move(name_), std::move(data_),
90  std::move(null_bitmap_));
91  }
92 
93  // Full vector, no need for null bitmap
94  return std::make_unique<ColumnType>(std::move(name_), std::move(data_));
95  }
96 
97  private:
98  std::string name_;
99  std::vector<value_type> data_{};
100  std::vector<bool> null_bitmap_{};
101  std::size_t null_count_ = 0;
102 
103  void append_null_internal(const std::size_t length) {
104  null_count_ += length;
105  data_.insert(data_.end(), length, value_type{});
106  null_bitmap_.insert(null_bitmap_.end(), length, false);
107  }
108 
109  void append_internal(const value_type value) {
110  data_.push_back(value);
111  null_bitmap_.push_back(true);
112  }
113 };
114 
117 
120 
123 
126 } // namespace hgps::core
Primitive data type DataTable column builder class.
Definition: column_builder.h:13
value_type & operator[](std::size_t index)
Gets the column value at a given index, no boundary checks.
Definition: column_builder.h:71
value_type operator[](std::size_t index) const
Gets the column value at a given index, no boundary checks.
Definition: column_builder.h:66
void append_null(const std::size_t count)
Append multiple null values.
Definition: column_builder.h:54
std::unique_ptr< ColumnType > build()
Builds the column with current data.
Definition: column_builder.h:82
std::size_t size() const
Gets the column data size.
Definition: column_builder.h:35
std::size_t null_count() const
Gets the number of null values in the column data.
Definition: column_builder.h:39
PrimitiveDataTableColumnBuilder(std::string name)
Initialise a new instance of the PrimitiveDataTableColumnBuilder class.
Definition: column_builder.h:22
std::string name() const
Gets the column name.
Definition: column_builder.h:31
void reserve(std::size_t capacity)
Reserve builder storage capacity.
Definition: column_builder.h:47
void append_null()
Append a single null value.
Definition: column_builder.h:50
void append(const value_type value)
Definition: column_builder.h:56
typename ColumnType::value_type value_type
Definition: column_builder.h:16
value_type value(std::size_t index) const
Gets the column value at a given index, no boundary checks.
Definition: column_builder.h:61
std::size_t capacity() const
Gets the builder data capacity.
Definition: column_builder.h:43
void reset()
Resets the column builder data.
Definition: column_builder.h:74
Top-level namespace for Health-GPS Core C++ API.
Definition: analysis.h:7