• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ectara

std::unordered_map::insert() Return Values

5 posts in this topic

Can anyone explain why some overloads of the insert() method of std::unordered_map return a pair with a boolean value indicating success, while others that differ only in providing a hint iterator don't return this boolean value? It seems like there'd be an equal chance of failure, so why does only one set of overloads return a boolean value?

0

Share this post


Link to post
Share on other sites

Pretty much you have two variations of return value, well three if you count void.  The single iterator return is actually the 'uncommon' case and the 'hint' is the important bit.  Basically the idea is that you pass in what you believe is the iterator to the item you want to replace and if that iterator references the proper key, the value is replaced without any lookup overhead.  The more common version is the pair return which tells you if the iterator represents a newly inserted item or something you simply replaced the value of.

 

What use is this 'hint' variation, that's tricky but can be useful for optimizations (no guarantee's though according to the standard).  As this is in the beginners forum you should probably ignore the versions which accept hints as it is really an optimization item you will only put to use at a later time.

0

Share this post


Link to post
Share on other sites

Pretty much you have two variations of return value, well three if you count void.  The single iterator return is actually the 'uncommon' case and the 'hint' is the important bit.  Basically the idea is that you pass in what you believe is the iterator to the item you want to replace and if that iterator references the proper key, the value is replaced without any lookup overhead.  The more common version is the pair return which tells you if the iterator represents a newly inserted item or something you simply replaced the value of.

 

What use is this 'hint' variation, that's tricky but can be useful for optimizations (no guarantee's though according to the standard).  As this is in the beginners forum you should probably ignore the versions which accept hints as it is really an optimization item you will only put to use at a later time.

I understand the differences between how the methods work; what I want to know is why the single insertion methods without a hint return a pair with a boolean value indicating success, while the methods accepting hints don't return a boolean value indicating whether or not they failed. From what I can tell, if an element already exists in the container, the insertion fails, and the methods accepting hints are no exception.

 

So, why does one return a bool, and the other does not?

 


As this is in the beginners forum you should probably ignore the versions which accept hints as it is really an optimization item you will only put to use at a later time.

I don't remember posting this in the For Beginners forum...

0

Share this post


Link to post
Share on other sites

 

Pretty much you have two variations of return value, well three if you count void.  The single iterator return is actually the 'uncommon' case and the 'hint' is the important bit.  Basically the idea is that you pass in what you believe is the iterator to the item you want to replace and if that iterator references the proper key, the value is replaced without any lookup overhead.  The more common version is the pair return which tells you if the iterator represents a newly inserted item or something you simply replaced the value of.

 

What use is this 'hint' variation, that's tricky but can be useful for optimizations (no guarantee's though according to the standard).  As this is in the beginners forum you should probably ignore the versions which accept hints as it is really an optimization item you will only put to use at a later time.

I understand the differences between how the methods work; what I want to know is why the single insertion methods without a hint return a pair with a boolean value indicating success, while the methods accepting hints don't return a boolean value indicating whether or not they failed. From what I can tell, if an element already exists in the container, the insertion fails, and the methods accepting hints are no exception.

 

So, why does one return a bool, and the other does not?

 

 

Returning an iterator is the usual insertion return value for any associative container - map, set, multiset, multimap. The iterator-bool pair is a pattern only used by associative containers with unique keys. This looks like a design compromise - the STL creators tried to make container interfaces match. So, I bet adding the iterator-bool pair version for the hint-based methods was a step too far for the designers. They were probably reluctant about the hint versions to begin with. They're only really supposed to be used when you know exactly where you want to insert something - you should know it will succeed. I doubt they wanted two separate versions for such a specialized method among associative containers.

1

Share this post


Link to post
Share on other sites

I understand the differences between how the methods work; what I want to know is why the single insertion methods without a hint return a pair with a boolean value indicating success, while the methods accepting hints don't return a boolean value indicating whether or not they failed. From what I can tell, if an element already exists in the container, the insertion fails, and the methods accepting hints are no exception.
 
So, why does one return a bool, and the other does not?

 

There could be a lot of why questions.

 

Why did they provide a hint version?  The documentation from GCC explains it pretty clearly:

 

In the case of <code>std::unordered_set</code> and <code>std::unordered_map</code> you need to look through all bucket's elements for an equivalent one. If there is none the insertion can be achieved, otherwise the insertion fails. As we always need to loop though all bucket's elements, the hint doesn't tell us if the element is already present, and we don't have any constraint on where the new element is to be inserted, the hint won't be of any help and will then be ignored.

 

In the case of <code>std::unordered_multiset</code> and <code>std::unordered_multimap</code> equivalent elements must be linked together so that the <code>equal_range(const key_type&amp;)</code> can return the range of iterators pointing to all equivalent elements. This is where hinting can be used to point to another equivalent element already part of the container and so skip all non equivalent elements of the bucket. So to be useful the hint shall point to an element equivalent to the one being inserted. The new element will be then inserted right after the hint. Note that because of an implementation detail inserting after a node can require to update the bucket of the following node. To check if the next bucket is to be modified we need to compute following node hash code. So if you want your hint to be really efficient it should be followed by another equivalent element, the implementation will detect this equivalence and won't compute next element hash code.

 

Why does the version that accepts the hint not include a flag indicating if the content was already there?

 

If you already have the value for the hint (as the docs mention above) "it won't be of any help and will then be ignored."  So why provide it?  For consistency. There is no penalty for having it, and adding it allows consistency between all eight containers.

1

Share this post


Link to post
Share on other sites


They're only really supposed to be used when you know exactly where you want to insert something - you should know it will succeed.

So, basically, there's no real difference, and it is left up to the user to ensure that the insertion will succeed for this method only?

 


Why did they provide a hint version?

I do understand that. I wanted to know why they made the return value of one a pair, and left the other without an additional return value. MSDN documentation even says their implementation returns insert(val).first. If they could change both as easily as they did one, why didn't they?

 


Why does the version that accepts the hint not include a flag indicating if the content was already there?



If you already have the value for the hint (as the docs mention above) "it won't be of any help and will then be ignored." So why provide it? For consistency. There is no penalty for having it, and adding it allows consistency between all eight containers.

I don't think the answer truly answers that question, unless this is a roundabout way of saying "Nobody will use this unless it is for compatibility with other containers that don't fail to insert elements, so that's why they didn't bother to change the return value."

 

It has little importance in the grand scheme of things, but I have a hash map class with an interface like unordered_map, and I want to understand why for one of the methods, I return extra information, but the other, I discard it. Basically, I want to make sure I am implementing it right, by understanding why it does what it does.

 

If the answer is "Nobody will call this unless its for compatibility," or "You should know whether or not it fails," then I guess I can accept that.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0