| 439 | |
| 440 | template <typename Value, typename Func1, typename Func2> |
| 441 | Status GetOrInsert(Value&& v, Func1&& on_found, Func2&& on_not_found, |
| 442 | int32_t* out_memo_index) { |
| 443 | const Scalar value(std::forward<Value>(v)); |
| 444 | auto cmp_func = [value](const Payload* payload) -> bool { |
| 445 | return ScalarHelper<Scalar, 0>::CompareScalars(value, payload->value); |
| 446 | }; |
| 447 | hash_t h = ComputeHash(value); |
| 448 | auto p = hash_table_.Lookup(h, cmp_func); |
| 449 | int32_t memo_index; |
| 450 | if (p.second) { |
| 451 | memo_index = p.first->payload.memo_index; |
| 452 | on_found(memo_index); |
| 453 | } else { |
| 454 | memo_index = size(); |
| 455 | RETURN_NOT_OK(hash_table_.Insert(p.first, h, {value, memo_index})); |
| 456 | on_not_found(memo_index); |
| 457 | } |
| 458 | *out_memo_index = memo_index; |
| 459 | return Status::OK(); |
| 460 | } |
| 461 | |
| 462 | template <typename Value> |
| 463 | Status GetOrInsert(Value&& value, int32_t* out_memo_index) { |