From 5c59bb5b9c89a173c6771d4db84abb53f4615d93 Mon Sep 17 00:00:00 2001 From: Matt Derksen Date: Mon, 16 May 2016 16:36:51 -0500 Subject: Implement std::map::emplace() Change-Id: Id3f42ac3e5edf6c65c420e60ac69d649cbaa1ed0 RTC:153697 Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/24631 Reviewed-by: Christian R. Geddes Tested-by: Jenkins Server Tested-by: FSP CI Jenkins Reviewed-by: A. P. Williams III Reviewed-by: Daniel M. Crowell --- src/include/util/impl/splaytree.H | 36 ++++++++++++++++++++++++++++++++++-- src/include/util/impl/stlmap.H | 27 ++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) (limited to 'src/include/util') diff --git a/src/include/util/impl/splaytree.H b/src/include/util/impl/splaytree.H index 11c801135..9e64cbfc5 100644 --- a/src/include/util/impl/splaytree.H +++ b/src/include/util/impl/splaytree.H @@ -5,7 +5,7 @@ /* */ /* OpenPOWER HostBoot Project */ /* */ -/* Contributors Listed Below - COPYRIGHT 2012,2015 */ +/* Contributors Listed Below - COPYRIGHT 2012,2016 */ /* [+] International Business Machines Corp. */ /* */ /* */ @@ -64,6 +64,7 @@ #include #include #include +#include namespace Util { @@ -113,12 +114,41 @@ namespace Util { } + + /** Variadic copy constructor from a Node. + * + * Initializes the node-links and copies the data storage area + * from the copied nodes. + * + */ + template explicit Node(Args&&... args) + { + parent = child[LEFT] = child[RIGHT] = NULL; + new (&data) T(std::forward(args)...); + } + + /** Copy constructor from a Node. + * + * Initializes the node-links and copies the data storage area + * from the copied node. + * + * non-const form - needed for variadic arguments form above + */ + explicit Node(Node&r) + { + // Don't want to copy the links out of the other node because + // they're from a different tree. + parent = child[LEFT] = child[RIGHT] = NULL; + + new (&data) T(r.data); + }; + /** Copy constructor from a Node. * * Initializes the node-links and copies the data storage area * from the copied node. */ - explicit Node(const Node& r) + explicit Node(const Node&r) { // Don't want to copy the links out of the other node because // they're from a different tree. @@ -139,6 +169,8 @@ namespace Util new (&data) T(v); }; + + /** Access the data storage area as a T&. */ T& data_T() { diff --git a/src/include/util/impl/stlmap.H b/src/include/util/impl/stlmap.H index 392762892..cdf5824e4 100644 --- a/src/include/util/impl/stlmap.H +++ b/src/include/util/impl/stlmap.H @@ -5,7 +5,9 @@ /* */ /* OpenPOWER HostBoot Project */ /* */ -/* COPYRIGHT International Business Machines Corp. 2012,2014 */ +/* Contributors Listed Below - COPYRIGHT 2012,2016 */ +/* [+] International Business Machines Corp. */ +/* */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); */ /* you may not use this file except in compliance with the License. */ @@ -38,6 +40,7 @@ #include #include + #ifndef __UTIL_SPLAYTREE_NS #define __UTIL_SPLAYTREE_NS ::Util::__Util_SplayTree_Impl #endif @@ -329,6 +332,28 @@ namespace Util return (result.first)->second; } + template + std::pair emplace ( Args&&... args ) + { + _Node *p = new _Node(std::forward(args)...); + + _Tree::node * hint_node = nullptr; + + bool rc = iv_tree.find_hint((_Tree::node*)p, hint_node); + if (!rc) + { + // node not found, so insert new one + iv_tree.insert((_Tree::node*)p); + return make_pair(iterator(_TreeItr(&iv_tree, (_Tree::node*)p)), true); + } + else + { + // node already exists, delete new one + delete p; + return make_pair(iterator(_TreeItr(&iv_tree, (_Tree::node*)hint_node)), false); + } + } + std::pair insert(const value_type& x) { _Tree::node* n = NULL; -- cgit v1.2.1