spla
cpu_format_coo_vec.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of spla project */
3 /* https://github.com/SparseLinearAlgebra/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_CPU_FORMAT_COO_VEC_HPP
29 #define SPLA_CPU_FORMAT_COO_VEC_HPP
30 
31 #include <cpu/cpu_formats.hpp>
32 
33 #include <algorithm>
34 #include <vector>
35 
36 namespace spla {
37 
43  template<typename T>
45  std::vector<std::pair<uint, T>> buffer;
46  buffer.reserve(vec.values);
47 
48  for (uint i = 0; i < vec.values; i++) {
49  buffer.emplace_back(vec.Ai[i], vec.Ax[i]);
50  }
51 
52  std::sort(buffer.begin(), buffer.end(), [](auto& a, auto& b) { return a.first < b.first; });
53 
54  for (uint i = 0; i < vec.values; i++) {
55  vec.Ai[i] = buffer[i].first;
56  vec.Ax[i] = buffer[i].second;
57  }
58  }
59 
60  template<typename T>
61  void cpu_coo_vec_resize(const uint n_values,
62  CpuCooVec<T>& vec) {
63  vec.Ai.resize(n_values);
64  vec.Ax.resize(n_values);
65  vec.values = n_values;
66  }
67 
68  template<typename T>
70  vec.Ai.clear();
71  vec.Ax.clear();
72  vec.values = 0;
73  }
74 
75  template<typename T>
77  CpuDokVec<T>& out) {
78  assert(out.values == 0);
79  assert(out.Ax.empty());
80 
81  for (std::size_t k = 0; k < in.Ai.size(); ++k) {
82  const uint i = in.Ai[k];
83  const T x = in.Ax[k];
84  out.Ax[i] = x;
85  out.values += 1;
86  }
87  }
88 
93 }// namespace spla
94 
95 #endif//SPLA_CPU_FORMAT_COO_VEC_HPP
CPU list-of-coordinates sparse vector representation.
Definition: cpu_formats.hpp:90
std::vector< uint > Ai
Definition: cpu_formats.hpp:96
std::vector< T > Ax
Definition: cpu_formats.hpp:97
Definition: cpu_formats.hpp:55
robin_hood::unordered_flat_map< uint, T > Ax
Definition: cpu_formats.hpp:63
uint values
Definition: tdecoration.hpp:58
void cpu_coo_vec_clear(CpuCooVec< T > &vec)
Definition: cpu_format_coo_vec.hpp:69
void cpu_coo_vec_resize(const uint n_values, CpuCooVec< T > &vec)
Definition: cpu_format_coo_vec.hpp:61
void cpu_coo_vec_sort(CpuCooVec< T > &vec)
Definition: cpu_format_coo_vec.hpp:44
void cpu_coo_vec_to_dok(const CpuCooVec< T > &in, CpuDokVec< T > &out)
Definition: cpu_format_coo_vec.hpp:76
std::uint32_t uint
Library index and size type.
Definition: config.hpp:56
Definition: algorithm.hpp:37