Code::Blocks  SVN r11506
searchtree.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  */
5 
6 #ifndef SEARCHTREE_H
7 #define SEARCHTREE_H
8 
9 #include "prep.h"
10 #include <wx/string.h>
11 
12 #include <vector>
13 #include <map>
14 #include <set>
15 
17 typedef size_t nSearchTreeNode;
19 typedef size_t nSearchTreeLabel;
20 
21 class SearchTreeNode;
22 class BasicSearchTree;
24 
28 typedef std::map<wxChar, nSearchTreeNode, std::less<wxChar> > SearchTreeLinkMap;
29 
30 
35 typedef std::vector<SearchTreeNode*> SearchTreeNodesArray;
36 
40 typedef std::vector<SearchTreePoint> SearchTreePointsArray;
41 
46 typedef std::map<size_t,size_t, std::less<size_t> > SearchTreeItemsMap;
47 
54 typedef std::vector<wxString> SearchTreeLabelsArray;
55 
58 {
59 public:
62 
65 
68 
70  bool IsValid();
71 
72  bool FindPrev(bool includechildren = true);
73 
74  bool FindNext(bool includechildren = true);
75 
77  const nSearchTreeNode& operator* () const { return m_CurNode; }
78 
80  const BasicSearchTreeIterator& operator++() { FindNext(); return *this; }
81 
83  const BasicSearchTreeIterator& operator--() { FindPrev(); return *this; }
84 
86  bool FindNextSibling();
87 
89  bool FindPrevSibling();
90 
92  bool FindSibling(wxChar ch);
93 
95  bool Eof() { return (!IsValid() || m_Eof); }
96 
97 protected:
99  bool m_Eof;
101  size_t m_LastTreeSize;
103 };
104 
128 {
129 public:
131  size_t depth;
132  SearchTreePoint(): n(0), depth(0) {}
133  SearchTreePoint(nSearchTreeNode nn, size_t dd) { n = nn; depth = dd; }
134 };
135 
158 {
159  friend class BasicSearchTree;
161 public:
162  SearchTreeNode();
163  SearchTreeNode(unsigned int depth,
164  nSearchTreeNode parent,
165  nSearchTreeLabel label,
166  unsigned int labelstart,
167  unsigned int labellen);
168  virtual ~SearchTreeNode();
170  nSearchTreeNode GetParent() const { return m_Parent; }
171 
173  void SetParent(nSearchTreeNode newparent) { m_Parent = newparent; }
174 
179  nSearchTreeNode GetChild(wxChar ch);
180 
182  size_t GetItemNo(size_t depth);
183 
185  size_t AddItemNo(size_t depth, size_t itemno);
186 
188  SearchTreeNode* GetParent(const BasicSearchTree* tree) const;
189 
193  SearchTreeNode* GetChild(BasicSearchTree* tree,wxChar ch);
194 
196  wxString GetLabel(const BasicSearchTree* tree) const;
197 
199  wxChar GetChar(const BasicSearchTree* tree) const;
200 
202  const wxString& GetActualLabel(const BasicSearchTree* tree) const;
203 
205  nSearchTreeLabel GetLabelNo() const { return m_Label; }
206 
208  unsigned int GetLabelStart() const { return m_LabelStart; }
209 
211  unsigned int GetLabelLen() const { return m_LabelLen; }
212 
214  void SetLabel(nSearchTreeLabel label, unsigned int labelstart, unsigned int labellen);
215 
217  unsigned int GetDepth() const { return m_Depth; }
218 
220  void RecalcDepth(BasicSearchTree* tree);
221 
223  void UpdateItems(BasicSearchTree* tree);
224 
228  unsigned int GetLabelStartDepth() const;
229 
231  bool IsLeaf() const { return m_Children.empty() && (m_Depth != 0); }
232 
237  unsigned int GetDeepestMatchingPosition(BasicSearchTree* tree, const wxString& s, unsigned int StringStartDepth);
238 
240  wxString Serialize(BasicSearchTree* tree, nSearchTreeNode node_id, bool withchildren = false);
242  void Dump(BasicSearchTree* tree, nSearchTreeNode node_id, const wxString& prefix, wxString& result);
243 
244  static wxString SerializeString(const wxString& s);
245  static wxString U2S(unsigned int u);
246  static wxString I2S(int i);
247  static bool UnSerializeString(const wxString& s,wxString& result);
248  static bool S2U(const wxString& s,unsigned int& u);
249  static bool S2I(const wxString& s,int& i);
250 
251 protected:
253  unsigned int m_Depth;
254 
257 
260 
262  unsigned int m_LabelStart;
263 
265  unsigned int m_LabelLen;
266 
271 };
272 
277 {
278  friend class SearchTreeNode;
280 public:
281  BasicSearchTree();
282  virtual ~BasicSearchTree();
283  virtual size_t size() const { return m_Points.size(); }
284  virtual size_t GetCount() const { return m_Points.size(); }
285  virtual void clear();
286 
290  size_t insert(const wxString& s);
291 
293  bool HasItem(const wxString& s);
294 
296  size_t count(const wxString& s) { return HasItem(s) ? 1 : 0; }
297 
299  size_t GetItemNo(const wxString& s);
300 
302  const wxString GetString(size_t n) const;
303 
308  size_t FindMatches(const wxString& s, std::set<size_t>& result, bool caseSensitive, bool is_prefix);
309 
311  wxString SerializeLabels();
312 
314  wxString dump();
315 
316 protected:
320  virtual SearchTreeNode* CreateNode(unsigned int depth,
321  nSearchTreeNode parent,
322  nSearchTreeLabel label,
323  unsigned int labelstart,
324  unsigned int labellen);
325 
330  wxString GetString(const SearchTreePoint& nn, nSearchTreeNode top = 0) const;
331 
335  SearchTreeNode* GetNode(nSearchTreeNode n, bool NullOnZero = false);
336 
342  bool FindNode(const wxString& s, nSearchTreeNode nparent, SearchTreePoint* result);
343 
345  SearchTreePoint AddNode(const wxString& s, nSearchTreeNode nparent = 0);
346 
348  wxString SerializeLabel(nSearchTreeLabel labelno);
349 
350 private:
352  void CreateRootNode();
353 
360  nSearchTreeNode SplitBranch(nSearchTreeNode n, size_t depth);
361 
362 protected:
369 };
374 template <class T> class SearchTree : public BasicSearchTree
375 {
376 public:
377  SearchTree();
378  virtual ~SearchTree();
379  virtual void clear();
380  size_t GetCount() const;
381  virtual size_t size() const;
382  bool SaveCacheTo(const wxString& filename);
383  bool LoadCacheFrom(const wxString& filename);
384  wxString Serialize();
385 
387  T GetItem(const wxString& s);
388 
390  T GetItem(const wxChar* s);
391 
393  size_t AddItem(const wxString& s, T item, bool replaceexisting = false);
394 
396  T& GetItemAtPos(size_t i);
397 
399  void SetItemAtPos(size_t i,T item);
400 
402  T& operator[](const wxString& s);
403 
405  virtual wxString SerializeItem(cb_unused size_t idx) { return wxString(_T("")); }
406 
408  virtual void* UnserializeItem(cb_unused const wxString& s) { return NULL; }
409 
410 protected:
411  std::vector<T> m_Items;
412 
414  virtual void ClearItems();
415 
417  virtual bool AddFirstNullItem();
418 };
419 
421 {
422  m_Items.clear();
424 }
425 
426 template <class T> SearchTree<T>::~SearchTree()
427 {
428  ClearItems();
429 }
430 
431 template <class T> void SearchTree<T>::clear()
432 {
433  ClearItems();
436 }
437 
438 template <class T> size_t SearchTree<T>::GetCount() const
439 {
440  size_t result = m_Items.size() -1;
441  return result;
442 }
443 
444 template <class T> size_t SearchTree<T>::size() const
445 {
446  size_t result = m_Items.size() -1;
447  return result;
448 }
449 
450 template <class T> bool SearchTree<T>::SaveCacheTo(const wxString& filename)
451 {
452  return true;
453 }
454 
455 template <class T> bool SearchTree<T>::LoadCacheFrom(const wxString& filename)
456 {
457  return true;
458 }
459 
460 template <class T> T SearchTree<T>::GetItem(const wxChar* s)
461 {
462  wxString tmps(s);
463  return GetItem(tmps);
464 }
465 
466 template <class T> T SearchTree<T>::GetItem(const wxString& s)
467 {
468  size_t itemno = GetItemNo(s);
469  if (!itemno && !s.empty())
470  return T();
471  return GetItemAtPos(itemno);
472 }
473 
474 template <class T> size_t SearchTree<T>::AddItem(const wxString& s, T item, bool replaceexisting)
475 {
476  size_t itemno = insert(s);
477 
478  if (itemno > m_Items.size())
479  m_Items.resize(itemno);
480  else if (itemno == m_Items.size())
481  m_Items.push_back(item);
482  else if (replaceexisting)
483  m_Items[itemno] = item;
484 
485  return itemno;
486 }
487 
488 template <class T> T& SearchTree<T>::GetItemAtPos(size_t i)
489 {
490  if (i>=m_Items.size() || i < 1)
491  i = 0;
492  return m_Items[i];
493 }
494 
495 template <class T> void SearchTree<T>::SetItemAtPos(size_t i,T item)
496 {
497  m_Items[i]=item;
498 }
499 
501 template <class T> void SearchTree<T>::ClearItems()
502 {
503  m_Items.clear();
504 }
505 
506 template <class T> bool SearchTree<T>::AddFirstNullItem()
507 {
508  T newvalue;
509  m_Items.push_back(newvalue);
510  return true;
511 }
512 
513 template <class T> T& SearchTree<T>::operator[](const wxString& s)
514 {
515  size_t curpos = GetItemNo(s);
516  if (!curpos)
517  {
518  T newitem;
519  curpos = AddItem(s, newitem);
520  }
521 
522  return m_Items[curpos];
523 }
524 
525 template <class T> wxString SearchTree<T>::Serialize()
526 {
527  wxString result;
528  result << _T("<SearchTree>\n");
529  result << SerializeLabels();
530  result << _T("<nodes>\n");
531  for (size_t i = 0; i < m_Nodes.size(); ++i)
532  result << m_Nodes[i]->Serialize(this, i, false);
533  result << _T("</nodes>\n");
534  result << _T(" <items>\n");
535  for (size_t i = 1; i < m_Items.size(); ++i)
536  result << SerializeItem(i);
537  result << _T(" </items>\n");
538  result << _T("</SearchTree>\n");
539  return result;
540 }
541 
542 #endif // SEARCHTREE_H
unsigned int GetDepth() const
the depth of node is the string length from root node to the last character of the incoming edge ...
Definition: searchtree.h:217
unsigned int GetLabelLen() const
the length of the incoming label, in the above example, it is 4 for node"hysi"(2) ...
Definition: searchtree.h:211
bool FindPrev(bool includechildren=true)
Definition: searchtree.cpp:46
nSearchTreeNode n
Definition: searchtree.h:130
nSearchTreeLabel m_Label
the string index, the edge is a sub-string of the label in the label array
Definition: searchtree.h:259
virtual void clear()
Gets the number of items stored.
Definition: searchtree.h:431
this is a class template derived from BasicSearchTree class, the type T was stored in a vector...
Definition: searchtree.h:374
size_t GetItemNo(const wxString &s)
Gets the array position defined by s.
Definition: searchtree.cpp:742
bool IsValid()
check to see whether the last newest added node is the last element of the node array ...
Definition: searchtree.cpp:39
T & operator[](const wxString &s)
Gets the item found at position s.
Definition: searchtree.h:513
bool FindNextSibling()
go to the next node under the same parent&#39;s link map
Definition: searchtree.cpp:139
T & GetItemAtPos(size_t i)
Gets the item found at position i, the i is the item index.
Definition: searchtree.h:488
std::vector< SearchTreePoint > SearchTreePointsArray
SearchTreePointsArray contains a list of tree points defining strings (keys),.
Definition: searchtree.h:40
size_t count(const wxString &s)
std::map compatibility for the above
Definition: searchtree.h:296
bool Eof()
reach the end of the tree
Definition: searchtree.h:95
virtual void clear()
Gets the number of items stored.
Definition: searchtree.cpp:542
BasicSearchTreeIterator()
default constructor
Definition: searchtree.cpp:15
bool FindNext(bool includechildren=true)
Definition: searchtree.cpp:93
SearchTreePoint(nSearchTreeNode nn, size_t dd)
Definition: searchtree.h:133
virtual size_t GetCount() const
How many string keys are stored.
Definition: searchtree.h:284
bool FindPrevSibling()
go to the previous node under the same parent&#39;s link map
Definition: searchtree.cpp:168
#define _T(string)
SearchTreeItemsMap m_Items
depth->item number map,
Definition: searchtree.h:270
SearchTreeNodesArray m_Nodes
Nodes array.
Definition: searchtree.h:366
bool IsLeaf() const
check to see this node is a leaf node.
Definition: searchtree.h:231
nSearchTreeNode GetParent() const
return the parent node index
Definition: searchtree.h:170
bool SaveCacheTo(const wxString &filename)
Same as GetCount.
Definition: searchtree.h:450
bool empty() const
size_t AddItem(const wxString &s, T item, bool replaceexisting=false)
Adds an item to position defined by s and return the item number.
Definition: searchtree.h:474
std::vector< wxString > SearchTreeLabelsArray
SearchTreeLabelsArray contains the labels used by the nodes, each node contains an incoming edge stri...
Definition: searchtree.h:54
SearchTreeLabelsArray m_Labels
Labels used by the nodes&#39; incoming edges.
Definition: searchtree.h:364
wxUSE_UNICODE_dependent wxChar
SearchTreeLinkMap m_Children
link to descent nodes,
Definition: searchtree.h:268
unsigned int m_Depth
the string length from the root node to the current node (end of the edge)
Definition: searchtree.h:253
virtual ~SearchTree()
Definition: searchtree.h:426
std::map< wxChar, nSearchTreeNode, std::less< wxChar > > SearchTreeLinkMap
SearchTreeLinkMap is the list of the edges towards child nodes.
Definition: searchtree.h:23
void SetItemAtPos(size_t i, T item)
Replaces the item found at position(index) i.
Definition: searchtree.h:495
bool FindSibling(wxChar ch)
check to see a sibling node with the first character &#39;ch&#39; exists
Definition: searchtree.cpp:199
const BasicSearchTreeIterator & operator--()
go to previous node
Definition: searchtree.h:83
bool m_Eof
current pointed node index
Definition: searchtree.h:99
std::vector< SearchTreeNode * > SearchTreeNodesArray
SearchTreeNodesArray contains all the nodes for a search tree, currently the vector should have all t...
Definition: searchtree.h:35
This class is generally a string -> size_t map, the tree details (graph) is already show in the decla...
Definition: searchtree.h:276
virtual size_t size() const
Gets the number of items stored.
Definition: searchtree.h:444
size_t GetCount() const
Clears the tree.
Definition: searchtree.h:438
virtual void * UnserializeItem(cb_unused const wxString &s)
Unserializes the items to be stored.
Definition: searchtree.h:408
size_t nSearchTreeNode
node index, we hold the node address in a vector and reference it by index
Definition: searchtree.h:17
size_t nSearchTreeLabel
label index, we put the labels (strings) in a vector, and access them by index
Definition: searchtree.h:19
SearchTreeNode * m_LastAddedNode
For checking validity.
Definition: searchtree.h:102
BasicSearchTree * m_Tree
whether the iterator reaches end of tree
Definition: searchtree.h:100
std::map< size_t, size_t, std::less< size_t > > SearchTreeItemsMap
SearchTreeItemsMap contains all the items belonging to an node, the key is the depth of the point...
Definition: searchtree.h:46
unsigned int m_LabelLen
label length in the string
Definition: searchtree.h:265
unsigned int m_LabelStart
label start index in the string
Definition: searchtree.h:262
wxString Serialize()
Loads the Tree and items from a file.
Definition: searchtree.h:525
virtual ~BasicSearchTreeIterator()
destructor
Definition: searchtree.h:67
std::vector< T > m_Items
Definition: searchtree.h:411
virtual void ClearItems()
The actual stored items.
Definition: searchtree.h:501
size_t m_LastTreeSize
pointer to tree instance
Definition: searchtree.h:101
void SetParent(nSearchTreeNode newparent)
set the parent node index
Definition: searchtree.h:173
unsigned int GetLabelStart() const
the first character index in the full label
Definition: searchtree.h:208
wxString SerializeLabels()
Serializes the labels into an XML-compatible string.
SearchTreePointsArray m_Points
Points array.
Definition: searchtree.h:368
size_t insert(const wxString &s)
Clear items and tree.
Definition: searchtree.cpp:822
virtual bool AddFirstNullItem()
Adds a null item to position 0.
Definition: searchtree.h:506
nSearchTreeLabel GetLabelNo() const
the index of the full label in tree->m_Labels array
Definition: searchtree.h:205
virtual wxString SerializeItem(cb_unused size_t idx)
Serializes the stored items.
Definition: searchtree.h:405
SearchTreeIterator lets us iterate through the nodes of a BasicSearchTree.
Definition: searchtree.h:57
T GetItem(const wxString &s)
Gets the item at position defined by key string s.
Definition: searchtree.h:466
nSearchTreeNode m_Parent
parent node index
Definition: searchtree.h:256
This class represents a node of the tree, we still take an example E.g.
Definition: searchtree.h:157
This class is used to access items of the tree, each node may contains a lot of items, E.g.
Definition: searchtree.h:127
const nSearchTreeNode & operator*() const
overload the *() operator, get the node index pointed by the iterator
Definition: searchtree.h:77
SearchTreePoint()
At what depth is the string&#39;s end located?
Definition: searchtree.h:132
bool LoadCacheFrom(const wxString &filename)
Stores the Tree and items into a file.
Definition: searchtree.h:455
const BasicSearchTreeIterator & operator++()
go to next node
Definition: searchtree.h:80
#define NULL
Definition: prefix.cpp:59
nSearchTreeNode m_CurNode
Definition: searchtree.h:98
size_t depth
Which node are we pointing to?
Definition: searchtree.h:131
virtual size_t size() const
Definition: searchtree.h:283