Skip to content

Commit

Permalink
use a table to lookup the known dims
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgerlek committed Sep 29, 2011
1 parent 5c93447 commit 5b01733
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/Dimension.cpp
Expand Up @@ -332,53 +332,60 @@ KnownDimension s_knownDimensions[] =
};


static void validate();
typedef std::map<DimensionId::Id, boost::uint32_t> Map;
typedef std::pair<DimensionId::Id, boost::uint32_t> Pair;

// BUG: this is too slow
static const KnownDimension& lookupKnownDimension(const DimensionId::Id& id)
static Map map;

static void buildMap()
{
validate();
// only do this once
if (map.size() != 0)
return;

int i=0;
boost::uint32_t i=0;
while (s_knownDimensions[i].id != DimensionId::Undefined)
{
const KnownDimension& kd = s_knownDimensions[i];
if (kd.id == id) return kd;
assert(map.find(s_knownDimensions[i].id) == map.end());
Pair pair(s_knownDimensions[i].id,i);
map.insert(pair);
++i;
}
throw pdal_error("Dimension not found");

return;
}

static bool hasKnownDimension(const DimensionId::Id& id)

// BUG: this is too slow
static const KnownDimension& lookupKnownDimension(const DimensionId::Id& id)
{
int i=0;
while (s_knownDimensions[i].id != DimensionId::Undefined)
buildMap();

Map::const_iterator iter = map.find(id);
if (iter == map.end())
{
const KnownDimension& kd = s_knownDimensions[i];
if (kd.id == id) return true;
++i;
throw pdal_error("Dimension not found");
}
return false;
assert(iter->first == id);
int index = iter->second;
const KnownDimension& kd = s_knownDimensions[index];
assert(kd.id == id);
return kd;
}

static void validate()
static bool hasKnownDimension(const DimensionId::Id& id)
{
static std::map<DimensionId::Id, int> map;
buildMap();

// only do this once
if (map.size() == 0)
{
int i=0;
while (s_knownDimensions[i].id != DimensionId::Undefined)
{
assert(map.find(s_knownDimensions[i].id) == map.end());
std::pair<DimensionId::Id,int> pair(s_knownDimensions[i].id,i);
map.insert(pair);
++i;
}
Map::const_iterator iter = map.find(id);
if (iter == map.end())
{
return false;
}
return true;
}


// --------------------------------------------------------------------------

Dimension::Dimension(DimensionId::Id id)
Expand Down

0 comments on commit 5b01733

Please sign in to comment.