Skip to content

Instantly share code, notes, and snippets.

@JamesBremner
Last active June 23, 2019 17:35
Show Gist options
  • Save JamesBremner/d71b158b32e4dd8ffaf8cbe93cf3f180 to your computer and use it in GitHub Desktop.
Save JamesBremner/d71b158b32e4dd8ffaf8cbe93cf3f180 to your computer and use it in GitHub Desktop.
Layer iterator
#include <iostream>
#include <sstream>
#include <iomanip>
#include <unordered_map>
#include "cRunWatch.h"
#define maxScene 10
#define maxLayer 10
#define maxType 50
#define COUNT_COMPS_FOR_EACH_SCENE_LAYER_TYPE 10
using namespace std;
class Component
{
public:
int myID;
static long long lastID;
Component()
{
myID = ++lastID;
}
};
long long Component::lastID = -1;
class cKey
{
public:
size_t scene;
size_t layer;
size_t type;
long long id;
void Display() const;
};
struct KeyHash
{
public:
size_t operator()(const cKey & key) const
{
// hash considers entire key except id
// means that all components of the same scene, layer and type are in same bucket
// which seems reasonable to me
// Time profiling shows this takes 10% of total time to iterate over a particular layer
// when there are 50,000 components in the map.
return key.scene * maxLayer * maxType + key.layer * maxType + key.type;
}
};
struct KeyEqual
{
public:
bool operator()(const cKey & key1, const cKey & key2) const
{
if( key1.scene != key2.scene )
return false;
if( key1.layer != key2.layer )
return false;
if( key1.type != key2.type )
return false;
if( key1.id != key2.id )
return false;
return true;
}
};
void cKey::Display() const
{
cout << scene <<" "<< layer <<" "<< type <<" "<<id ;
}
int main()
{
raven::set::cRunWatch::Start();
unordered_map< cKey, Component*,
KeyHash, KeyEqual > theMap;
// store components
int insertCount = 0;
cKey key;
for( key.scene = 0; key.scene < maxScene; key.scene++ )
{
for( key.layer = 0; key.layer < maxLayer; key.layer++ )
{
// store specified number of types
for( key.type = 0; key.type < maxType; key.type++ )
{
// store specified number of components for this scene, layer and type
for( int k = 0; k < COUNT_COMPS_FOR_EACH_SCENE_LAYER_TYPE; k++ )
{
insertCount++;
Component* pc = new Component;
key.id = pc->myID;
auto ret = theMap.insert( make_pair( key, pc ));
if( ! ret.second )
{
cout << "insert failed ";
key.Display();
return 1;
}
}
}
}
}
cout << "Map contains " << theMap.size() << "\n";
{
raven::set::cRunWatch r("iterate over layer components");
// iterate over components in a particular scene and layer
key.scene = 3;
key.layer = 2;
// loop over types
for( key.type = 0; key.type < maxType; key.type++ )
{
// loop over all assigned IDs
int count = 0;
for( key.id = 0; key.id <= Component::lastID; key.id++ )
{
auto it = theMap.find( key );
if( it == theMap.end() )
continue;
count++;
}
//cout << "type " << key.type << " found total "<< count << ", ";
if( count != COUNT_COMPS_FOR_EACH_SCENE_LAYER_TYPE )
{
cout << "ERROR! Incorrect component count recovered\n";
return 1;
}
}
}
raven::set::cRunWatch::Report();
return 0;
}
@JamesBremner
Copy link
Author

Iterates over all components in a layer in a map containing 50,000 components in 225 msecs

C:\Users\James\code\bin>so56687921
Map contains 50000
raven::set::cRunWatch code timing profile
Calls           Mean (secs)     Total           Scope
       1        0.224277        0.224277        iterate over layer components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment