forked from simongog/sdsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cst_sct3.hpp
1195 lines (1098 loc) · 49.8 KB
/
cst_sct3.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sdsl - succinct data structures library
Copyright (C) 2010 Simon Gog
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ .
*/
/*! \file cst_sct3.hpp
\brief cst_sct3.hpp contains an implementation of the interval based CST.
\author Simon Gog
*/
#ifndef INCLUDED_SDSL_CST_SCT3
#define INCLUDED_SDSL_CST_SCT3
#include "int_vector.hpp"
#include "algorithms.hpp"
#include "iterators.hpp"
#include "lcp.hpp"
#include "bp_support.hpp"
#include "csa_wt.hpp" // for std initialization of cst_sct3
#include "cst_iterators.hpp"
#include "rank_support.hpp"
#include "select_support.hpp"
#include "util.hpp"
#include "sdsl_concepts.hpp"
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cstring> // for strlen
#include <iomanip>
#include <iterator>
#include <stack> // for the calculation of the balanced parentheses sequence
#include <ostream>
namespace sdsl
{
// Declaration of the CST's node type
template<class t_int = int_vector<>::size_type>
struct bp_interval;
//! A class for the Compressed Suffix Tree (CST) proposed by Ohlebusch and Gog.
/*!
* \tparam t_csa Type of a CSA (member of this type is accessible via
* member `csa`, default class is sdsl::csa_sada).
* \tparam t_lcp Type of a LCP structure (member is accessible via member
* `lcp`, default class is sdsl::lcp_support_sada),
* \tparam t_bp_support Type of a BPS structure (member accessible via member
* `bp_support`, default class is sdsl::bp_support_sada),
* \tparam t_rank Type of rank structure which supports the bitvector
* which indicates the leftmost child of the nodes.
*
* It also contains a sdsl::bit_vector which represents the BP sequence of the
* Super-Cartesian tree of the LCP array. This bitvector can be accessed via
* the member `bp`. Another sdsl::bit_vector stores information, if a node is
* the leftmost child of another node. This bitvector can be access via the
* member first_child_bv and takes n bits.
*
* A node \f$v\f$ of the csa_sct is represented by an sdsl::bp_interval. The
* size of the sdsl::cst_sct3 is smaller than the size of a sdsl::cst_sada
* since the tree topology needs only \f$2n+n=3n\f$ bits in contrast to the
* \f$4n\f$ bits in sdsl::cst_sada.
*
* \par Reference
* Enno Ohlebusch, Johannes Fischer, Simon Gog:
* CST++.
* SPIRE 2010: 322-333
*
* \par Applications of the CST
* The compressed suffix tree could be used for string matching and many other
* application in sequence analysis. 17 applications are in the book
* "Algorithms on Strings, Trees, and Sequences" of Dan Gusfield.
*
* @ingroup cst
*/
template<class t_csa = csa_wt<>,
class t_lcp = lcp_dac<>,
class t_bp_support = bp_support_sada<>,
class t_rank = rank_support_v5<>
>
class cst_sct3
{
public:
typedef cst_dfs_const_forward_iterator<cst_sct3> const_iterator;
typedef cst_bottom_up_const_forward_iterator<cst_sct3> const_bottom_up_iterator;
typedef typename t_csa::size_type size_type;
typedef ptrdiff_t difference_type;
typedef t_csa csa_type;
typedef typename t_lcp::template type<cst_sct3>::lcp_type lcp_type;
typedef t_bp_support bp_support_type;
typedef typename t_csa::char_type char_type;
typedef bp_interval<size_type> node_type; //!< Type for the nodes in the tree
typedef t_rank fc_rank_support_type;
typedef typename t_csa::alphabet_type::comp_char_type comp_char_type;
typedef typename t_csa::alphabet_type::sigma_type sigma_type;
typedef typename t_csa::alphabet_category alphabet_category;
typedef cst_tag index_category;
private:
csa_type m_csa;
lcp_type m_lcp;
bit_vector m_bp;
bp_support_type m_bp_support;
bit_vector m_first_child; // Note: no rank structure is needed for the first_child bit_vector, except for id()
fc_rank_support_type m_first_child_rank;
sigma_type m_sigma;
size_type m_nodes;
void copy(const cst_sct3& cst) {
m_csa = cst.m_csa;
copy_lcp(m_lcp, cst.m_lcp, *this);
m_bp = cst.m_bp;
m_bp_support = cst.m_bp_support;
m_bp_support.set_vector(&m_bp);
m_first_child = cst.m_first_child;
m_first_child_rank = cst.m_first_child_rank;
m_first_child_rank.set_vector(&m_first_child);
m_sigma = cst.m_sigma;
m_nodes = cst.m_nodes;
}
// Get the first l index of a [i,j] interval.
/* I.e. given an interval [i,j], the function returns the position of
* the smallest entry lcp[k] with \f$ i<k\leq j \f$
* \par Time complexity
* \f$ \Order{1} \f$
*/
inline size_type get_first_l_index(const node_type& node, size_type& kpos, size_type& ckpos)const {
if (node.cipos > node.jp1pos) { // corresponds to m_lcp[i] <= m_lcp[j+1]
ckpos = node.jp1pos-1;
} else { // corresponds to m_lcp[i] > m_lcp[j+1]
ckpos = node.cipos-1;
}
assert(m_bp[ckpos]==0);
kpos = m_bp_support.find_open(ckpos);
return m_bp_support.rank(kpos)-1;
}
// Get the i-th l-index of a node
// if there exists no ith l-index return node.j+1
size_type get_ith_l_index(const node_type& node, size_type i, size_type& kpos, size_type& ckpos)const {
uint64_t children = 0;
assert(i > 0);
if (node.cipos > node.jp1pos) { // corresponds to m_lcp[i] <= m_lcp[j+1]
ckpos = node.jp1pos-1;
} else { // corresponds to m_lcp[i] > m_lcp[j+1]
ckpos = node.cipos-1;
}
assert(m_bp[ckpos] == 0); // at least the first l-index should be present, i.e. node is not leaf
if (1 == i) {
kpos = m_bp_support.find_open(ckpos);
return m_bp_support.rank(kpos)-1;
} else { // i > 1 // TODO for integer-alphabets: replace this linear search
// numbers of closing parentheses - 1 = index of first child in m_first_child
size_type r = ckpos - m_bp_support.rank(ckpos);
if (r+1 >= i) { // if there exist more than i l-indices
// check if m_first_child[r-i+1..r-1] consists of zeros
const uint64_t* p = m_first_child.data() + (r>>6);
uint8_t offset = r&0x3F;
uint64_t w = (*p) & bits::lo_set[offset];
if (w) {
children = offset - bits::hi(w) + 1;
} else if (m_first_child.data() == p) { // w==0 and we are in the first word
children = offset + 2; // since bits::hi(w)=-1 holds
} else {
children = offset + 2;
while (p > m_first_child.data()) {
w = *(--p);
if (0==w)
children += 64;
else {
children += (63-bits::hi(w));
break;
}
}
children += (w==0);
}
if (i < children) { // there exists an i-th l-index
ckpos -= (i-1);
assert(m_bp[ckpos] == 0);
kpos = m_bp_support.find_open(ckpos);
return m_bp_support.rank(kpos)-1;
}
}
// if i >= children
kpos = node.jp1pos;
if (kpos < m_bp.size())
ckpos = m_bp_support.find_close(kpos);
else
ckpos = m_bp.size();
return node.j+1;
}
}
// Position of the first l-index of a l-[i,j] interval in the BP.
/* \par Time complexity
* \f$ \Order{1} \f$
*/
inline size_type closing_pos_of_first_l_index(const node_type& node)const {
if (node.cipos > node.jp1pos) { // corresponds to m_lcp[i] <= m_lcp[j+1]
return node.jp1pos-1;
} else { // corresponds to m_lcp[i] > m_lcp[j+1]
return node.cipos-1;
}
}
// Get the next smaller value.
/*
* \param i Position in the original vector.
* \param ipos Position of the corresponding opening parenthesis in BP.
* \return Position of the next smaller value in [i+1..n-1], and n when
* no such value exists.
* \par Time complexity
* \f$ \Order{1} \f$
*/
// possible optimization: calculate also position of nsv,
// i.e. next ( following position cipos
inline size_type nsv(SDSL_UNUSED size_type i, size_type ipos)const {
size_type cipos = m_bp_support.find_close(ipos);
size_type result = m_bp_support.rank(cipos);
return result;
}
// Get the previous smaller value.
/*
* \param i Position in the original vector.
* \param ipos Corr
* \par Time complexity
* \f$ \Order{\frac{\sigma}{w}} \f$, where w=64 is the word size,
* can be implemented in \f$\Order{1}\f$ with rank and select.
*/
inline size_type psv(SDSL_UNUSED size_type i, size_type ipos,
size_type cipos, size_type& psvpos,
size_type& psvcpos)const {
// if lcp[i]==0 => psv is the 0-th index by definition
if ((cipos + (size_type)m_sigma) >= m_bp.size()) {
psvpos = 0;
psvcpos = m_bp.size()-1;
return 0;
}
if (m_bp[cipos+1]) {
psvpos = m_bp_support.enclose(ipos);
psvcpos = m_bp_support.find_close(psvpos);
return m_bp_support.rank(psvpos)-1;
}
// r0 = index of clothing parenthesis in m_first_child
size_type r0 = cipos - m_bp_support.rank(cipos);
size_type next_first_child = 0;
const uint64_t* p = m_first_child.data() + (r0>>6);
uint64_t w = (*p) >> (r0&0x3F);
if (w) { // if w!=0
next_first_child = cipos + bits::lo(w);
if (cipos == next_first_child and m_bp[next_first_child+1]) {
psvpos = m_bp_support.enclose(ipos);
psvcpos = m_bp_support.find_close(psvpos);
return m_bp_support.rank(psvpos)-1;
}
} else {// TODO for integer-alphabets: replace this linear process by a binary search
cipos += 64-(r0&0x3F);
++p;
while (!(w=*p)) { // while w==0
++p;
cipos += 64;
}
next_first_child = cipos + bits::lo(w);
}
if (!m_bp[next_first_child+1]) { // if next parenthesis is a closing one
psvcpos = next_first_child+1;
psvpos = m_bp_support.find_open(psvcpos);
return m_bp_support.rank(psvpos)-1;
} else {
psvpos = m_bp_support.enclose(m_bp_support.find_open(next_first_child));
psvcpos = m_bp_support.find_close(psvpos);
return m_bp_support.rank(psvpos)-1;
}
}
// Range minimum query based on the rr_enclose method.
/* \par Time complexity
* \f$ \Order{\rrenclose} \f$
*/
inline size_type rmq(size_type l, size_type r)const {
size_type i = m_bp_support.select(l+1);
size_type j = m_bp_support.select(r+1);
size_type fc_i = m_bp_support.find_close(i);
if (j < fc_i) { // i < j < find_close(j) < find_close(i)
return l;
} else { // i < find_close(i) < j < find_close(j)
size_type ec = m_bp_support.rr_enclose(i,j);
if (ec == m_bp_support.size()) {// no restricted enclosing pair found
return r;
} else { // found range restricted enclosing pair
return m_bp_support.rank(ec)-1; // subtract 1, as the index is 0 based
}
}
}
public:
const csa_type& csa; //!< Underlying CSA
const lcp_type& lcp; //!< Underlying LCP array
const bit_vector& bp; //!< Underlying BP of the SCT
const bp_support_type& bp_support;//!< BPS for BP
const bit_vector& first_child_bv;
const fc_rank_support_type& first_child_rank;
/*! \defgroup cst_sct3_constructors Constructors of cst_sct3 */
/* @{ */
//! Default Constructor
cst_sct3(): csa(m_csa), lcp(m_lcp), bp(m_bp), bp_support(m_bp_support),
first_child_bv(m_first_child),
first_child_rank(m_first_child_rank) {}
//! Construct CST from file_map
cst_sct3(cache_config& cache, bool build_only_bps=false);
//! Copy constructor
/*!
* \param cst The cst_sct3 which should be copied.
* \par Time complexity
* \f$ \Order{n} \f$, where \f$n=\f$cst_sct3.size()
*/
cst_sct3(const cst_sct3& cst): csa(m_csa),lcp(m_lcp),bp(m_bp),
bp_support(m_bp_support),
first_child_bv(m_first_child),
first_child_rank(m_first_child_rank) {
copy(cst);
}
/* @} */
//! Number of leaves of the suffix tree.
/*! Required for the Container Concept of the STL.
* \sa max_size, empty
*/
size_type size()const {
return m_bp.size()>>1;
}
//! Returns the largest size that cst_sct3 can ever have.
/*! Required for the Container Concept of the STL.
* \sa size
*/
static size_type max_size() {
return t_csa::max_size();
}
//! Returns if the data strucutre is empty.
/*! Required for the Container Concept of the STL.A
* \sa size
*/
bool empty()const {
return m_csa.empty();
}
//! Swap method for cst_sct3
/*! The swap method can be defined in terms of assignment.
This requires three assignments, each of which, for a container type, is linear
in the container's size. In a sense, then, a.swap(b) is redundant.
This implementation guaranties a run-time complexity that is constant rather than linear.
\param cst cst_sct3 to swap.
Required for the Assignable Conecpt of the STL.
*/
void swap(cst_sct3& cst) {
if (this != &cst) {
m_csa.swap(cst.m_csa);
m_bp.swap(cst.m_bp);
util::swap_support(m_bp_support, cst.m_bp_support, &m_bp, &(cst.m_bp));
m_first_child.swap(cst.m_first_child);
util::swap_support(m_first_child_rank, cst.m_first_child_rank, &m_first_child, &(cst.m_first_child));
std::swap(m_sigma, cst.m_sigma);
std::swap(m_nodes, cst.m_nodes);
// anything else has to be swapped before swapping lcp
swap_lcp(m_lcp, cst.m_lcp, *this, cst);
}
}
//! Returns a const_iterator to the first element of a depth first traversal of the tree.
/*! Required for the STL Container Concept.
* \sa end
*/
const_iterator begin()const {
if (0 == m_bp.size()) // special case: tree is uninitialized
return end();
// else if (2 == m_bp.size()) { // special case: the root is a leaf
// return const_iterator(this, root(), true, true);
// }
return const_iterator(this, root(), false, true);
};
//! Returns a const_iterator to the element after the last element of a depth first traversal of the tree.
/*! Required for the STL Container Concept.
* \sa begin.
*/
const_iterator end()const {
return const_iterator(this, root(), true, false);
}
//! Returns an iterator to the first element of a bottom-up traversal of the tree.
const_bottom_up_iterator begin_bottom_up()const {
if (0 == m_bp.size()) // special case: tree is uninitialized
return end_bottom_up();
return const_bottom_up_iterator(this, leftmost_leaf(root()));
}
//! Returns an iterator to the element after the last element of a bottom-up traversal of the tree.
const_bottom_up_iterator end_bottom_up()const {
return const_bottom_up_iterator(this, root(), false);
}
//! Assignment Operator.
/*!
* Required for the Assignable Concept of the STL.
*/
cst_sct3& operator=(const cst_sct3& cst);
//! Serialize to a stream.
/*! \param out Outstream to write the data structure.
* \return The number of written bytes.
*/
size_type serialize(std::ostream& out, structure_tree_node* v=NULL, std::string name="")const;
//! Load from a stream.
/*! \param in Inputstream to load the data structure from.
*/
void load(std::istream& in);
/*! \defgroup cst_sct3_tree_methods Tree methods of cst_sct3 */
/* @{ */
//! Return the root of the suffix tree.
/*!
* \par Time complexity O(1)
* \f$ \Order{1} \f$
*/
node_type root() const {
return node_type(0, size()-1, 0, m_bp.size()-1, m_bp.size());
}
//! Decide if a node is a leaf.
/*!
* \param v A valid node.
* \returns A boolean value indicating if v is a leaf.
* \par Time complexity
* \f$ \Order{1} \f$
*/
bool is_leaf(const node_type& v)const {
return v.i==v.j;
}
//! Return the i-th leaf (1-based from left to right).
/*!
* \param i 1-based position of the leaf.
* \return The i-th leave.
* \par Time complexity
* \f$ \Order{1} \f$
* \pre \f$ 1 \leq i \leq size() \f$
*/
node_type select_leaf(size_type i)const {
assert(i > 0 and i <= size());
size_type ipos = m_bp_support.select(i);
size_type jp1pos;
if (i == size())
jp1pos = m_bp.size();
else if (m_bp[ipos+1])
jp1pos = ipos+1;
else
jp1pos = m_bp_support.select(i+1);
return node_type(i-1, i-1, ipos, m_bp_support.find_close(ipos), jp1pos);
}
//! Calculate the number of leaves in the subtree rooted at node v.
/*! \param v A valid node of the suffix tree.
* \return The number of leaves in the subtree rooted at node v.
* \par Time complexity
* \f$ \Order{1} \f$
*/
size_type size(const node_type& v)const {
return v.j-v.i+1;
}
//! Calculates the leftmost leaf in the subtree rooted at node v.
/*! \param v A valid node of the suffix tree.
* \return The leftmost leaf in the subtree rooted at node v.
* \par Time complexity
* \f$ \Order{1} \f$
*/
node_type leftmost_leaf(const node_type& v)const {
return select_leaf(v.i+1);
}
//! Calculates the rightmost leaf in the subtree rooted at node v.
/*! \param v A valid node of the suffix tree.
* \return The rightmost leaf in the subtree rooted at node v.
* \par Time complexity
* \f$ \Order{1} \f$
*/
node_type rightmost_leaf(const node_type& v)const {
return select_leaf(v.j+1);
}
//! Calculates the index of the leftmost leaf in the corresponding suffix array.
/*! \param v A valid node of the suffix tree.
* \return The index of the leftmost leaf in the corresponding suffix array.
* \par Time complexity
* \f$ \Order{1} \f$
* \par Note
* lb is an abbreviation for ,,left bound''
*/
size_type lb(const node_type& v)const {
return v.i;
}
//! Calculates the index of the rightmost leaf in the corresponding suffix array.
/*! \param v A valid node of the suffix tree.
* \return The index of the rightmost leaf in the corresponding suffix array.
* \par Time complexity
* \f$ \Order{1} \f$
* \par Note
* rb is an abbreviation for ,,right bound''
*/
size_type rb(const node_type& v)const {
return v.j;
}
//! Calculate the parent node of a node v.
/*! \param v A valid node of the suffix tree.
* \return The parent node of v or the root if v==root().
* \par Time complexity
* \f$ \Order{1}\f$
*/
node_type parent(const node_type& v) const {
if (v.cipos > v.jp1pos) { // LCP[i] <= LCP[j+1]
size_type psv_pos, psv_cpos, psv_v, nsv_v, nsv_p1pos;
psv_v = psv(v.j+1, v.jp1pos, m_bp_support.find_close(v.jp1pos), psv_pos, psv_cpos);
nsv_v = nsv(v.j+1, v.jp1pos)-1;
if (nsv_v == size()-1) {
nsv_p1pos = m_bp.size();
} else { // nsv_v < size()-1
nsv_p1pos = m_bp_support.select(nsv_v+2);
}
return node_type(psv_v, nsv_v, psv_pos, psv_cpos, nsv_p1pos);
} else { // LCP[i] > LCP[j+1]
size_type psv_pos, psv_cpos, psv_v;
psv_v = psv(v.i, v.ipos, v.cipos, psv_pos, psv_cpos);
return node_type(psv_v, v.j, psv_pos, psv_cpos, v.jp1pos);
}
}
//! Returns the next sibling of node v.
/*!
* \param v A valid node v of the suffix tree.
* \return The next (right) sibling of node v or root() if v has no next (right) sibling.
* \par Time complexity
* \f$ \Order{1} \f$
*/
node_type sibling(const node_type& v)const {
//Procedure:(1) Determine, if v has a right sibling.
if (v.cipos < v.jp1pos) { // LCP[i] > LCP[j+1] => v has the same right border as parent(v) => no right sibling
return root();
}
// (2) There exists a right sibling, LCP[j+1] >= LCP[i] and j>i
// Now it holds: v.cipos > v.jp1pos
size_type cjp1posm1 = m_bp_support.find_close(v.jp1pos)-1; // v.cipos-2 ???
// m_bp[cjp1posm1] equals 1 => v is the last child
bool last_child = m_bp[cjp1posm1];
// otherwise if m_bp[cjp1posm1] equals 0 => we don't know if it is the last child
if (!last_child) {
size_type first_child_idx = cjp1posm1 - m_bp_support.rank(cjp1posm1);
last_child = m_first_child[first_child_idx]; // if first_child indicator is true => the new sibling is the rightmost sibling
}
if (last_child) {
size_type nsv_v = nsv(v.j+1, v.jp1pos)-1, nsv_p1pos;
if (nsv_v == size()-1) {
nsv_p1pos = m_bp.size();
} else {
nsv_p1pos = m_bp_support.select(nsv_v+2);
}
return node_type(v.j+1, nsv_v, v.jp1pos, m_bp_support.find_close(v.jp1pos), nsv_p1pos);
} else {
size_type new_j = m_bp_support.rank(m_bp_support.find_open(cjp1posm1))-2;
return node_type(v.j+1, new_j, v.jp1pos, m_bp_support.find_close(v.jp1pos), m_bp_support.select(new_j+2));
}
}
//! Get the i-th child of a node v.
/*!
* \param v A valid tree node of the cst.
* \param i 1-based index of the child which should be returned.
* \return The i-th child node of v or root() if v has no i-th child.
* \par Time complexity
* \f$ \Order{\frac{\sigma}{w}} \f$, where w=64 is the word size,
* can be implemented in \f$\Order{1}\f$ with rank and select.
* \pre \f$ 1 \leq i \leq degree(v) \f$
*/
node_type select_child(const node_type& v, size_type i)const {
assert(i > 0);
if (is_leaf(v)) // if v is a leave, v has no child
return root();
if (1 == i) {
size_type k = 0, kpos = 0, k_find_close = 0;
// v is not a leave: v has at least two children
k = get_first_l_index(v, kpos, k_find_close);// get first l-index k and the position of k
return node_type(v.i, k-1, v.ipos, v.cipos, kpos);
} else { // i > 1
size_type k1, kpos1, k_find_close1;
k1 = get_ith_l_index(v, i-1, kpos1, k_find_close1);
if (k1 == v.j+1)
return root();
size_type k2, kpos2, k_find_close2;
k2 = get_ith_l_index(v, i, kpos2, k_find_close2);
return node_type(k1, k2-1, kpos1, k_find_close1, kpos2);
}
}
//! Get the number of children of a node v.
/*!
* \param v A valid node v.
* \returns The number of children of node v.
* \par Time complexity
* \f$ \Order{\frac{\sigma}{w}} \f$, where w=64 is the word size,
* can be implemented in \f$\Order{1}\f$ with rank and select.
*/
size_type degree(const node_type& v)const {
if (is_leaf(v)) // if v is a leave, v has no child
return 0;
// v is not a leave: v has at least two children
size_type r = closing_pos_of_first_l_index(v);
/* if( m_bp[r-1] ){// if there exists no next l-index
return 2;
}
*/
size_type r0 = r - m_bp_support.rank(r);
const uint64_t* p = m_first_child.data() + (r0>>6);
uint8_t offset = r0&0x3F;
uint64_t w = (*p) & bits::lo_set[offset];
if (w) {
return offset-bits::hi(w)+1;
} else if (m_first_child.data() == p) { // w==0 and we are in the first word
return offset+2; // da bits::hi(w)=-1 sein muesste
} else {
size_type res = offset+2;
while (p > m_first_child.data()) {
w = *(--p);
if (0==w)
res += 64;
else {
return res + (63-bits::hi(w));
}
}
return res + (w==0);
}
}
// Returns the next sibling of node v.
// Only for tests.
node_type sibling_naive(const node_type& v)const {
if (v==root())
return root();
node_type parent = this->parent(v);
assert(parent != v);
size_type nr = degree(parent);
for (size_type i=1; i <= nr; ++i)
if (select_child(parent, i) == v and i!=nr)
return select_child(parent, i+1);
return root();
}
//! Get the child w of node v which edge label (v,w) starts with character c.
/*!
* \param v A valid tree node of the cst.
* \param c First character on the edge label.
* \param char_pos Reference which will hold the position (0-based) of
* the matching char c in the sorted text/suffix array.
* \return The child node w which edge label (v,w) starts with c or
* root() if it does not exist.
* \par Time complexity
* \f$ \Order{(\saaccess+\isaaccess) \cdot \log\sigma + \lcpaccess} \f$
*/
node_type child(const node_type& v, const char_type c, size_type& char_pos)const {
if (is_leaf(v)) // if v is a leaf = (), v has no child
return root();
// else v = ( ( ))
comp_char_type cc = m_csa.char2comp[c];
if (cc==0 and c!=0) // TODO: change char2comp so that we don't need this special case
return root();
size_type char_ex_max_pos = m_csa.C[((size_type)1)+cc], char_inc_min_pos = m_csa.C[cc];
size_type d = depth(v);
// (1) check the first child
char_pos = get_char_pos(v.i, d, m_csa);
if (char_pos >= char_ex_max_pos) {// the first character of the first child interval is lex. greater than c
// => all other first characters of the child intervals are also greater than c => no solution
return root();
} else if (char_pos >= char_inc_min_pos) { // i.e. char_pos < char_ex_max_pos and char_pos >= char_inc_min_pos
return select_child(v, 1);
}
size_type child_cnt = degree(v);
// (2) check the last child
char_pos = get_char_pos(v.j, d, m_csa);
if (char_pos < char_inc_min_pos) {// the first character of the last child interval is lex. smaller than c
// => all other first characters of the child intervals are also smaller than c => no solution
return root();
} else if (char_pos < char_ex_max_pos) { // i.e. char_pos < char_ex_max_pos and char_pos >= char_inc_min_pos
return select_child(v, child_cnt);
}
// (3) binary search for c in the children [2..children)
size_type l_bound = 2, r_bound = child_cnt, mid, kpos, ckpos, l_index;
while (l_bound < r_bound) {
mid = (l_bound + r_bound) >> 1;
l_index = get_ith_l_index(v, mid-1, kpos, ckpos);
char_pos = get_char_pos(l_index, d, m_csa);
if (char_inc_min_pos > char_pos) {
l_bound = mid+1;
} else if (char_ex_max_pos <= char_pos) {
r_bound = mid;
} else { // char_inc_min_pos <= char_pos < char_ex_max_pos => found child
// we know that the child is not the last child, see (2)
// find next l_index: we know that a new l_index exists: i.e. assert( 0 == m_bp[ckpos-1]);
size_type lp1_index = m_bp_support.rank(m_bp_support.find_open(ckpos-1))-1;
size_type jp1pos = m_bp.size();
if (lp1_index-1 < size()-1) {
jp1pos = m_bp_support.select(lp1_index+1);
}
return node_type(l_index, lp1_index-1, kpos, ckpos, jp1pos);
}
}
return root();
}
//! Get the child w of node v which edge label (v,w) starts with character c.
// \sa child(node_type v, const char_type c, size_type &char_pos)
node_type child(const node_type& v, const char_type c) {
size_type char_pos;
return child(v, c, char_pos);
}
//! Returns the d-th character (1-based indexing) of the edge-label pointing to v.
/*!\param v The node at which the edge path ends.
* \param d The position (1-based indexing) on the edge path from the
* root to v. \f$ d > 0 \wedge d < depth(v) \f$
* \return The character at position d on the edge path from the root to v.
* \par Time complexity
* \f$ \Order{ \log\sigma + (\saaccess+\isaaccess) } \f$
* \pre \f$ 1 \leq d \leq depth(v) \f$
*/
char_type edge(const node_type& v, size_type d)const {
assert(1 <= d);
assert(d <= depth(v));
size_type order = get_char_pos(v.i, d-1, m_csa);
size_type c_begin = 1, c_end = ((size_type)m_sigma)+1, mid;
while (c_begin < c_end) {
mid = (c_begin+c_end)>>1;
if (m_csa.C[mid] <= order) {
c_begin = mid+1;
} else {
c_end = mid;
}
}
return m_csa.comp2char[c_begin-1];
}
//! Calculate the LCA of two nodes `v` and `w`
/*!
* \param v The first node.
* \param w The second node.
* \return The lowest common ancestor of v and w.
* \par Time complexity
* \f$ \Order{\rrenclose}\ \f$
*/
node_type lca(node_type v, node_type w)const {
if (v.i > w.i or (v.i == w.i and v.j < w.j)) {
std::swap(v, w);
}
if (v.j >= w.j) { // v encloses w or v==w
return v;
} else { // v.i < v.j < w.i < w.j
size_type min_index = rmq(v.i+1, w.j);
size_type min_index_pos = m_bp_support.select(min_index+1);
size_type min_index_cpos = m_bp_support.find_close(min_index_pos);
if (min_index_cpos >= (m_bp.size() - m_sigma)) { // if lcp[min_index]==0 => return root
return root();
}
size_type new_j = nsv(min_index, min_index_pos)-1;
size_type new_ipos, new_icpos;
size_type new_i = psv(min_index, min_index_pos, min_index_cpos, new_ipos, new_icpos);
size_type jp1pos = m_bp.size();
if (new_j < size()-1) {
jp1pos = m_bp_support.select(new_j+2);
}
return node_type(new_i, new_j, new_ipos, new_icpos, jp1pos);
}
}
//! Returns the string depth of node v.
/*!
* \param v A valid node of a cst_sct3.
* \return The string depth of node v.
* \par Time complexity
* \f$ \Order{1} \f$ for non-leaves and \f$\Order{t_{SA}}\f$ for leaves
*/
size_type depth(const node_type& v)const {
if (v.i == v.j) {
return size()-m_csa[v.i];
} else if (v == root()) {
return 0;
} else {
size_type kpos, ckpos;
size_type l = get_first_l_index(v, kpos, ckpos);
return m_lcp[l];
}
}
//! Returns the node depth of node v
/*!
* \param v A valid node of a cst_sct3.
* \return The node depth of node v.
* \par Time complexity
* \f$ \Order{z} \f$, where \f$z\f$ is the resulting node depth.
* \par Note
* Can be implemented in O(1) with o(n) space. See
* Jansson, Sadakane, Sung:
* Ultra-succinct Representation of Ordered Trees
* SODA 2007
*/
size_type node_depth(node_type v)const {
size_type d = 0;
while (v != root()) {
++d;
v = parent(v);
}
return d;
}
//! Compute the suffix link of node v.
/*!
* \param v A valid node of a cst_sct3.
* \return The suffix link of node v.
* \par Time complexity
* \f$ \Order{ \rrenclose } \f$
*/
node_type sl(const node_type& v)const {
if (v == root())
return root();
// get interval with first char deleted
size_type i = m_csa.psi[v.i];
if (is_leaf(v)) {
if (v.i==0 and v.j==0) // if( v.l==1 )
return root();
else
return select_leaf(i+1);
}
size_type j = m_csa.psi[v.j];
assert(i < j);
size_type min_index = rmq(i+1, j); // rmq
size_type min_index_pos = m_bp_support.select(min_index+1);
size_type min_index_cpos = m_bp_support.find_close(min_index_pos);
if (min_index_cpos >= (m_bp.size() - m_sigma)) { // if lcp[min_index]==0 => return root
return root();
}
size_type new_j = nsv(min_index, min_index_pos)-1;
size_type new_ipos, new_icpos;
size_type new_i = psv(min_index, min_index_pos, min_index_cpos, new_ipos, new_icpos);
size_type jp1pos = m_bp.size();
if (new_j < size()-1) {
jp1pos = m_bp_support.select(new_j+2);
}
return node_type(new_i, new_j, new_ipos, new_icpos, jp1pos);
}
//! Compute the Weiner link of node v and character c.
/*!
* \param v A valid not of a cst_sct3.
* \param c The character which should be prepended to the string of the current node.
* \return root() if the Weiner link of (v, c) does not exist,
* otherwise the Weiner link is returned.
* \par Time complexity
* \f$ \Order{ t_{rank\_bwt} } \f$
*/
node_type wl(const node_type& v, const char_type c) const {
size_type c_left = m_csa.rank_bwt(v.i, c);
size_type c_right = m_csa.rank_bwt(v.j+1, c);
if (c_left == c_right) // there exists no Weiner link
return root();
if (c_left+1 == c_right)
return select_leaf(m_csa.C[m_csa.char2comp[c]] + c_left + 1);
else {
size_type left = m_csa.C[m_csa.char2comp[c]] + c_left;
size_type right = m_csa.C[m_csa.char2comp[c]] + c_right - 1;
assert(left < right);
size_type ipos = m_bp_support.select(left+1);
size_type jp1pos = m_bp.size();
if (right < size()-1) {
jp1pos = m_bp_support.select(right+2);
}
return node_type(left, right, ipos,
m_bp_support.find_close(ipos), jp1pos);
}
}
//! Computes the suffix number of a leaf node v.
/*!\param v A valid leaf node of a cst_sct3.
* \return The suffix array value corresponding to the leaf node v.
* \par Time complexity
* \f$ \Order{ \saaccess } \f$
*/
size_type sn(const node_type& v)const {
assert(is_leaf(v));
return m_csa[v.i];
}
//! Computes a unique identification number for a node of the suffx tree in the range [0..nodes()-1]
/*!
* \param v A valid node of a cst_sct3.
* \return A unique identification number for the node v in the range [0..nodes()-1]
* \par Time complexity
* \f$ \Order{1} \f$
*/
size_type id(const node_type& v)const {
if (is_leaf(v)) { // return id in the range from 0..csa.size()-1
return v.i;
}
size_type ckpos; // closing parentheses of the l-index
if (v.cipos > v.jp1pos) { // corresponds to m_lcp[i] <= m_lcp[j+1]
ckpos = v.jp1pos-1;
} else { // corresponds to m_lcp[i] > m_lcp[j+1]
ckpos = v.cipos-1;
}
assert(m_bp[ckpos]==0);
size_type r0ckpos = ckpos-m_bp_support.rank(ckpos); // determine the rank of the closing parenthesis
return size()+m_first_child_rank(r0ckpos);
}
//! Computes the node for such that id(v)=id.
/*!
* \param id An id in the range [0..nodes()-1].
* \return A node v of the CST such that id(v)=id.
* \par Time complexity
* \f$ \Order{1} \f$ for leaves and \f$ \Order{\log size()} \f$ for inner nodes
* \sa id(node_type v)
*/
node_type inv_id(size_type id) {
if (id < size()) { // the corresponding node is a leaf
return select_leaf(id+1);
} else { // the corresponding node is a inner node
// (1) get index of the closing parenthesis in m_first_child
size_type r0ckpos = 0;
{
//binary search for the position of the (id-size()+1)-th set bit in
id = id-size()+1;
size_type lb = 0, rb = m_bp.size(); // lb inclusive, rb exclusive
// invariant: arg(lb) < id, arg(rb) >= id
while (rb-lb > 1) {
size_type mid = lb + (rb-lb)/2;
size_type arg = m_first_child_rank(mid); // ones in the prefix [0..mid-1]
if (arg < id) {
lb = mid;
} else { // arg >= id
rb = mid;
}
}
r0ckpos = lb;
}
// (2) determine position clpos of the r0clpos-th closing parentheses in the parentheses sequence
size_type ckpos = 0;
{
// binary search for the position of the (r0ckpos+1)-th closing parenthesis
size_type lb = 0, rb = m_bp.size(); // lb inclusive, rb exclusive
// invariant: arg(lb) < r0ckpos+1, arg(rb) >= r0ckpos+1
while (rb-lb > 1) {
size_type mid = lb + (rb-lb)/2;
size_type arg = mid - m_bp_support.rank(mid-1); // zeros in the prefix [0..mid-1]
if (arg < r0ckpos+1) {
lb = mid;