Map::MapIterator Map::insert(const Map::Pair& pair) { Map::Node *parent = 0; Map::Node **target = &m_root; // starting with root while(*target) { if (m_order(pair, (*target)->value())) { parent = *target; // left smaller value target = &((*target)->m_left); } else if (m_order((*target)->value(), pair)) { parent = *target; // right: larger value target = &((*target)->m_right); } else break; // already there } // actual insertion if (*target) (*target)->m_pair = pair; // replace value if node exists else { *target = new Node(pair, &m_order, parent); // or create new leaf with value ++m_size; } return MapIterator(this, *target); }
bool MapToFirst<K, V>::operator () (const Pair<K, V> &lhs, const Pair<K, V> &rhs){ return m_order(lhs.key(), rhs.key()); }