Added computing and caching the sequence size

This commit is contained in:
Jesse Beder
2011-09-11 16:21:36 -05:00
parent 3a90454d50
commit ecdd9cc66d
3 changed files with 60 additions and 10 deletions

View File

@@ -59,6 +59,11 @@ namespace YAML
static std::string empty_scalar; static std::string empty_scalar;
private: private:
void compute_seq_size() const;
void compute_map_size() const;
void reset_sequence();
void reset_map();
void convert_sequence_to_map(shared_memory_holder pMemory); void convert_sequence_to_map(shared_memory_holder pMemory);
template<typename T> template<typename T>
@@ -78,6 +83,8 @@ namespace YAML
typedef std::vector<node *> node_seq; typedef std::vector<node *> node_seq;
node_seq m_sequence; node_seq m_sequence;
mutable std::size_t m_seqSize;
// map // map
typedef std::pair<node *, node *> kv_pair; typedef std::pair<node *, node *> kv_pair;
typedef std::list<kv_pair> node_map; typedef std::list<kv_pair> node_map;

View File

@@ -10,7 +10,7 @@ namespace YAML
{ {
std::string node_data::empty_scalar; std::string node_data::empty_scalar;
node_data::node_data(): m_isDefined(false), m_type(NodeType::Null) node_data::node_data(): m_isDefined(false), m_type(NodeType::Null), m_seqSize(0)
{ {
} }
@@ -43,10 +43,10 @@ namespace YAML
m_scalar.clear(); m_scalar.clear();
break; break;
case NodeType::Sequence: case NodeType::Sequence:
m_sequence.clear(); reset_sequence();
break; break;
case NodeType::Map: case NodeType::Map:
m_map.clear(); reset_map();
break; break;
case NodeType::Undefined: case NodeType::Undefined:
assert(false); assert(false);
@@ -70,11 +70,33 @@ namespace YAML
// size/iterator // size/iterator
std::size_t node_data::size() const std::size_t node_data::size() const
{ {
if(!m_isDefined)
return 0; return 0;
switch(m_type) {
case NodeType::Sequence: compute_seq_size(); return m_seqSize;
case NodeType::Map: compute_map_size(); return m_map.size();
default:
return 0;
}
return 0;
}
void node_data::compute_seq_size() const
{
while(m_seqSize < m_sequence.size() && m_sequence[m_seqSize]->is_defined())
m_seqSize++;
}
void node_data::compute_map_size() const
{
} }
const_node_iterator node_data::begin() const const_node_iterator node_data::begin() const
{ {
if(!m_isDefined)
return const_node_iterator();
switch(m_type) { switch(m_type) {
case NodeType::Sequence: return const_node_iterator(m_sequence.begin()); case NodeType::Sequence: return const_node_iterator(m_sequence.begin());
case NodeType::Map: return const_node_iterator(m_map.begin()); case NodeType::Map: return const_node_iterator(m_map.begin());
@@ -84,6 +106,9 @@ namespace YAML
node_iterator node_data::begin() node_iterator node_data::begin()
{ {
if(!m_isDefined)
return node_iterator();
switch(m_type) { switch(m_type) {
case NodeType::Sequence: return node_iterator(m_sequence.begin()); case NodeType::Sequence: return node_iterator(m_sequence.begin());
case NodeType::Map: return node_iterator(m_map.begin()); case NodeType::Map: return node_iterator(m_map.begin());
@@ -93,6 +118,9 @@ namespace YAML
const_node_iterator node_data::end() const const_node_iterator node_data::end() const
{ {
if(!m_isDefined)
return const_node_iterator();
switch(m_type) { switch(m_type) {
case NodeType::Sequence: return const_node_iterator(m_sequence.end()); case NodeType::Sequence: return const_node_iterator(m_sequence.end());
case NodeType::Map: return const_node_iterator(m_map.end()); case NodeType::Map: return const_node_iterator(m_map.end());
@@ -102,6 +130,9 @@ namespace YAML
node_iterator node_data::end() node_iterator node_data::end()
{ {
if(!m_isDefined)
return node_iterator();
switch(m_type) { switch(m_type) {
case NodeType::Sequence: return node_iterator(m_sequence.end()); case NodeType::Sequence: return node_iterator(m_sequence.end());
case NodeType::Map: return node_iterator(m_map.end()); case NodeType::Map: return node_iterator(m_map.end());
@@ -114,7 +145,7 @@ namespace YAML
{ {
if(m_type == NodeType::Undefined || m_type == NodeType::Null) { if(m_type == NodeType::Undefined || m_type == NodeType::Null) {
m_type = NodeType::Sequence; m_type = NodeType::Sequence;
m_sequence.clear(); reset_sequence();
} }
if(m_type != NodeType::Sequence) if(m_type != NodeType::Sequence)
@@ -127,7 +158,7 @@ namespace YAML
{ {
if(m_type == NodeType::Undefined || m_type == NodeType::Null) { if(m_type == NodeType::Undefined || m_type == NodeType::Null) {
m_type = NodeType::Map; m_type = NodeType::Map;
m_map.clear(); reset_map();
} else if(m_type == NodeType::Sequence) { } else if(m_type == NodeType::Sequence) {
convert_sequence_to_map(pMemory); convert_sequence_to_map(pMemory);
} }
@@ -159,7 +190,7 @@ namespace YAML
case NodeType::Null: case NodeType::Null:
case NodeType::Scalar: case NodeType::Scalar:
m_type = NodeType::Map; m_type = NodeType::Map;
m_map.clear(); reset_map();
break; break;
case NodeType::Sequence: case NodeType::Sequence:
convert_sequence_to_map(pMemory); convert_sequence_to_map(pMemory);
@@ -193,11 +224,22 @@ namespace YAML
return false; return false;
} }
void node_data::reset_sequence()
{
m_sequence.clear();
m_seqSize = 0;
}
void node_data::reset_map()
{
m_map.clear();
}
void node_data::convert_sequence_to_map(shared_memory_holder pMemory) void node_data::convert_sequence_to_map(shared_memory_holder pMemory)
{ {
assert(m_type == NodeType::Sequence); assert(m_type == NodeType::Sequence);
m_map.clear(); reset_map();
for(std::size_t i=0;i<m_sequence.size();i++) { for(std::size_t i=0;i<m_sequence.size();i++) {
std::stringstream stream; std::stringstream stream;
stream << i; stream << i;
@@ -207,7 +249,7 @@ namespace YAML
m_map.push_back(kv_pair(&key, m_sequence[i])); m_map.push_back(kv_pair(&key, m_sequence[i]));
} }
m_sequence.clear(); reset_sequence();
m_type = NodeType::Map; m_type = NodeType::Map;
} }
} }

View File

@@ -44,7 +44,8 @@ namespace Test
YAML_ASSERT(node.size() == 3); YAML_ASSERT(node.size() == 3);
YAML_ASSERT(node[0].as<int>() == 10); YAML_ASSERT(node[0].as<int>() == 10);
YAML_ASSERT(node[1].as<std::string>() == "foo"); YAML_ASSERT(node[1].as<std::string>() == "foo");
YAML_ASSERT(node[1].as<std::string>() == "monkey"); YAML_ASSERT(node[2].as<std::string>() == "monkey");
YAML_ASSERT(node.Type() == YAML::NodeType::Sequence);
return true; return true;
} }