File: CharIndexedVector.hpp

package info (click to toggle)
libpwiz 3.0.18342-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,888 kB
  • sloc: cpp: 157,552; sh: 4,182; makefile: 317
file content (248 lines) | stat: -rw-r--r-- 6,479 bytes parent folder | download | duplicates (7)
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
//
// $Id$ 
//
//
// Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
//
// Copyright 2008 Spielberg Family Center for Applied Proteomics
//   Cedars Sinai Medical Center, Los Angeles, California  90048
// Copyright 2008 Vanderbilt University - Nashville, TN 37232
//
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
//


#ifndef _CHARINDEXEDVECTOR_HPP_
#define _CHARINDEXEDVECTOR_HPP_

#include "boost/array.hpp"

namespace pwiz {
namespace util {

/// an iterator for CharIndexedVector
template<class T>
class CharIndexedVectorIterator
{
    typedef boost::array<T, 129> type;
    typename type::iterator m_itr;

public:
    typedef typename type::value_type value_type;
    typedef typename type::iterator iterator;
    typedef typename type::iterator pointer;
    typedef typename type::const_iterator const_iterator;
    typedef typename type::difference_type difference_type;
    typedef typename type::reference reference;
    typedef typename type::const_reference const_reference;
    typedef typename type::size_type size_type;
    typedef std::random_access_iterator_tag iterator_category;

    CharIndexedVectorIterator(const iterator& itr) : m_itr(itr) {}

    reference operator*() const
    {
        return *m_itr;
    }

    bool operator!=(const CharIndexedVectorIterator& rhs) const
    {
        return m_itr != *(iterator*)&rhs;
    }

    difference_type operator-(const CharIndexedVectorIterator& rhs) const
    {
        return m_itr - rhs.m_itr;
    }

    CharIndexedVectorIterator& operator++()
    {	// preincrement
        ++m_itr;
        return (*this);
    }

    CharIndexedVectorIterator operator++(int)
    {	// postincrement
        CharIndexedVectorIterator _Tmp = *this;
        ++m_itr;
        return (_Tmp);
    }

    CharIndexedVectorIterator& operator--()
    {	// predecrement
        ++m_itr;
        return (*this);
    }

    CharIndexedVectorIterator operator--(int)
    {	// postdecrement
        CharIndexedVectorIterator _Tmp = *this;
        ++m_itr;
        return (_Tmp);
    }

    CharIndexedVectorIterator& operator+=(difference_type _Off)
    {	// increment by integer
        m_itr += _Off;
        return (*this);
    }

    CharIndexedVectorIterator& operator-=(difference_type _Off)
    {	// decrement by integer
        return (*this += -_Off);
    }

    bool operator<(const CharIndexedVectorIterator& rhs)
    {
        return m_itr < rhs.m_itr;
    }
};

/// a const_iterator for CharIndexedVector
template<class T>
class CharIndexedVectorConstIterator
{
    typedef boost::array<T, 129> type;
    typename type::const_iterator m_itr;

public:
    typedef typename type::value_type value_type;
    typedef typename type::iterator iterator;
    typedef typename type::iterator pointer;
    typedef typename type::const_iterator const_iterator;
    typedef typename type::difference_type difference_type;
    typedef typename type::reference reference;
    typedef typename type::const_reference const_reference;
    typedef typename type::size_type size_type;
    typedef std::random_access_iterator_tag iterator_category;

    CharIndexedVectorConstIterator(const const_iterator& itr) : m_itr(itr) {}

    const_reference operator*() const
    {
        return *m_itr;
    }

    bool operator!=(const CharIndexedVectorConstIterator& rhs) const
    {
        return m_itr != *(const_iterator*)&rhs;
    }

    difference_type operator-(const CharIndexedVectorConstIterator& rhs) const
    {
        return m_itr - rhs.m_itr;
    }

    CharIndexedVectorConstIterator& operator++()
    {	// preincrement
        ++m_itr;
        return (*this);
    }

    CharIndexedVectorConstIterator operator++(int)
    {	// postincrement
        CharIndexedVectorConstIterator _Tmp = *this;
        ++m_itr;
        return (_Tmp);
    }

    CharIndexedVectorConstIterator& operator--()
    {	// predecrement
        ++m_itr;
        return (*this);
    }

    CharIndexedVectorConstIterator operator--(int)
    {	// postdecrement
        CharIndexedVectorConstIterator _Tmp = *this;
        ++m_itr;
        return (_Tmp);
    }

    CharIndexedVectorConstIterator& operator+=(difference_type _Off)
    {	// increment by integer
        m_itr += _Off;
        return (*this);
    }

    CharIndexedVectorConstIterator& operator-=(difference_type _Off)
    {	// decrement by integer
        return (*this += -_Off);
    }

    bool operator<(const CharIndexedVectorConstIterator& rhs)
    {
        return m_itr < rhs.m_itr;
    }
};

/// a wrapper for boost::array that is indexable by character; supports indexes 0-127
template<class T>
struct CharIndexedVector : public boost::array<T, 129>
{
    typedef boost::array<T, 129> type;
    typedef CharIndexedVectorIterator<T> iterator;
    typedef CharIndexedVectorConstIterator<T> const_iterator;
    typedef std::reverse_iterator<iterator> reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

    CharIndexedVector()
    {
        clear();
    }

    size_t size() const
    {
        return 128;
    }

    void erase(const char c)
    {
        this->operator[](c) = T();
    }

    void clear()
    {
        std::fill(type::begin(), type::end(), T());
    }

    char getIndexAsChar(iterator itr) const
    {
        return 'A' + (itr - type::begin());
    }

    char getIndexAsChar(size_t i) const
    {
        return 'A' + (&this->operator[](i) - type::begin());
    }

    const T& operator[](const char c) const
    {
        return type::operator[]((size_t) c);
    }

    T& operator[](const char c)
    {
        return type::operator[]((size_t) c);
    }

    const_iterator begin() const    {return type::begin();}
    const_iterator end() const      {return type::end();}
    iterator begin()                {return type::begin();}
    iterator end()                  {return type::end();}
};

} // namespace util
} // namespace pwiz

#endif // _CHARINDEXEDVECTOR_HPP_