Debian Codegraph @

CodeLite/tree.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : tree.h
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #ifndef CODELITE_TREE_H
27 #define CODELITE_TREE_H
28
29 #include "tree_node.h"
30
31 template <typename TKey, typename TData>
32 class Tree
33 {
34 std::map<TKey, TreeNode<TKey, TData>*> m_nodes;
35 TreeNode<TKey, TData>* m_root;
36 public:
37 /**
38 * Construct a tree with root node with Key and data.
39 * \param key Root node key
40 * \param data Root node data
41 */
42 Tree(const TKey& key, const TData& data);
43
44 /**
45 * Destructor, deletes all tree and its sub nodes.
46 */
47 virtual ~Tree();
48
49 /**
50 * Find node in the tree with Key.
51 * \param key Search key
52 * \return node
53 */
54 TreeNode<TKey, TData>* Find(const TKey& key) ;
55
56 /**
57 * Remove node (and all its sub nodes) from the tree using key as the criteria.
58 * This function does not delete the removed node.
59 * \param key search key
60 * \return remove node
61 */
62 TreeNode<TKey, TData>* Remove(const TKey& key);
63
64 /**
65 * Add child node to the tree.
66 * \param key New node key
67 * \param data New node data
68 * \param parent New node parent, if left NULL parent is set to root
69 * \return newly added node
70 */
71 TreeNode<TKey, TData>* AddChild(const TKey& key, const TData& data, TreeNode<TKey, TData>* parent = NULL) ;
72
73 /**
74 * Returns tree root.
75 * \return root node
76 */
77 TreeNode<TKey, TData>* GetRoot() { return m_root;}
78
79 /**
80 * Prints the tree in tabulated format to stream.
81 * \param stream Output stream (default is set to stdout)
82 * \param depth Tab depth (for internal use)
83 */
84 void Print(std::ostream& stream = std::cout , int depth = 0);
85
86 /**
87 * Compare this tree against another tree.
88 * \param targetTree Target tree to compare with
89 * \param deletedItems Array of pairs of items which exist in this tree and not in target tree
90 * \param modifiedItems Array of pairs of items which have the same key but differnt data
91 * \param newItems Aarray of pairs of items which exist in the target tree but not in this tree
92 * \param fromNode If its set to null, comparison will start from this tree root node, if not null,
93 * comparison will compare sub-tree which root of its fromNode
94 */
95 void Compare(Tree* targetTree, std::vector<std::pair<TKey, TData> >& deletedItems, std::vector<std::pair<TKey, TData> >& modifiedItems, std::vector<std::pair<TKey, TData> >& newItems, TreeNode<TKey, TData>* fromNode = NULL);
96
97 /**
98 * Serialize the tree to vector
99 * \param vec output vector
100 */
101 void ToVector(std::vector<std::pair<TKey, TData> >& vec);
102 };
103
104 template <typename TKey, typename TData>
105 Tree<TKey, TData>::Tree(const TKey& key, const TData& data)
106 {
107 m_root = new TreeNode<TKey, TData>(key, data);
108 }
109
110 template <typename TKey, typename TData>
111 Tree<TKey, TData>::~Tree()
112 {
113 delete m_root;
114 }
115
116 template <typename TKey, typename TData>
117 TreeNode<TKey, TData>* Tree<TKey, TData>::Find(const TKey& key)
118 {
119 typename std::map<TKey, TreeNode<TKey, TData>*>::const_iterator iter = m_nodes.find(key);
120 if(iter == m_nodes.end())
121 return NULL;
122 return iter->second;
123 }
124
125 template <typename TKey, typename TData>
126 TreeNode<TKey, TData>* Tree<TKey, TData>::AddChild(const TKey& key, const TData& data, TreeNode<TKey, TData>* parent /*NULL*/)
127 {
128 TreeNode<TKey, TData>* parentNode;
129 (parent == NULL) ? parentNode = m_root : parentNode = parent;
130 TreeNode<TKey, TData>* node = parentNode->AddChild(key, data);
131 m_nodes[key] = node;
132 return node;
133 }
134
135 template <typename TKey, typename TData>
136 TreeNode<TKey, TData>* Tree<TKey, TData>::Remove(const TKey& key)
137 {
138 typename std::map<TKey, TreeNode<TKey, TData>*>::const_iterator iter = m_nodes.find(key);
139 if(iter == m_nodes.end())
140 return NULL;
141
142 // Remove from the map all sub-nodes of the tree
143 TreeWalker<TKey, TData> walker(iter->second);
144
145 for(; !walker.End(); walker++)
146 {
147 typename std::map<TKey, TreeNode<TKey, TData>*>::iterator it = m_nodes.find(walker.GetNode()->GetKey());
148 if(it != m_nodes.end())
149 m_nodes.erase(it);
150 }
151 return m_root->Remove(key);
152 }
153 template <typename TKey, typename TData>
154 void Tree<TKey, TData>::Print(std::ostream& stream , int depth)
155 {
156 m_root->Print(stream, depth);
157 }
158
159 template <typename TKey, typename TData>
160 void Tree<TKey, TData>::ToVector(std::vector<std::pair<TKey, TData> >& vec)
161 {
162 TreeWalker<TKey, TData> walker(GetRoot());
163 for(; !walker.End(); walker++)
164 {
165 if( walker.GetNode()->IsRoot() )
166 continue;
167
168 std::pair<TKey, TData> itemPair;
169 itemPair.first = walker.GetNode()->GetKey();
170 itemPair.second = walker.GetNode()->GetData();
171 vec.push_back( itemPair );
172 }
173 }
174
175 template <typename TKey, typename TData>
176 void Tree<TKey, TData>::Compare(Tree* targetTree, std::vector<std::pair<TKey, TData> >& deletedItems, std::vector<std::pair<TKey, TData> >& modifiedItems, std::vector<std::pair<TKey, TData> >& newItems, TreeNode<TKey, TData>* fromNode)
177 {
178 // we break generic for the sake of thread safety:
179 // we explicitly calling TKey.c_str(), which means that we assume that TKey has such member
180 if(!targetTree){
181 return;
182 }
183
184 deletedItems.clear(); newItems.clear(); modifiedItems.clear();
185
186 TreeNode<TKey, TData>* node;
187
188 fromNode == NULL ? node = GetRoot() : node = fromNode;
189 TreeWalker<TKey, TData> sourceTreeWalker(node);
190 TreeWalker<TKey, TData> targetTreeWalker(targetTree->GetRoot());
191
192 for(; !sourceTreeWalker.End(); sourceTreeWalker++)
193 {
194 if( sourceTreeWalker.GetNode()->IsRoot() )
195 continue;
196
197 TreeNode<TKey, TData>* node = targetTree->Find(sourceTreeWalker.GetNode()->GetKey());
198 if(node == NULL)
199 {
200 // Item does not exist in target tree which means it must been deleted
201 std::pair<TKey, TData> itemPair;
202
203 itemPair.first = sourceTreeWalker.GetNode()->GetKey().c_str();
204 itemPair.second = sourceTreeWalker.GetNode()->GetData();
205 deletedItems.push_back( itemPair );
206 }
207 else
208 {
209 // Compare data
210 if(node->GetData() == sourceTreeWalker.GetNode()->GetData())
211 continue;
212
213 // Data was modified
214 std::pair<TKey, TData> itemPair;
215 itemPair.first = sourceTreeWalker.GetNode()->GetKey().c_str();
216 itemPair.second = node->GetData();
217 modifiedItems.push_back( itemPair );
218 }
219 }
220
221 for(; !targetTreeWalker.End(); targetTreeWalker++)
222 {
223 if(targetTreeWalker.GetNode()->IsRoot())
224 continue;
225
226 if(Find(targetTreeWalker.GetNode()->GetKey()) == NULL)
227 {
228 // Node from target tree does not exist in this tree
229 // which means that this node is new
230 // Data was modified
231 std::pair<TKey, TData> itemPair;
232 itemPair.first = targetTreeWalker.GetNode()->GetKey().c_str();
233 itemPair.second = targetTreeWalker.GetNode()->GetData();
234 newItems.push_back( itemPair );
235 }
236 }
237 }
238
239 #endif // CODELITE_TREE_H