[ create a new paste ] login | about

Link: http://codepad.org/o0nuIYCa    [ raw code | fork ]

C++, pasted on Mar 25:
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
#if !defined (TRIE_H)
#define TRIE_H

#include <string>
#include <limits>
#include <queue>
#include <set>

#if !defined (NO_BOOST)
    #include <boost/lexical_cast.hpp>
#else
    #include <sstream>
#endif

namespace lexical
{

//////////////////////////////////////////////////
// Debugging instrumentation
//////////////////////////////////////////////////

#if !defined (NDEBUG)
    namespace detail
    {
        //! Provides a way to precompile out debug code.
        #define TRIE_DEBUG(op) op
    
        static std::size_t       throw_count;           //!< Countdown to throwing an exception.
        static bool              debug_enabled = false; //!< Only throw if this is true.
        static const std::size_t tests_count = 11;      //!< Number of can_throw()ing functions.
        static std::size_t       tested_count = 0;      //!< How many can_throw()ers tested.
        
        //! Marks places where the following code can throw an exception.
        void
        can_throw()
        {
            if (debug_enabled && throw_count-- <= 0)
            {
                throw 0;
            }
        }
            
        //! Tests the strong exception safety guarantee.
        template<typename T, class Operation> 
        void strong_check(const T &v, const Operation &op) 
        { 
            bool succeeded = false; 
            for (std::size_t count = 0; !succeeded; ++count) 
            {
                T duplicate = v;
    
                debug_enabled = true;
                try
                {
                    throw_count = count; 
                    op (duplicate);
                    succeeded = true;
                    ++tested_count;
                    debug_enabled = false;
                } 
                catch (...)
                { 
                    debug_enabled = false;
                    bool unchanged = duplicate == v;
                    assert (unchanged); 
                }
            }
        }
    }
#else
    #define TRIE_DEBUG(op) ((void*)0)
#endif

//////////////////////////////////////////////////
// trie policies
//////////////////////////////////////////////////

    //! Namespace for implementation details that should not be known to users of trie.
    namespace detail
    {
        //! \brief Length policy for determining the lexicographical length of a given value.
        template<typename T, typename SizeT>
        struct length
        : public std::unary_function<const T&, SizeT>
        {
            //! Default implementation uses boost::lexical_cast or stringstreams 
            //! to determine length of the value once it has been cast to a std::string.
            //! \return The size (length) of given value.
            SizeT
            operator()(const T &value) const
            {
                TRIE_DEBUG (detail::can_throw ());
                #if !defined (NO_BOOST)
                    return boost::lexical_cast<std::string> (value).length ();
                #else
                    std::ostringstream oss;
                    oss << value;
                    return oss.str ().length ();
                #endif
            }
        };

        //! \brief Policy for determining a lexicographical prefix for a given value.
        template<typename T, typename SizeT>
        struct prefix
        : public std::binary_function<const T&, SizeT, T>
        {
            //! Default implementation uses boost::lexical_cast or stringstreams to cast 
            //! the value as a std::string and then return an un-cast substring of that string.
            //! \return The prefix value of given length.
            T
            operator()(const T &value, SizeT length) const
            {
                TRIE_DEBUG (detail::can_throw ());
                bool neg = value < 0;
                #if !defined (NO_BOOST)
                    using boost::lexical_cast;
                    T r = lexical_cast<T> (
                        lexical_cast<std::string> (neg ? -value : value).substr (0, length));
                #else
                    std::stringstream ss;
                    ss << (neg ? -value : value);
                    std::string prefix = ss.str ().substr (0, length);
                    ss.str (std::string ());
                    ss << prefix;
                    T r;
                    ss >> r;
                #endif
                return (neg ? -r : r);
            }
        };
    }

//////////////////////////////////////////////////
// trie definition
//////////////////////////////////////////////////

    //! \brief A generic trie class that supports any type so long as it is provided
    //! appropriate Compare, Length, and Prefix policies.
    //!
    //! \li T is the data type that the trie will store.
    //! \li Compare is the policy by which values in the trie will be compared to one another.
    //! \li Length is the policy that determines lexicographical lengths of the values.
    //! \li Prefix is the policy that determines lexicographical prefixes of values.
    //!
    //! The defaults for Compare, Length, and Prefix are setup to accommodate any type that
    //! can be cast to a std::string via boost::lexical_cast.  Specific std::string
    //! defaults are also provided to optimize string tries.
    template<typename T, class Compare = std::less<T>, 
        class Length = detail::length<T, std::size_t>,
        class Prefix = detail::prefix<T, std::size_t> >
    class trie
    {
        private:
            typedef trie*           trie_node;              //!< Child node type.
            typedef const trie*     const_trie_node;        //!< Const child node type.

        public:
            class const_iterator;
            
            //! It makes sense for trie::const_iterator to be the same as a trie::iterator only
            //! because it doesn't make sense to offer a non-const iterator for a trie container.
            //! Arbitrarily changing node values could mess up a trie's internal ordering causing
            //! undefined behavior. Thus all trie iterators are const iterators.
            typedef const_iterator iterator;
    
            // Value types
            typedef T                                           value_type;
            typedef std::size_t                                 size_type;
            typedef value_type&                                 reference;
            typedef const value_type&                           const_reference;
            typedef value_type*                                 pointer;
            typedef const value_type*                           const_pointer;
            typedef Compare                                     value_compare;
            typedef value_compare                               key_compare;
            
            trie();
            trie(const trie &copy);
            trie& operator=(const trie &copy);
            ~trie() throw();

            template<class InputIterator>
            trie(const InputIterator &first, const InputIterator &last);
                        
            size_type                   size() const;
            size_type                   max_size() const;
            std::pair<iterator, bool>   insert(const_reference word);
            iterator                    insert(const iterator &i, const_reference word);
            const_iterator              begin() const;
            const_iterator              end() const;
            iterator                    begin();
            iterator                    end();
            bool                        has(const_reference word) const;
            bool                        has_prefix(const_reference word) const;
            void                        swap(trie &from) throw();
            void                        clear() throw ();
            bool                        empty() const;
            iterator                    erase(const iterator &i);
            iterator                    erase(const iterator &begin, const iterator &end);
            void                        remove(const_reference word);
            void                        remove_prefix(const_reference word);
            iterator                    find(const_reference word) const;
            iterator                    find_prefix(const_reference word) const;
            trie                        clone(const iterator &i) const;
            void                        insert(const iterator &begin, const iterator &end);
            value_compare               value_comp() const;
            key_compare                 key_comp() const;

            template<class InputIterator>
            void                        insert(const InputIterator &first,
                                            const InputIterator &last);

            //! \brief Provides an iterator over trie values.
            class const_iterator 
            : public std::iterator<std::forward_iterator_tag, value_type> 
            {
                public:
                    explicit const_iterator(trie_node copy);
                    
                    const_iterator&     operator=(const const_iterator &rhs);
                    bool                operator==(const const_iterator &rhs) const;
                    bool                operator!=(const const_iterator &rhs) const;
                    const_iterator&     operator++();
                    const_iterator      operator++(int);
                    const_reference     operator*() const;
                    const_pointer       operator->() const;
                    
                    // Observers
                    bool                word() const;
                    bool                leaf() const;
    
                private:
                    friend class trie;

                    const_trie_node             node;       //!< Current trie node pointer.
                    std::deque<const_trie_node> parents;    //!< Parents of current node.
            };

        private:
            typedef Prefix value_prefix;                    //!< Prefix policy for values.
            typedef Length value_length;                    //!< Length policy for values.
            
            //! \brief Provides implementation for comparison of trie nodes.
            struct trie_node_less
            : public std::binary_function<trie_node, trie_node, bool>
            {
                bool operator()(trie_node lhs, trie_node rhs) const;
            };
            typedef std::set<trie_node, trie_node_less> subtrie;    //<! Children subtrie type.
            
            trie(const_reference value, size_type depth);

            iterator    insert_helper(value_type word, size_type depth,
                            size_type &children_added = 0);
            void        reduce_childs(trie_node node) throw();
            bool        root() const;
            bool        leaf() const;
            iterator    find_helper(const_reference word, size_type depth, bool whole) const;
            
            //! \brief Provides implementation for safely deleting a given trie node.
            struct trie_node_delete
            {
                void operator()(trie_node const &node) throw();
            };

            subtrie                 children;       //!< Subtrie of children of this node.
            size_type               childs;         //!< Number of all children below this node.
            trie_node               parent;         //!< Parent node of this node.
            value_type              value;          //!< Value of this node.
            size_type               depth;          //!< Depth from root of this node.
            bool                    endpoint;       //!< True if node is a word endpoint.

        public:
            // More value types
            typedef typename iterator::difference_type      difference_type;
    };
    
    template<typename T, class Compare, class Length, class Prefix>
    bool operator==(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs);

    template<typename T, class Compare, class Length, class Prefix>
    bool operator!=(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs);

    template<typename T, class Compare, class Length, class Prefix>
    bool operator<(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs);

    template<typename T, class Compare, class Length, class Prefix>
    bool operator>(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs);

    template<typename T, class Compare, class Length, class Prefix>
    bool operator<=(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs);

    template<typename T, class Compare, class Length, class Prefix>
    bool operator>=(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs);    
    
//////////////////////////////////////////////////
// trie implementation
//////////////////////////////////////////////////

    //! Create default empty trie.
    template<typename T, class Compare, class Length, class Prefix>
    trie<T, Compare, Length, Prefix>::trie()
    : childs(0), parent(NULL), value(), depth(0), endpoint(false)
    {
        TRIE_DEBUG (detail::can_throw ());
    }
    
    //! Create trie as a copy of given trie.
    template<typename T, class Compare, class Length, class Prefix>
    trie<T, Compare, Length, Prefix>::trie(const trie &copy)
    : childs(0), parent(NULL), value(), depth(0), endpoint(false)
    {
        TRIE_DEBUG (detail::can_throw ());
        insert (copy.begin (), copy.end ());
    }

    //! Create trie from given range of values.
    template<typename T, class Compare, class Length, class Prefix>
    template<class InputIterator>
    trie<T, Compare, Length, Prefix>::trie(const InputIterator &first, const InputIterator &last)
    : childs(0), parent(NULL), value(), depth(0), endpoint(false)
    {
        insert (first, last);
    }

    //! Assign this trie to be a copy of given trie.    
    template<typename T, class Compare, class Length, class Prefix>
    trie<T, Compare, Length, Prefix>&
    trie<T, Compare, Length, Prefix>::operator=(const trie &copy)
    {
        if (this == &copy)
        {
            return *this;
        }

        TRIE_DEBUG (detail::can_throw ());
        trie temp = copy;
        swap (temp);

        // Note here that the direct children of temp will have parent pointers that point
        // to a temporary. We must re-parent these pointers now.
        for (typename subtrie::iterator i = this->children.begin ();
            i != this->children.end (); ++i)
        {
            (*i)->parent = this;
        }

        return *this;
    }

    //! Compares two given trie values according to policy.
    //! \return Boolean value of comparison.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool
    trie<T, Compare, Length, Prefix>::trie_node_less::operator()(trie_node lhs,
        trie_node rhs) const
    {
        return value_compare () (lhs->value, rhs->value);
    }

    //! Does not throw, safely deletes a given trie node.
    template<typename T, class Compare, class Length, class Prefix>
    inline void
    trie<T, Compare, Length, Prefix>::trie_node_delete::operator()(trie_node const &node) throw()
    {
        delete node;
    }
    
    //! Safely deletes this trie and it's children. Does not throw.
    template<typename T, class Compare, class Length, class Prefix>
    trie<T, Compare, Length, Prefix>::~trie() throw()
    {
        for_each (this->children.begin (), this->children.end (), trie_node_delete ());
        reduce_childs (this->parent);
    }
    
    //! Inserts given value into trie.
    //! \return iterator to node whose value equals given value.
    template<typename T, class Compare, class Length, class Prefix>
    inline std::pair<typename trie<T, Compare, Length, Prefix>::iterator, bool>
    trie<T, Compare, Length, Prefix>::insert(const_reference word)
    {
        TRIE_DEBUG (detail::can_throw ());
        size_type new_childs = 0;
        iterator r = insert_helper (word, 1, new_childs);
        return std::make_pair<iterator, bool> (r, new_childs);
    }

    //! Inserts given value into trie using given iterator as a suggestion. If the suggestion is 
    //! good this will be faster than insert(const_reference), however if it is not, then
    //! this method will be slightly slower. Returns iterator to last inserted node.
    //! \return iterator to node whose value equals given value.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::iterator
    trie<T, Compare, Length, Prefix>::insert(const iterator &i, const_reference word)
    {
        TRIE_DEBUG (detail::can_throw ());
        if (i.node != NULL && 
            i.node->value == value_prefix () (word, value_length () (i.node->value)))
        {
            size_type new_childs = 0;
            return const_cast<trie_node&> (i.node)->insert_helper (
                word, value_length () (i.node->value) + 1, new_childs);
        }
        return insert (word).first;
    }

    //! Helps insert() do it's job. Begins trying to insert given value at given depth.
    //! \post children_added equals number of children nodes added to trie.
    //! \return iterator to node whose value equals given value.
    template<typename T, class Compare, class Length, class Prefix>
    typename trie<T, Compare, Length, Prefix>::iterator
    trie<T, Compare, Length, Prefix>::insert_helper(value_type word, size_type depth, 
        size_type &children_added)
    {    
        if (depth - 1 == value_length () (word))
        {
            // word already in trie.
            children_added = 0;
            this->endpoint = true;
            return iterator (this);
        }

        trie target (word, depth);
        typename subtrie::iterator i = this->children.find (&target);
        // If next node in line to target does not exist,
        if (i == this->children.end ())
        {
            // then create it and continue insert on new node.
            iterator r (NULL);
            trie_node node = NULL;
            try
            {
                TRIE_DEBUG (detail::can_throw ());
                node = new trie (word, depth);

                size_type new_childs = 0;
                TRIE_DEBUG (detail::can_throw ());
                r = node->insert_helper (word, depth + 1, new_childs);

                this->children.insert (node);
                this->childs += ++new_childs ;
                node->parent = this;
                children_added += new_childs;
            }
            catch (...)
            {
                if (node)
                {
                    delete node;
                }
                throw;
            }
            return r;
        }
    
        // else continue insert on already existing node.
        size_type new_childs = 0;
        TRIE_DEBUG (detail::can_throw ());
        iterator r = (*i)->insert_helper (word, depth + 1, new_childs);
        this->childs += new_childs;
        children_added = new_childs;
        return r;
    }
    
    //! Reduces the children counter for parent nodes of the given node.
    //! For use when the given node is erased from the trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline void 
    trie<T, Compare, Length, Prefix>::reduce_childs(trie_node node) throw()
    {
        if (node == NULL)
        {
            return;
        }

        if (node->childs > 0)
        {
            --node->childs;
        }
        
        reduce_childs (node->parent);
    }

    //! Creates trie node with given value and given depth. 
    template<typename T, class Compare, class Length, class Prefix>
    inline trie<T, Compare, Length, Prefix>::trie(const_reference word, size_type depth)
    : childs(0), parent(NULL), depth(depth), endpoint(false)
    {
        this->value = value_prefix () (word, depth);
    }

    //! \return iterator to first node in this trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::const_iterator 
    trie<T, Compare, Length, Prefix>::begin() const
    {
        if (root ()) 
        {
            return ++iterator (const_cast<trie_node> (this));
        }
        return iterator (const_cast<trie_node> (this));
    }
    
    //! \return iterator to one past last node in this trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::const_iterator 
    trie<T, Compare, Length, Prefix>::end() const
    {
        return iterator (NULL);
    }

    //! \return const_iterator to first node in this trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::iterator 
    trie<T, Compare, Length, Prefix>::begin()
    {
        typedef const_iterator (trie::*const_begin)() const;
        return (this->*static_cast<const_begin> (&trie::begin)) ();
    }
    
    //! \return const_iterator to one past last node in this trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::iterator 
    trie<T, Compare, Length, Prefix>::end()
    {
        typedef const_iterator (trie::*const_end)() const;
        return (this->*static_cast<const_end> (&trie::end)) ();
    }
    
    //! Swap one trie with another.
    template<typename T, class Compare, class Length, class Prefix>
    void
    trie<T, Compare, Length, Prefix>::swap(trie &from) throw()
    {
        using std::swap;

        swap (from.children, this->children);
        swap (from.childs, this->childs);
        swap (from.value, this->value);
        swap (from.parent, this->parent);
        swap (from.endpoint, this->endpoint);
    }
    
    //! Compares one trie to another. Runs in O(N) time.
    //! \return True if and only if this trie is strictly less than given trie, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    operator<(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs)
    {
        return std::lexicographical_compare (lhs.begin (), lhs.end (), rhs.begin (), rhs.end ());
    }

    //! Compares one trie to another. Runs in O(N) time.
    //! \return True if and only if this trie is strictly greater than given trie, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    operator>(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs)
    {
        return std::lexicographical_compare (lhs.begin (), lhs.end (), rhs.begin (), rhs.end (),
            std::greater<typename trie<T, Compare, Length, Prefix>::value_type> ());
    }
    
    //! Compares one trie to another. Runs in O(N) time.
    //! \return True if and only if this trie exactly equal to given trie, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    operator==(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs)
    {
        return std::equal (lhs.begin (), lhs.end (), rhs.begin ());
    }

    //! Compares one trie to another. Runs in O(N) time.
    //! \return True if and only if this trie is not equal to given trie, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    operator!=(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs)
    {
        return !(lhs == rhs);
    }

    //! Compares one trie to another. Runs in O(N) time.
    //! \return True if and only if this trie less than or equal to given trie, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    operator<=(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs)
    {
        return std::lexicographical_compare (lhs.begin (), lhs.end (), rhs.begin (), rhs.end (),
            std::less_equal<typename trie<T, Compare, Length, Prefix>::value_type> ());
    }

    //! Compares one trie to another. Runs in O(N) time.
    //! \return True if and only if this trie greater than or equal to given trie, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    operator>=(const trie<T, Compare, Length, Prefix> &lhs,
        const trie<T, Compare, Length, Prefix> &rhs)
    {
        return std::lexicographical_compare (lhs.begin (), lhs.end (), rhs.begin (), rhs.end (),
            std::greater_equal<typename trie<T, Compare, Length, Prefix>::value_type> ());
    }

    //! \return True if this node is the root node of this trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool
    trie<T, Compare, Length, Prefix>::root() const
    {
        return this->depth == 0;
    }

    //! \return Size of trie in O(1) time.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::size_type 
    trie<T, Compare, Length, Prefix>::size() const
    {
        return this->childs + (root () ? 0 : 1);
    }

    //! \return Maximum size of a trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::size_type 
    trie<T, Compare, Length, Prefix>::max_size() const
    {
        return std::numeric_limits<size_type>::max ();
    }
    
    //! Clear this trie and its children.
    template<typename T, class Compare, class Length, class Prefix>
    void
    trie<T, Compare, Length, Prefix>::clear() throw ()
    {
        for_each (this->children.begin (), this->children.end (), trie_node_delete ());
        this->children.clear ();
        this->childs = 0;
    }

    //! \return True if and only if trie is empty, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    bool
    trie<T, Compare, Length, Prefix>::empty() const
    {
        return (root () && this->childs == 0);
    }

    //! Removes node of given value (and all its children) pointed to by given iterator from trie.
    //! \return The iterator to the node one past last deleted node.
    template<typename T, class Compare, class Length, class Prefix>
    typename trie<T, Compare, Length, Prefix>::iterator 
    trie<T, Compare, Length, Prefix>::erase(const iterator &i)
    {
        if (i == iterator (NULL))
        {
            return i;
        }

        // Find element after the last element erased
        trie_node target = const_cast<trie_node> (i.node);
        typename subtrie::iterator this_child = target->parent->children.find (target);
        iterator next = iterator (NULL);
        
        if (this_child == target->parent->children.end ())
        {
            clear ();
            return end ();
        }
        
        if (++this_child != target->parent->children.end ())
        {
            next = iterator (*this_child);
        }
        
        // Remove target from target's parent's children.
        target->parent->children.erase (target);
        delete target;
        
        return next;
    }
    
    //! Erase range of node from begin (inclusive) to end (exclusive).
    //! \return The iterator to the node one past last deleted node.
    template<typename T, class Compare, class Length, class Prefix>
    typename trie<T, Compare, Length, Prefix>::iterator
    trie<T, Compare, Length, Prefix>::erase(const iterator &begin, const iterator &end)
    {
        iterator i = begin;
        iterator ob = iterator (NULL);
        while (i != end && i != ob)
        {
            i = erase (i);
        }
        return i;
    }

    //! \return True if and only if given word value is in our trie, otherwise false.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    trie<T, Compare, Length, Prefix>::has(const_reference word) const
    {
        return (find_helper (word, 1, true) != end ());
    }

    //! \return True if and only if given value is in our trie, otherwise false.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool 
    trie<T, Compare, Length, Prefix>::has_prefix(const_reference word) const
    {
        return (find_helper (word, 1, false) != end ());
    }

    //! Insert a range of values into trie given iterators to those values.
    template<typename T, class Compare, class Length, class Prefix>
    template<class InputIterator>
    void
    trie<T, Compare, Length, Prefix>::insert(const InputIterator &first, const InputIterator &last)
    {
        TRIE_DEBUG (detail::can_throw ());
        using std::copy;
        copy (first, last, inserter (*this, this->begin ()));
    }

    //! Specialized range insert for copying from a trie iterator.
    template<typename T, class Compare, class Length, class Prefix>
    void
    trie<T, Compare, Length, Prefix>::insert(const iterator &begin, const iterator &end)
    {
        TRIE_DEBUG (detail::can_throw ());
        for (iterator i = begin; i != end; ++i)
        {
            // We only have to insert endpoint nodes since the rest are constructed automatically.
            if (i.node->endpoint)
            {
                insert (*i);
            }
        }
    }

    //! Creates a clone of this trie from the given iterator using only the value of the given
    //! iterator and its children as nodes.
    //! \return The cloned trie.
    template<typename T, class Compare, class Length, class Prefix>
    trie<T, Compare, Length, Prefix>
    trie<T, Compare, Length, Prefix>::clone(const iterator &i) const
    {
        TRIE_DEBUG (detail::can_throw ());
        trie r;
        r.insert (i, end ());
        return r;
    }

    //! Convenience method to easily remove a given word value from this trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline void
    trie<T, Compare, Length, Prefix>::remove(const_reference word)
    {
        erase (find_helper (word, 1, true));
    }

    //! Convenience method to easily remove a given value from this trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline void
    trie<T, Compare, Length, Prefix>::remove_prefix(const_reference word)
    {
        erase (find_helper (word, 1, false));
    }

    //! \return An iterator to a subtrie of this trie or end() if value is not found or not a word.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::iterator 
    trie<T, Compare, Length, Prefix>::find(const_reference word) const
    {
        return find_helper (word, 1, true);
    }

    //! \return An iterator to a subtrie of this trie or end() if value is not found.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::iterator 
    trie<T, Compare, Length, Prefix>::find_prefix(const_reference word) const
    {
        return find_helper (word, 1, false);
    }

    //! Looks for given value at given depth in this trie, if not found continues search and
    //! ultimately returns end() if value is not found.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::iterator 
    trie<T, Compare, Length, Prefix>::find_helper(const_reference word, size_type depth,
        bool whole) const
    {
        if ((whole && this->endpoint && this->value == word) ||
            (!whole && this->value == word))
        {
            return iterator (const_cast<trie_node> (this));
        }

        trie target (word, depth);
        typename subtrie::iterator i = this->children.find (&target);
        if (i != this->children.end ())
        {
            return (*i)->find_helper (word, depth + 1, whole);
        }
    
        return end ();
    }   

    //! \return True if and only if this trie node is a leaf node, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool
    trie<T, Compare, Length, Prefix>::leaf() const
    {
        return this->children.empty ();
    }

    //! \return The value_compare object used by the trie.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::value_compare
    trie<T, Compare, Length, Prefix>::value_comp() const
    {
        return value_compare ();
    }

    //! \return The key_compare object used by the trie (same as value_compare object).
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::key_compare
    trie<T, Compare, Length, Prefix>::key_comp() const
    {
        return key_compare ();
    }

//////////////////////////////////////////////////
// const_iterator implementation
//////////////////////////////////////////////////
    
    //! Create an iterator to a given node.
    template<typename T, class Compare, class Length, class Prefix>
    inline trie<T, Compare, Length, Prefix>::const_iterator::const_iterator(trie_node copy)
    : node(copy)
    {
        ;
    }

    //! \return This iterator which is a copy of the given iterator.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::const_iterator&
    trie<T, Compare, Length, Prefix>::const_iterator::operator=(const const_iterator &rhs)
    {
        if (this == &rhs)
        {
            return *this;
        }
        
        using std::swap;
        const_iterator temp = rhs;
        swap (this->node, temp.node);
        swap (this->parents, temp.parents);
        return *this;
    }
    
    //! \return True if and only if this iterator points to the same object as the given iterator,
    //! false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool
    trie<T, Compare, Length, Prefix>::const_iterator::operator==(const const_iterator &rhs) const
    {
        return this->node == rhs.node;
    }
    
    //! \return True if and only if this iterator points to a different object as the given
    //! iterator, false otherwise.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool
    trie<T, Compare, Length, Prefix>::const_iterator::operator!=(const const_iterator &rhs) const
    {
        return this->node != rhs.node;
    }

    //! Steps through trie in pre-order.
    //! \return An iterator to next object in trie.
    template<typename T, class Compare, class Length, class Prefix>
    typename trie<T, Compare, Length, Prefix>::const_iterator&
    trie<T, Compare, Length, Prefix>::const_iterator::operator++()
    {
        // After we've fallen off the trie, we're always at the end.
        if (this->node == NULL)
        {
            return *this;
        }
    
        // Step through children in pre-order.
        if (!this->node->leaf ())
        {
            this->parents.push_back (this->node);
            // Get pointer to child trie object.
            this->node = *this->node->children.begin ();
            return *this;
        }
    
        // Peel off parents and step through their children in pre-order.
        typedef typename subtrie::iterator child;
        while (!this->parents.empty ())
        {
            child end = this->parents.back ()->children.end ();
            // Recursively step through parents' children.
            child next = ++(this->parents.back ()->children.find (
                const_cast<trie_node> (this->node)));
            if (next != end)
            {
                this->node = *next;
                return *this;
            }
            else
            {
                this->node = &(*this->parents.back ());
                this->parents.pop_back ();
            }
        }
    
        // End of trie has been reached.
        this->node = NULL;
        return *this;
    }
    
    //! Steps through trie in pre-order.
    //! \return An iterator to the current object that this iterator points to.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::const_iterator 
    trie<T, Compare, Length, Prefix>::const_iterator::operator++(int)
    {
        const_iterator temp (*this);
        ++(*this);
        return temp;
    }

    //! \return The value of the trie object pointer to by this iterator.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::const_reference
    trie<T, Compare, Length, Prefix>::const_iterator::operator*() const
    {
        return this->node->value;
    }
    
    //! \return A pointer to the trie object pointer to by this iterator.
    template<typename T, class Compare, class Length, class Prefix>
    inline typename trie<T, Compare, Length, Prefix>::const_pointer
    trie<T, Compare, Length, Prefix>::const_iterator::operator->() const
    {
        return &this->node->value;
    }

    //! \return True if and only if this node is a word, as in not-a-prefix.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool
    trie<T, Compare, Length, Prefix>::const_iterator::word() const
    {
        return this->node->endpoint;
    }

    //! \return True if and only if this node is a leaf node.
    template<typename T, class Compare, class Length, class Prefix>
    inline bool
    trie<T, Compare, Length, Prefix>::const_iterator::leaf() const
    {
        return this->node->leaf ();
    }
    
//////////////////////////////////////////////////
// trie<std::string> optimization
//////////////////////////////////////////////////
    
    namespace detail
    {
        //! \brief std::string specialization for the length policy.
        template<>
        struct length<std::string, std::string::size_type>
        {
            //! Simply uses std::string::length() to determine length.
            std::string::size_type operator()(const std::string &value) const
            {
                TRIE_DEBUG (detail::can_throw ());
                return value.length ();
            }
        };

        //! \brief std::string specialization for the prefix policy.
        template<>
        struct prefix<std::string, std::string::size_type>
        {
            //! Simply uses std::string::substr() to make prefixes.
            std::string operator()(const std::string &value, std::string::size_type length) const
            {
                TRIE_DEBUG (detail::can_throw ());
                return value.substr (0, length);
            }
        };
    }
} // namespace lexical

#undef TRIE_DEBUG
#endif // #if !defined (TRIE_H)


Create a new paste based on this one


Comments: