In traditional C++ style, this performs "insert if not present."
| 956 | |
| 957 | // In traditional C++ style, this performs "insert if not present." |
| 958 | std::pair<iterator, bool> insert(const KeyValuePair& kv) { |
| 959 | std::pair<const_iterator, size_type> p = FindHelper(kv.key()); |
| 960 | // Case 1: key was already present. |
| 961 | if (p.first.node_ != NULL) |
| 962 | return std::make_pair(iterator(p.first), false); |
| 963 | // Case 2: insert. |
| 964 | if (ResizeIfLoadIsOutOfRange(num_elements_ + 1)) { |
| 965 | p = FindHelper(kv.key()); |
| 966 | } |
| 967 | const size_type b = p.second; // bucket number |
| 968 | Node* node = Alloc<Node>(1); |
| 969 | alloc_.construct(&node->kv, kv); |
| 970 | iterator result = InsertUnique(b, node); |
| 971 | ++num_elements_; |
| 972 | return std::make_pair(result, true); |
| 973 | } |
| 974 | |
| 975 | // The same, but if an insertion is necessary then the value portion of the |
| 976 | // inserted key-value pair is left uninitialized. |