spla
Loading...
Searching...
No Matches
tarray.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/JetBrains-Research/spla */
4/**********************************************************************************/
5/* MIT License */
6/* */
7/* Copyright (c) 2023 SparseLinearAlgebra */
8/* */
9/* Permission is hereby granted, free of charge, to any person obtaining a copy */
10/* of this software and associated documentation files (the "Software"), to deal */
11/* in the Software without restriction, including without limitation the rights */
12/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13/* copies of the Software, and to permit persons to whom the Software is */
14/* furnished to do so, subject to the following conditions: */
15/* */
16/* The above copyright notice and this permission notice shall be included in all */
17/* copies or substantial portions of the Software. */
18/* */
19/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25/* SOFTWARE. */
26/**********************************************************************************/
27
28#ifndef SPLA_TARRAY_HPP
29#define SPLA_TARRAY_HPP
30
31#include <spla/array.hpp>
32#include <spla/config.hpp>
33
34#include <core/logger.hpp>
35#include <core/tdecoration.hpp>
36#include <core/ttype.hpp>
37
38#include <vector>
39
40namespace spla {
41
53 template<typename T>
54 class TArray final : public Array {
55 public:
56 explicit TArray(uint n_values);
57
58 ~TArray() override = default;
59
60 uint get_n_values() override;
61 ref_ptr<Type> get_type() override;
62 Status set_int(uint i, T_INT value) override;
63 Status set_uint(uint i, T_UINT value) override;
64 Status set_float(uint i, T_FLOAT value) override;
65 Status get_int(uint i, T_INT& value) override;
66 Status get_uint(uint i, T_UINT& value) override;
67 Status get_float(uint i, T_FLOAT& value) override;
68 Status resize(uint n_values) override;
69 Status build(const ref_ptr<MemView>& view) override;
70 Status read(ref_ptr<MemView>& view) override;
71 Status clear() override;
72 void set_label(std::string label) override;
73 const std::string& get_label() const override;
74
75 const std::vector<T>& data() const { return m_data; }
76 std::vector<T>& data() { return m_data; }
77
78 private:
79 std::vector<T> m_data;
80 std::string m_label;
81 };
82
83 template<typename T>
85 m_data.resize(n_values);
86 }
87
88 template<typename T>
90 return uint(m_data.size());
91 }
92 template<typename T>
94 return get_ttype<T>().template cast<Type>();
95 }
96
97 template<typename T>
99 assert(i < get_n_values());
100 m_data[i] = T(value);
101 return Status::Ok;
102 }
103 template<typename T>
105 assert(i < get_n_values());
106 m_data[i] = T(value);
107 return Status::Ok;
108 }
109 template<typename T>
111 assert(i < get_n_values());
112 m_data[i] = T(value);
113 return Status::Ok;
114 }
115
116 template<typename T>
118 assert(i < get_n_values());
119 value = T_INT(m_data[i]);
120 return Status::Ok;
121 }
122 template<typename T>
124 assert(i < get_n_values());
125 value = T_UINT(m_data[i]);
126 return Status::Ok;
127 }
128 template<typename T>
130 assert(i < get_n_values());
131 value = T_FLOAT(m_data[i]);
132 return Status::Ok;
133 }
134
135 template<typename T>
137 m_data.resize(n_values);
138 return Status::Ok;
139 }
140 template<typename T>
142 m_data.clear();
143 return Status::Ok;
144 }
145
146 template<typename T>
147 void TArray<T>::set_label(std::string label) {
148 m_label = std::move(label);
149 LOG_MSG(Status::Ok, "set label '" << m_label << "' to " << (void*) this);
150 }
151 template<typename T>
152 const std::string& TArray<T>::get_label() const {
153 return m_label;
154 }
155 template<typename T>
157 const auto element_size = sizeof(T);
158 const auto element_count = view->get_size() / element_size;
159
160 if (element_size * element_count != view->get_size()) {
162 }
163
164 const T* elements = (T*) view->get_buffer();
165
166 m_data.resize(element_count);
167
168 for (std::size_t i = 0; i < element_count; i++) {
169 m_data[i] = elements[i];
170 }
171
172 return Status::Ok;
173 }
174 template<typename T>
176 view = MemView::make(m_data.data(), m_data.size() * sizeof(T), false);
177 return Status::Ok;
178 }
179
180
185}// namespace spla
186
187#endif//SPLA_TARRAY_HPP
Status of library operation execution.
One-dimensional dense tightly packed array of typed values.
Definition array.hpp:50
static ref_ptr< MemView > make()
Definition memview.cpp:67
Array interface implementation with type information bound.
Definition tarray.hpp:54
~TArray() override=default
std::vector< T > & data()
Definition tarray.hpp:76
const std::vector< T > & data() const
Definition tarray.hpp:75
Automates reference counting and behaves as shared smart pointer.
Definition ref.hpp:117
ref_ptr< Type > get_type() override
Definition tarray.hpp:93
Status build(const ref_ptr< MemView > &view) override
Definition tarray.hpp:156
const std::string & get_label() const override
Definition tarray.hpp:152
Status resize(uint n_values) override
Definition tarray.hpp:136
Status read(ref_ptr< MemView > &view) override
Definition tarray.hpp:175
void set_label(std::string label) override
Definition tarray.hpp:147
Status get_uint(uint i, T_UINT &value) override
Definition tarray.hpp:123
Status set_int(uint i, T_INT value) override
Definition tarray.hpp:98
Status get_int(uint i, T_INT &value) override
Definition tarray.hpp:117
TArray(uint n_values)
Definition tarray.hpp:84
Status get_float(uint i, T_FLOAT &value) override
Definition tarray.hpp:129
Status clear() override
Definition tarray.hpp:141
Status set_uint(uint i, T_UINT value) override
Definition tarray.hpp:104
uint get_n_values() override
Definition tarray.hpp:89
Status set_float(uint i, T_FLOAT value) override
Definition tarray.hpp:110
float T_FLOAT
Definition type.hpp:60
std::int32_t T_INT
Definition type.hpp:58
std::uint32_t uint
Library index and size type.
Definition config.hpp:56
std::uint32_t T_UINT
Definition type.hpp:59
#define LOG_MSG(status, msg)
Definition logger.hpp:66
Definition algorithm.hpp:37