{"id":1280,"date":"2022-02-23T19:12:50","date_gmt":"2022-02-23T11:12:50","guid":{"rendered":"https:\/\/markjohntaylor.com\/blog\/wordpress\/?p=1280"},"modified":"2022-03-05T16:35:39","modified_gmt":"2022-03-05T08:35:39","slug":"how-to-implement-a-real-emplace-routine","status":"publish","type":"post","link":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/2022\/02\/23\/how-to-implement-a-real-emplace-routine\/","title":{"rendered":"How To Implement A Real Emplace Routine"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"705\" src=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-1024x705.png\" alt=\"\" class=\"wp-image-1283\" srcset=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-1024x705.png 1024w, https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-300x207.png 300w, https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-768x529.png 768w, https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image.png 1289w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Above is a piece of code that attempts to implement the emplace family routines. One can insert something like <code>const value_type&amp; (value_type&amp;&amp;) val<\/code> or variadic arguments <code>Args&amp;&amp;... args<\/code>, both of which then can be handled by the <code>insert_leaf_at<\/code> subroutine<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"308\" src=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-1-1024x308.png\" alt=\"\" class=\"wp-image-1286\" srcset=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-1-1024x308.png 1024w, https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-1-300x90.png 300w, https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-1-768x231.png 768w, https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2022\/02\/image-1.png 1142w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Of course, this simple implementation is flawed as it requires an extra move operation. In order to obtain the key from <code>Args&amp;&amp;... args<\/code>, we need to somehow get the value itself which can be a value (set) or a pair of &lt;key, val&gt; values (map).  This is done by using the compile-time template bool constant\/parameter <code>IsMap<\/code>. If we must construct the actual object within the method <code>insert_at_leaf<\/code>, then, well, an extra move operation is inevitable.<\/p>\n\n\n\n<p>The solution to the question is we construct the node in-place, thus the name <code>empalce<\/code> ;) We first construct the node in the heap using the variadic arguments <code>node_ptr z = new_node(\/*parent=*\/nullptr, std::forward&lt;Args&gt;(args)...);<\/code> , then <code>get_key<\/code> from the node by reference. And what is left is we walk through the tree and find the right position to insert the node and reset the parent link. Tada?, there you are. This makes even more sense if we look at the C++ specification where <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/map\">node_type<\/a><\/code> and <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/node_handle\">node_handle<\/a><\/code> were introduced in C++17. Their presence makes implementing <code>emplace<\/code> more easily and naturally.<\/p>\n\n\n\n<p>But the C++ folks are soooo ambitious and if the original one has so many combined benefits (simplicity, flexibility, versatility, etc.) and actually works pretty well (given the overhead of an extra move operation is negligible), we can just keep the way it is :)<\/p>\n\n\n\n<p>Ref: A C++17 compliant <a href=\"https:\/\/github.com\/How-u-doing\/DataStructures\/blob\/master\/Searching\/TreeMap\/AVLtree_impl.h\">AVL<\/a> map container.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Above is a piece of code that attempts to implement the emplace family routines. One can insert something like const value_type&amp; (value_type&amp;&amp;) val or variadic arguments Args&amp;&amp;&#8230; args, both of which then can be handled by the insert_leaf_at subroutine Of course, this simple implementation is flawed as it requires an extra move operation. In order &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/2022\/02\/23\/how-to-implement-a-real-emplace-routine\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How To Implement A Real Emplace Routine&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1280"}],"collection":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":19,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":1402,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1280\/revisions\/1402"}],"wp:attachment":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}