2 #include <score/model/Identifier.hpp>
3 #include <score/tools/std/ArrayView.hpp>
4 #include <score/tools/std/Optional.hpp>
8 #include <score_lib_base_export.h>
11 #include <type_traits>
25 static int32_t getRandomId();
26 static int32_t getFirstId() {
return getRandomId(); }
34 template <
typename Vector>
37 typename Vector::value_type
id{};
39 constexpr
auto fnd = [](
const auto& ids,
const auto& id) noexcept {
48 id =
typename Vector::value_type{getRandomId()};
49 }
while(fnd(ids,
id));
60 static int32_t getFirstId() {
return 1; }
62 template <
typename Vector>
63 static auto getNextId(
const Vector& ids)
65 auto it = std::max_element(ids.begin(), ids.end());
67 return typename Vector::value_type{getId(*it) + 1};
69 return typename Vector::value_type{getFirstId()};
74 static int32_t getId(
const Id<T>& other)
78 static int32_t getId(
const std::optional<int32_t>& i) {
return *i; }
79 static int32_t getId(int32_t i) {
return i; }
85 auto getStrongId(
const std::vector<
Id<T>>& v)
87 return Id<T>{score::id_generator::getNextId(v)};
93 return Id<T>{score::id_generator::getNextId(v)};
96 template <
typename Container>
97 requires(std::is_pointer<typename Container::value_type>::value)
98 auto getStrongId(
const Container& v)
104 vector<int32_t> ids(v.size());
107 v.begin(), v.end(), ids.begin(),
108 [](
const typename Container::value_type& elt) { return elt->id().val(); });
110 return local_id_t{score::id_generator::getNextId(ids)};
113 template <
typename Container>
114 requires(!std::is_pointer<typename Container::value_type>::value)
118 auto ids = make_dynarray(int32_t, v.size());
121 v.begin(), v.end(), ids.begin(), [](
const auto& elt) { return elt.id().val(); });
126 template <
typename T>
127 auto getStrongIdRange(std::size_t s)
129 std::vector<Id<T>> vec;
131 vec.emplace_back(score::id_generator::getFirstId());
134 for(std::size_t i = 0; i < s; i++)
136 vec.push_back(getStrongId(vec));
142 template <
typename T,
typename Vector>
143 auto getStrongIdRange(std::size_t s,
const Vector& existing)
145 auto existing_size = existing.size();
146 auto total_size = existing_size + s;
147 std::vector<Id<T>> vec;
148 vec.reserve(total_size);
152 existing.begin(), existing.end(), std::back_inserter(vec),
153 [](
const auto& elt) { return elt.id(); });
156 for(std::size_t i = 0; i < s; i++)
158 vec.push_back(getStrongId(vec));
161 return std::vector<Id<T>>(vec.begin() + existing.size(), vec.end());
164 template <
typename T,
typename Vector1,
typename Vector2>
166 getStrongIdRange2(std::size_t s,
const Vector1& existing1,
const Vector2& existing2)
168 std::vector<Id<T>> vec;
169 vec.reserve(s + existing1.size() + existing2.size());
171 existing1.begin(), existing1.end(), std::back_inserter(vec),
172 [](
const auto& elt) { return elt.id(); });
174 existing2.begin(), existing2.end(), std::back_inserter(vec),
175 [](
const auto& elt) { return elt->id(); });
177 for(std::size_t i = 0; i < s; i++)
179 vec.push_back(getStrongId(vec));
182 = std::vector<Id<T>>(vec.begin() + existing1.size() + existing2.size(), vec.end());
187 template <
typename T,
typename Vector>
188 auto getStrongIdRangePtr(std::size_t s,
const Vector& existing)
190 std::vector<Id<T>> vec;
191 vec.reserve(s + existing.size());
193 existing.begin(), existing.end(), std::back_inserter(vec),
194 [](
const auto& elt) { return elt->id(); });
198 vec.push_back(getStrongId(vec));
201 return std::vector<Id<T>>(vec.begin() + existing.size(), vec.end());
The id_base_t class.
Definition: Identifier.hpp:57
Definition: ArrayView.hpp:56
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Generates identifiers for new objects, starting from 1.
Definition: IdentifierGeneration.hpp:59
Generates random identifiers for new objects.
Definition: IdentifierGeneration.hpp:20
static auto getNextId(const Vector &ids)
getNextId
Definition: IdentifierGeneration.hpp:35