/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/core/container/map.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/core/container/map.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/core/container/map.hpp Source File
map.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
10 
11 namespace ck_tile {
12 
13 // naive map
14 template <typename key, typename data, index_t max_size = 128>
15 struct map
16 {
19 
22 
23  struct iterator
24  {
27 
29  : impl_{impl}, pos_{pos}
30  {
31  }
32 
34  {
35  pos_++;
36  return *this;
37  }
38 
39  CK_TILE_HOST_DEVICE constexpr bool operator!=(const iterator& other) const
40  {
41  return other.pos_ != pos_;
42  }
43 
44  CK_TILE_HOST_DEVICE constexpr pair_type& operator*() { return impl_.at(pos_); }
45  };
46 
48  {
49  const impl_type& impl_;
51 
53  : impl_{impl}, pos_{pos}
54  {
55  }
56 
58  {
59  pos_++;
60 
61  return *this;
62  }
63 
64  CK_TILE_HOST_DEVICE constexpr bool operator!=(const const_iterator& other) const
65  {
66  return other.pos_ != pos_;
67  }
68 
69  CK_TILE_HOST_DEVICE constexpr const pair_type& operator*() const { return impl_.at(pos_); }
70  };
71 
72  CK_TILE_HOST_DEVICE constexpr map() : impl_{}, size_{0} {}
73 
74  CK_TILE_HOST_DEVICE constexpr index_t size() const { return size_; }
75 
77 
78  CK_TILE_HOST_DEVICE constexpr index_t find_position(const key& k) const
79  {
80  for(index_t i = 0; i < size(); i++)
81  {
82  if(impl_[i].template at<0>() == k)
83  {
84  return i;
85  }
86  }
87 
88  return size_;
89  }
90 
91  CK_TILE_HOST_DEVICE constexpr const_iterator find(const key& k) const
92  {
94  }
95 
96  CK_TILE_HOST_DEVICE constexpr iterator find(const key& k)
97  {
98  return iterator{impl_, find_position(k)};
99  }
100 
101  CK_TILE_HOST_DEVICE constexpr const data& operator[](const key& k) const
102  {
103  const auto it = find(k);
104 
105  // FIXME
106  // assert(it.pos_ < size());
107 
108  return impl_[it.pos_].template at<1>();
109  }
110 
111  CK_TILE_HOST_DEVICE constexpr data& operator()(const key& k)
112  {
113  auto it = find(k);
114 
115  // if entry not found
116  if(it.pos_ == size())
117  {
118  impl_(it.pos_).template at<0>() = k;
119  size_++;
120  }
121 
122  // FIXME
123  // assert(size_ <= max_size);
124 
125  return impl_(it.pos_).template at<1>();
126  }
127 
128  // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
129  CK_TILE_HOST_DEVICE constexpr const_iterator begin() const { return const_iterator{impl_, 0}; }
130 
131  // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
133  {
134  return const_iterator{impl_, size_};
135  }
136 
137  // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
138  CK_TILE_HOST_DEVICE constexpr iterator begin() { return iterator{impl_, 0}; }
139 
140  // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
142 };
143 
144 template <typename key, typename data, index_t max_size>
145 CK_TILE_HOST_DEVICE static void print(const map<key, data, max_size>& m)
146 {
147  printf("map{size_: %d, impl_: [", m.size_);
148  for(const auto& [k, d] : m)
149  {
150  printf("{key: ");
151  print(k);
152  printf(", data: ");
153  print(d);
154  printf("}, ");
155  }
156  printf("]}");
157 }
158 
159 } // namespace ck_tile
#define CK_TILE_HOST_DEVICE
Definition: config.hpp:42
Definition: cluster_descriptor.hpp:13
int32_t index_t
Definition: integer.hpp:9
A fixed-size array container similar to std::array with additional utilities.
Definition: array.hpp:43
constexpr CK_TILE_HOST_DEVICE auto & at(index_t i)
Definition: array.hpp:110
Definition: map.hpp:48
const impl_type & impl_
Definition: map.hpp:49
constexpr CK_TILE_HOST_DEVICE const pair_type & operator*() const
Definition: map.hpp:69
constexpr CK_TILE_HOST_DEVICE const_iterator & operator++()
Definition: map.hpp:57
constexpr CK_TILE_HOST_DEVICE const_iterator(const impl_type &impl, index_t pos)
Definition: map.hpp:52
constexpr CK_TILE_HOST_DEVICE bool operator!=(const const_iterator &other) const
Definition: map.hpp:64
index_t pos_
Definition: map.hpp:50
Definition: map.hpp:24
impl_type & impl_
Definition: map.hpp:25
constexpr CK_TILE_HOST_DEVICE iterator(impl_type &impl, index_t pos)
Definition: map.hpp:28
constexpr CK_TILE_HOST_DEVICE bool operator!=(const iterator &other) const
Definition: map.hpp:39
constexpr CK_TILE_HOST_DEVICE pair_type & operator*()
Definition: map.hpp:44
index_t pos_
Definition: map.hpp:26
constexpr CK_TILE_HOST_DEVICE iterator & operator++()
Definition: map.hpp:33
Definition: map.hpp:16
constexpr CK_TILE_HOST_DEVICE map()
Definition: map.hpp:72
constexpr CK_TILE_HOST_DEVICE index_t size() const
Definition: map.hpp:74
constexpr CK_TILE_HOST_DEVICE const_iterator end() const
Definition: map.hpp:132
constexpr CK_TILE_HOST_DEVICE index_t find_position(const key &k) const
Definition: map.hpp:78
constexpr CK_TILE_HOST_DEVICE const_iterator begin() const
Definition: map.hpp:129
constexpr CK_TILE_HOST_DEVICE data & operator()(const key &k)
Definition: map.hpp:111
impl_type impl_
Definition: map.hpp:20
constexpr CK_TILE_HOST_DEVICE const data & operator[](const key &k) const
Definition: map.hpp:101
constexpr CK_TILE_HOST_DEVICE iterator end()
Definition: map.hpp:141
constexpr CK_TILE_HOST_DEVICE iterator begin()
Definition: map.hpp:138
CK_TILE_HOST_DEVICE void clear()
Definition: map.hpp:76
constexpr CK_TILE_HOST_DEVICE const_iterator find(const key &k) const
Definition: map.hpp:91
index_t size_
Definition: map.hpp:21
constexpr CK_TILE_HOST_DEVICE iterator find(const key &k)
Definition: map.hpp:96
Definition: tuple.hpp:192