Health-GPS  1.2.2.0
Global Health Policy Simulation model (Health-GPS)
column_primitive.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <optional>
5 #include <vector>
6 
7 #include "column.h"
8 #include "column_iterator.h"
9 
10 namespace hgps::core {
11 
14 template <typename TYPE> class PrimitiveDataTableColumn : public DataTableColumn {
15  public:
16  using value_type = TYPE;
18 
24  explicit PrimitiveDataTableColumn(std::string &&name, std::vector<TYPE> &&data)
25  : DataTableColumn(), name_{name}, data_{data} {
26  if (name_.length() < 2 || !std::isalpha(name_.front())) {
27  throw std::invalid_argument(
28  "Invalid column name: minimum length of two and start with alpha character.");
29  }
30 
31  null_count_ = 0;
32  }
33 
41  explicit PrimitiveDataTableColumn(std::string &&name, std::vector<TYPE> &&data,
42  std::vector<bool> &&null_bitmap)
43  : DataTableColumn(), name_{name}, data_{data}, null_bitmap_{null_bitmap} {
44  if (name_.length() < 2 || !std::isalpha(name_.front())) {
45  throw std::invalid_argument(
46  "Invalid column name: minimum length of two and start with alpha character.");
47  }
48 
49  if (data_.size() != null_bitmap_.size()) {
50  throw std::out_of_range(
51  "Input vectors size mismatch, the data and valid vectors size must be the same.");
52  }
53 
54  null_count_ = std::count(null_bitmap_.begin(), null_bitmap_.end(), false);
55  }
56 
57  std::string type() const noexcept override { return typeid(TYPE).name(); }
58 
59  std::string name() const noexcept override { return name_; }
60 
61  std::size_t null_count() const noexcept override { return null_count_; }
62 
63  std::size_t size() const noexcept override { return data_.size(); }
64 
65  bool is_null(std::size_t index) const noexcept override {
66  // Bound checks hit performance!
67  if (index >= size()) {
68  return true;
69  }
70 
71  return !null_bitmap_.empty() && !null_bitmap_[index];
72  }
73 
74  bool is_valid(std::size_t index) const noexcept override {
75  // Bound checks hit performance!
76  if (index >= size()) {
77  return false;
78  }
79 
80  return null_bitmap_.empty() || null_bitmap_[index];
81  }
82 
83  const std::any value(std::size_t index) const noexcept override {
84  if (is_valid(index)) {
85  return data_[index];
86  }
87 
88  return std::any();
89  }
90 
94  const std::optional<value_type> value_safe(const std::size_t index) const noexcept {
95  if (is_valid(index)) {
96  return data_[index];
97  }
98 
99  return std::nullopt;
100  }
101 
106  const value_type value_unsafe(const std::size_t index) const { return data_[index]; }
107 
110  IteratorType begin() const { return IteratorType(*this); }
111 
114  IteratorType end() const { return IteratorType(*this, size()); }
115 
116  private:
117  std::string name_;
118  std::vector<TYPE> data_;
119  std::vector<bool> null_bitmap_{};
120  std::size_t null_count_ = 0;
121 };
122 
124 class StringDataTableColumn : public PrimitiveDataTableColumn<std::string> {
125  public:
127 
128  std::string type() const noexcept override { return "string"; }
129 
130  void accept(DataTableColumnVisitor &visitor) const override { visitor.visit(*this); }
131 };
132 } // namespace hgps::core
DataTable column iterator data type class.
Definition: column_iterator.h:29
DataTable column visitor interface.
Definition: visitor.h:8
virtual void visit(const StringDataTableColumn &column)=0
Visits a column of StringDataTableColumn type.
DataTable columns interface data type.
Definition: column.h:11
Primitive data type DataTable columns class.
Definition: column_primitive.h:14
IteratorType begin() const
Gets the iterator to the first element of the column.
Definition: column_primitive.h:110
bool is_null(std::size_t index) const noexcept override
Determine whether a column value is null.
Definition: column_primitive.h:65
const std::any value(std::size_t index) const noexcept override
Gets the column value at a given index.
Definition: column_primitive.h:83
PrimitiveDataTableColumn(std::string &&name, std::vector< TYPE > &&data)
Initialises a new instance of the PrimitiveDataTableColumn class.
Definition: column_primitive.h:24
std::string type() const noexcept override
Gets the column type name.
Definition: column_primitive.h:57
const std::optional< value_type > value_safe(const std::size_t index) const noexcept
Gets the column value at a given index.
Definition: column_primitive.h:94
const value_type value_unsafe(const std::size_t index) const
Gets the column value at a given index, unsafe without boundary checks.
Definition: column_primitive.h:106
PrimitiveDataTableColumn(std::string &&name, std::vector< TYPE > &&data, std::vector< bool > &&null_bitmap)
Initialises a new instance of the PrimitiveDataTableColumn class.
Definition: column_primitive.h:41
TYPE value_type
Definition: column_primitive.h:16
DataTableColumnIterator< PrimitiveDataTableColumn< TYPE > > IteratorType
Definition: column_primitive.h:17
std::size_t size() const noexcept override
The size of the column data, number of rows.
Definition: column_primitive.h:63
IteratorType end() const
Gets the iterator element following the last element of the column.
Definition: column_primitive.h:114
std::string name() const noexcept override
Gets the column name identifier.
Definition: column_primitive.h:59
bool is_valid(std::size_t index) const noexcept override
Determine whether a column value is not null.
Definition: column_primitive.h:74
std::size_t null_count() const noexcept override
Gets the number of null values in the column data.
Definition: column_primitive.h:61
DataTable column for storing string data type class.
Definition: column_primitive.h:124
void accept(DataTableColumnVisitor &visitor) const override
Double dispatch the column using a visitor implementation.
Definition: column_primitive.h:130
std::string type() const noexcept override
Gets the column type name.
Definition: column_primitive.h:128
Top-level namespace for Health-GPS Core C++ API.
Definition: analysis.h:7