Skip to content

Commit c40330a

Browse files
committed
Merge pull request #40 from mpetri/cst-node-child-proxy
added cst child iterator proxy class
2 parents 20e4822 + 367928d commit c40330a

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <sdsl/suffix_trees.hpp>
2+
#include <iostream>
3+
#include <string>
4+
5+
using namespace std;
6+
using namespace sdsl;
7+
8+
typedef cst_sct3<> cst_t;
9+
typedef cst_sada<> csts_t;
10+
11+
int main(int argc, char* argv[])
12+
{
13+
if (argc < 2) {
14+
cout << "usage: "<<argv[0]<< " file" << std::endl;
15+
return 1;
16+
}
17+
18+
cst_t cst;
19+
construct(cst, argv[1], 1);
20+
21+
auto root = cst.root();
22+
23+
for (auto& child: cst.children(root)) {
24+
std::cout << "sct3 id = " << cst.id(child) << std::endl;
25+
}
26+
27+
csts_t csts;
28+
construct(csts, argv[1], 1);
29+
auto roots = csts.root();
30+
for (auto child: csts.children(roots)) {
31+
std::cout << "sada id = " << csts.id(child) << std::endl;
32+
}
33+
}

include/sdsl/cst_sada.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,17 @@ class cst_sada
434434
}
435435
}
436436

437+
//! Return a proxy object which allows iterating over the children of a node
438+
/*! \param v A valid node of the suffix tree.
439+
* \return The proxy object of v containing all children
440+
* \par Time complexity
441+
* \f$ \Order{1}\f$
442+
*/
443+
cst_node_child_proxy<cst_sada> children(const node_type& v) const {
444+
return cst_node_child_proxy<cst_sada>(*this,v);
445+
}
446+
447+
437448
//! Returns the next sibling of node v.
438449
/*!
439450
* \param v A valid node v of the suffix tree.

include/sdsl/cst_sct3.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "iterators.hpp"
2727
#include "lcp.hpp"
2828
#include "bp_support.hpp"
29-
#include "csa_wt.hpp" // for std initialization of cst_sct3
29+
#include "csa_wt.hpp" // for std initialization of cst_sct3
3030
#include "cst_iterators.hpp"
3131
#include "rank_support.hpp"
3232
#include "select_support.hpp"
@@ -556,6 +556,16 @@ class cst_sct3
556556
}
557557
}
558558

559+
//! Return a proxy object which allows iterating over the children of a node
560+
/*! \param v A valid node of the suffix tree.
561+
* \return The proxy object of v containing all children
562+
* \par Time complexity
563+
* \f$ \Order{1}\f$
564+
*/
565+
cst_node_child_proxy<cst_sct3> children(const node_type& v) const {
566+
return cst_node_child_proxy<cst_sct3>(*this,v);
567+
}
568+
559569
//! Returns the next sibling of node v.
560570
/*!
561571
* \param v A valid node v of the suffix tree.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#ifndef INCLUDED_SDSL_SUFFIX_TREE_HELPER
2+
#define INCLUDED_SDSL_SUFFIX_TREE_HELPER
3+
4+
#include <stdint.h>
5+
#include <cstdlib>
6+
#include <cassert>
7+
#include "iterators.hpp"
8+
9+
namespace sdsl
10+
{
11+
12+
13+
template <class t_cst>
14+
class cst_node_child_proxy_iterator
15+
{
16+
public:
17+
using node_type = typename t_cst::node_type;
18+
using const_reference = const node_type;
19+
using iterator_type = cst_node_child_proxy_iterator<t_cst>;
20+
private:
21+
const t_cst& m_cst;
22+
node_type current_node;
23+
public:
24+
cst_node_child_proxy_iterator() = delete;
25+
cst_node_child_proxy_iterator(const t_cst& cst,const node_type& v) : m_cst(cst) , current_node(v) {}
26+
public:
27+
const_reference operator*() const {
28+
return current_node;
29+
}
30+
iterator_type& operator++() {
31+
current_node = m_cst.sibling(current_node);
32+
return *this;
33+
}
34+
bool operator==(const iterator_type& it)const {
35+
return it.current_node == current_node;
36+
}
37+
bool operator!=(const iterator_type& it)const {
38+
return !(*this==it);
39+
}
40+
};
41+
42+
template <class t_cst>
43+
class cst_node_child_proxy
44+
{
45+
public: // types
46+
using iterator_type = cst_node_child_proxy_iterator<t_cst>;
47+
using node_type = typename t_cst::node_type;
48+
using size_type = typename t_cst::size_type;
49+
private: // data
50+
const node_type& m_parent;
51+
const t_cst& m_cst;
52+
public: // constructors
53+
cst_node_child_proxy() = delete;
54+
explicit cst_node_child_proxy(const t_cst& cst,const node_type& v) : m_parent(v) , m_cst(cst) {};
55+
public: // methods
56+
node_type operator[](size_type i) const { return m_cst.select_child(m_parent,i+1); } // enumeration starts with 1 not 0
57+
size_type size() { return m_cst.degree(m_parent); }
58+
iterator_type begin() const { return iterator_type(m_cst,m_cst.select_child(m_parent,1)); }
59+
iterator_type end() const { return iterator_type(m_cst,m_cst.root()); }
60+
};
61+
62+
}
63+
64+
#endif

include/sdsl/suffix_trees.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ void cst_info(const Cst& cst)
149149
* - ..
150150
*/
151151

152+
#include "suffix_tree_helper.hpp"
152153
#include "cst_sct3.hpp"
153154
#include "cst_sada.hpp"
154155

0 commit comments

Comments
 (0)