• 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
Stragen

Container Selection

6 posts in this topic

Just after some opinions,

 

I have a 2 element structure that I need to store, and am trying to consider the options available, and am not sure which approach i want to take, hopefully staying within the STD libraries.

 

The information being stored is a string name with an associated matrix. It needs to be searchable, but also referenced by index.

 

So far:

I've implemented this using a MAP, keyed by the string name, the search-ability is brilliant, however the need to access the index has come up.

 

I'm considering a struct based vector with searcher function, though i'd really like to avoid this if there is a more elegant solution available in library.

I've considered a list, however, the data needs to be randomly referenced, and when the index is already known i dont want to have to scan the entire list to get to the last reference (could be well over 5k references). Sets suffer from the same issue as maps, and arrays aren't intrinsically searchable or as expandable as i'd like.

 

I'm expecting that i'm not going to be able to avoid the Struct-Vector with search off the side, but if anyone has another idea, i'm all ears.

 

 

0

Share this post


Link to post
Share on other sites

Depending on your requirements on the indexing, you could get away with sorting the vector based on the string and look up the name with std::binary_search which has the same logarithmic lookup performace as a map but allows linear time indexing into the vector as well. It does, however, require that your indices can be determined from the array, and not the other way around: for example, that all you need is some "arbitrary" unique integer that you can use to reference the object in the vector, instead of having the objects generate their own specific ID.

 

Another option depending on how dynamic the sets of objects are during runtime; keep a map and a vector, and then access the map with the key and the vector with the index.

2

Share this post


Link to post
Share on other sites

Boost has a container that allows multiple types of indexing; I've never used it before.

 

Are you sure you must allow index lookup? How fast does it need to be? How many elements do you expect the container to hold?

Unless your map needs to be ordered, using std::unordered_map is faster for lookups than std::map.

 

If your index-based lookups must be fast, then use std::vector, and use a function to crawl the vector for string-based lookups.

If your string-based lookups must be fast, then use std::unordered_map, and use a function to crawl the vector for ID-based lookups.

If your iterations need to be fast, then use std::vector, and use a function to crawl the vector for string-based lookups.

 

If all need to be fast, and the container is huge, then you could use Boost's container that is basically a map and a vector combined, or roll your own wrapping std::vector and std::unordered_map.

 

By 'use a function', std::find_if(...container..., predicate) would work well, and you can pass a lambda or a functor, or wrap it for convience:

auto it = std::find_if(std::begin(data), std::end(data),
						   [&](const MyStruct &myStruct) -> bool
						   { return (myStruct.name == nameToLookup); });

http://ideone.com/QyaRJC

1

Share this post


Link to post
Share on other sites

There are two options I'd consider:

 

A) Store matrices unordered in a vector. Also store a map of strings to indices. Wrap these two things in your own class.

 

B) Store structs containing with the matrix-string pair in a vector sorted by the string. Use the index to search by index. Use a binary search to find a string.

1

Share this post


Link to post
Share on other sites

If you use a map, you can store pointers to the items which in most case are similar to index's.  That way you have constant time pointer access, and logarithmic time access with the string.

1

Share this post


Link to post
Share on other sites
Another option: a vector of matrices and a std::unordered_map<string,unsigned> that maps strings to indices. That gets you amortized constant time insertion, constant-time access by index and near-constant-time access by string (fast, in any case).
1

Share this post


Link to post
Share on other sites

To answer the question around the need to be indexed, while also look up capable; the resource files that i'm using have multiple ways of referencing the same piece of data...

 

For example: one part of the file might refer to an element by its ID name, with another part referring to its index within the container, the format isnt mine, so i've got no real control over that.

 

this being said, a vector of pointers might be the trick, if the string name is used, use the map to find the reference, if an integer index is used, use the vector of pointers to the map.

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