1 | /* |
---|
2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
---|
3 | |
---|
4 | This file is part of SINA. |
---|
5 | SINA is free software: you can redistribute it and/or modify it under |
---|
6 | the terms of the GNU General Public License as published by the Free |
---|
7 | Software Foundation, either version 3 of the License, or (at your |
---|
8 | option) any later version. |
---|
9 | |
---|
10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
13 | for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | Additional permission under GNU GPL version 3 section 7 |
---|
19 | |
---|
20 | If you modify SINA, or any covered work, by linking or combining it |
---|
21 | with components of ARB (or a modified version of that software), |
---|
22 | containing parts covered by the terms of the |
---|
23 | ARB-public-library-license, the licensors of SINA grant you additional |
---|
24 | permission to convey the resulting work. Corresponding Source for a |
---|
25 | non-source form of such a combination shall include the source code |
---|
26 | for the parts of ARB used as well as that of the covered work. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _CSEQ_IMPL_H_ |
---|
30 | #define _CSEQ_IMPL_H_ |
---|
31 | |
---|
32 | #include "cseq.h" |
---|
33 | |
---|
34 | namespace sina { |
---|
35 | |
---|
36 | inline cseq_base& |
---|
37 | cseq_base::append(const std::string& str) { |
---|
38 | return append(str.c_str()); |
---|
39 | } |
---|
40 | |
---|
41 | template<typename T> |
---|
42 | class lexical_cast_visitor |
---|
43 | : public boost::static_visitor<T> { |
---|
44 | public: |
---|
45 | template<typename S> |
---|
46 | const T operator()(const S& s) const { |
---|
47 | try { |
---|
48 | return boost::lexical_cast<T>(s); |
---|
49 | } catch (...) { |
---|
50 | return T(); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | // no convert for identical type |
---|
55 | |
---|
56 | const T operator()(const T& t) const { |
---|
57 | return t; |
---|
58 | } |
---|
59 | |
---|
60 | // return default value for vector (no sense to convert) |
---|
61 | const T operator()(const std::vector<cseq_base>& /*v*/) const { |
---|
62 | return T(); |
---|
63 | } |
---|
64 | }; |
---|
65 | |
---|
66 | inline cseq_base::iterator |
---|
67 | prev_begin(const cseq_base& c, const cseq_base::iterator& it) { |
---|
68 | if (c.begin() != it) { |
---|
69 | return it-1; |
---|
70 | } |
---|
71 | return it; |
---|
72 | } |
---|
73 | inline cseq_base::iterator |
---|
74 | prev_end(const cseq_base& /*c*/, const cseq_base::iterator& it) { |
---|
75 | return it; |
---|
76 | } |
---|
77 | inline bool |
---|
78 | has_prev(const cseq_base& c, const cseq_base::iterator& it) { |
---|
79 | return c.begin() != it; |
---|
80 | } |
---|
81 | |
---|
82 | inline cseq_base::iterator |
---|
83 | next_begin(const cseq_base& /*c*/, const cseq_base::iterator& it) { |
---|
84 | return it+1; |
---|
85 | } |
---|
86 | inline cseq_base::iterator |
---|
87 | next_end(const cseq_base& c, const cseq_base::iterator& it) { |
---|
88 | if (it+1 == c.end()) { |
---|
89 | return it + 1; |
---|
90 | } |
---|
91 | return it + 2; |
---|
92 | } |
---|
93 | inline bool |
---|
94 | has_next(const cseq_base& c, const cseq_base::iterator& it) { |
---|
95 | return (it+1) != c.end(); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | inline cseq_base::idx_type |
---|
100 | get_node_id(cseq_base& c, const cseq_base::iterator& it){ |
---|
101 | return it - c.begin(); |
---|
102 | } |
---|
103 | |
---|
104 | inline cseq_base::iterator |
---|
105 | cseq_base::begin() { |
---|
106 | return {bases.begin()}; |
---|
107 | } |
---|
108 | |
---|
109 | inline cseq_base::const_iterator |
---|
110 | cseq_base::begin() const { |
---|
111 | return {bases.begin()}; |
---|
112 | } |
---|
113 | |
---|
114 | inline cseq_base::const_reverse_iterator |
---|
115 | cseq_base::rbegin() const { |
---|
116 | return {bases.rbegin()}; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | inline cseq_base::iterator |
---|
121 | cseq_base::end() { |
---|
122 | return {bases.end()}; |
---|
123 | } |
---|
124 | |
---|
125 | inline cseq_base::const_iterator |
---|
126 | cseq_base::end() const { |
---|
127 | return {bases.end()}; |
---|
128 | } |
---|
129 | inline cseq_base::const_reverse_iterator |
---|
130 | cseq_base::rend() const { |
---|
131 | return {bases.rend()}; |
---|
132 | } |
---|
133 | |
---|
134 | inline cseq_base::iterator |
---|
135 | cseq_base::getIterator(cseq_base::vidx_type i) { |
---|
136 | // this is weird. FIXME |
---|
137 | return {std::lower_bound(bases.begin(), |
---|
138 | bases.end(),aligned_base(i,'.'))}; |
---|
139 | } |
---|
140 | |
---|
141 | inline cseq_base::const_iterator |
---|
142 | cseq_base::getIterator(cseq_base::vidx_type i) const { |
---|
143 | return {lower_bound(bases.begin(), |
---|
144 | bases.end(),aligned_base(i,'.'))}; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | template<typename T> |
---|
149 | void |
---|
150 | for_each_prev(cseq_base::iterator cit, T t) { |
---|
151 | t(*(cit-1)); |
---|
152 | } |
---|
153 | |
---|
154 | } // namespace sina |
---|
155 | |
---|
156 | #endif // _CSEQ_IMPL_H_ |
---|
157 | |
---|
158 | /* |
---|
159 | Local Variables: |
---|
160 | mode:c++ |
---|
161 | c-file-style:"stroustrup" |
---|
162 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
---|
163 | indent-tabs-mode:nil |
---|
164 | fill-column:99 |
---|
165 | End: |
---|
166 | */ |
---|
167 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
---|