WARPXM v1.10.0
Loading...
Searching...
No Matches
wmnametree.h
Go to the documentation of this file.
1/*
2 * wmnametree.h
3 * warpm Xcode project
4 *
5 * Created by Noah Reddell on 3/30/11.
6 * Copyright 2011 University of Washington. All rights reserved.
7 *
8 */
9
10#ifndef nametree_hh_
11#define nametree_hh_
12
13#include <map>
14#include <string>
15
29template<typename V> class WmNameTree : public std::map<std::string, V>
30{
31
32public:
37 WmNameTree<V>() : std::map<std::string, V>()
38 {
39 }
40
48 WmNameTree<V> find_children(const std::string& baseNodeName) const
49 {
50 using namespace std;
51 WmNameTree<V> childMatches;
52
53 string prefix(baseNodeName + "."); // parent prefix plus the '.' delimiter
54
55 typename map<string, V>::const_iterator lastMatch = childMatches.begin();
56 typename map<string, V>::const_iterator endpoint = this->end();
57 // later this could be implemented more efficiently by using a different sort
58 // function in the parent map class for now, we will iterate through each member,
59 // checking for components that match the base node prefix
60 for (typename map<string, V>::const_iterator it = this->begin(); it != endpoint;
61 ++it)
62 {
63 if (!it->first.compare(0, prefix.size(), prefix))
64 {
65 // both strings begin with the same prefix, so add this element to the
66 // list of matches - without prefix we use lastMatch as a hint for
67 // insertion. Which should be dead-on, because we are just copying
68 // elemens over in order
69 childMatches.insert(
70 lastMatch++,
71 pair<string, V>(it.first.substr(prefix.size()) /*remove prefix*/,
72 it.second));
73 // TODO: this lastMatch hint is likely no longer good now that I am
74 // stripping prefix. It all comes down to using the defaulet sorting
75 // function for strings. I should probably implement my own for
76 // performance.
77 }
78 }
79
80 return childMatches;
81 }
82
88 unsigned num_children(const std::string& baseNodeName) const
89 {
90 // this will also include grandchildren, etc.
91 WmNameTree<V> childMatches = find_children(baseNodeName);
92 return childMatches.size();
93 }
94
95 /* *** No Member variables!!!!! *** Deriving from a std::map is not recommended,
96 because its destructor is not virtual I chose to do it anyway, because I do not see a
97 need for new members variables while the std::map member functions still make sense
98 */
99
100private:
101};
102
104#endif // nametree_hh_
Provides hierarchical container for T values according to unique string keys.
Definition: wmnametree.h:30
unsigned num_children(const std::string &baseNodeName) const
Return count of all children (and grandchildren, etc.) of a baseNode.
Definition: wmnametree.h:88
WmNameTree< V > find_children(const std::string &baseNodeName) const
Return all children (and grandchildren, etc.) of a baseNode.
Definition: wmnametree.h:48