111 if (!last_insert)
return false;
113 auto hint_lease = last_insert->lock.start_read();
117 if (!last_insert->lock.validate(hint_lease))
return false;
121 cur_lease = hint_lease;
126 if (hints.last_insert.any(checkHint)) {
144 cur_lease = cur->lock.start_read();
157 auto a = &(cur->keys[0]);
158 auto b = &(cur->keys[cur->numElements]);
166 if (!cur->lock.validate(cur_lease)) {
172 if (
typeid(
Comparator) !=
typeid(WeakComparator) && this->
less(
k, *pos)) {
173 if (!cur->lock.try_upgrade_to_write(cur_lease)) {
180 auto res = (*pos).second;
182 cur->lock.end_write();
187 auto res = (*pos).second;
190 if (!cur->lock.validate(cur_lease)) {
200 auto next = cur->getChild(idx);
203 auto next_lease = next->lock.start_read();
206 if (!cur->lock.end_read(cur_lease)) {
215 cur_lease = next_lease;
225 auto a = &(cur->keys[0]);
226 auto b = &(cur->keys[cur->numElements]);
232 if (isSet && pos != a && this->
weak_equal(*(pos - 1),
k)) {
234 if (!cur->lock.validate(cur_lease)) {
241 if (
typeid(
Comparator) !=
typeid(WeakComparator) && this->
less(
k, *(pos - 1))) {
242 if (!cur->lock.try_upgrade_to_write(cur_lease)) {
249 auto res = (*(pos - 1)).second;
251 cur->lock.end_write();
256 std::atomic<typename Functor::result_type>& loc =
257 *
reinterpret_cast<std::atomic<typename Functor::result_type>*
>(&(*(pos - 1)).second);
258 auto res = loc.load(std::memory_order_relaxed);
261 if (!cur->lock.validate(cur_lease)) {
271 if (!cur->lock.try_upgrade_to_write(cur_lease)) {
273 hints.last_insert.access(cur);
280 auto parent = priv->parent;
281 std::vector<typename parenttype::node*> parents;
284 parent->lock.start_write();
287 if (parent == priv->parent) {
291 parent->lock.abort_write();
292 parent = priv->parent;
293 parent->lock.start_write();
301 parents.push_back(parent);
304 if (!parent || !parent->isFull()) {
310 parent = parent->parent;
315 auto old_root = this->
root;
316 idx -= cur->rebalance_or_split(
317 const_cast<typename parenttype::node**
>(&this->
root), this->
root_lock, idx, parents);
320 for (
auto it = parents.rbegin(); it != parents.rend(); ++it) {
325 parent->lock.end_write();
327 if (old_root != this->
root) {
338 cur->lock.end_write();
349 for (
int j = cur->numElements;
j > idx; --
j) {
350 cur->keys[
j] = cur->keys[
j - 1];
354 typename Functor::result_type res = f(
k);
359 cur->lock.end_write();
362 hints.last_insert.access(cur);
370 this->
leftmost =
new typename parenttype::leaf_node();
373 typename Functor::result_type res = f(
k);
377 hints.last_insert.access(this->
leftmost);
383 typename parenttype::node* cur = this->
root;
385 auto checkHints = [&](
typename parenttype::node* last_insert) {
386 if (!last_insert)
return false;
393 if (hints.last_insert.any(checkHints)) {
402 auto a = &(cur->keys[0]);
403 auto b = &(cur->keys[cur->numElements]);
411 if (
typeid(
Comparator) !=
typeid(WeakComparator) && this->
less(
k, *pos)) {
413 return (*pos).second;
416 return (*pos).second;
419 cur = cur->getChild(idx);
428 auto a = &(cur->keys[0]);
429 auto b = &(cur->keys[cur->numElements]);
435 if (isSet && pos != a && this->
weak_equal(*(pos - 1),
k)) {
437 if (
typeid(
Comparator) !=
typeid(WeakComparator) && this->
less(
k, *(pos - 1))) {
439 return (*(pos - 1)).second;
442 return (*(pos - 1)).second;
447 idx -= cur->rebalance_or_split(
448 const_cast<typename parenttype::node**
>(&this->
root), this->
root_lock, idx);
452 idx -= cur->numElements + 1;
453 cur = cur->parent->getChild(cur->position + 1);
461 for (
int j = cur->numElements;
j > idx; --
j) {
462 cur->keys[
j] = cur->keys[
j - 1];
466 typename Functor::result_type res = f(
k);
472 hints.last_insert.access(cur);
481 template <
typename Iter>
482 void insert(
const Iter& a,
const Iter&
b) {
486 for (
auto it = a; it !=
b; ++it) {
499 std::swap(this->
root, other.root);
500 std::swap(this->
leftmost, other.leftmost);