ProteoWizard
IntegerSet.hpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2007 Spielberg Family Center for Applied Proteomics
8// Cedars Sinai Medical Center, Los Angeles, California 90048
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21//
22
23
24#ifndef _INTEGERSET_HPP_
25#define _INTEGERSET_HPP_
26
28#include <list>
29#include <ostream>
30
31namespace pwiz {
32namespace util {
33
34
35/// a virtual container of integers, accessible via an iterator interface,
36/// stored as union of intervals
38{
39 public:
40
41 /// a single closed interval of integers
43 {
44 int begin;
45 int end;
46
47 Interval(int a = 0); // allow int conversion
48 Interval(int a, int b);
49
50 bool contains(int n) const {return n>=begin && n<=end;}
51
52 friend PWIZ_API_DECL std::ostream& operator<<(std::ostream& os, const Interval& interval);
53 friend PWIZ_API_DECL std::istream& operator>>(std::istream& is, Interval& interval);
54 };
55
56 /// collection of Interval objects
57 typedef std::list<Interval> Intervals;
58
59 /// forward iterator providing readonly access to the virtual container
61 {
62 public:
63
64 /// \name instantiation
65 //@{
66 /// default constructed Iterator marks end of any IntegerSet
68
69 /// initialized to beginning of the IntegerSet
70 Iterator(const IntegerSet& integerSet);
71 //@}
72
73 /// \name forward iterator operators
74 //@{
76 const Iterator operator++(int);
77 int operator*() const; // note return by value
78 bool operator!=(const Iterator& that) const;
79 bool operator==(const Iterator& that) const;
80 //@}
81
82 /// \name standard iterator typedefs
83 //@{
84 typedef std::forward_iterator_tag iterator_category;
85 typedef int value_type;
86 typedef int difference_type;
89 //@}
90
91 private:
92 Intervals::const_iterator it_;
93 Intervals::const_iterator end_;
94 int value_;
95 };
96
97 /// default construction
99
100 /// construction with a single integer
101 explicit IntegerSet(int a);
102
103 /// construction with a single interval
104 IntegerSet(int a, int b);
105
106 /// \name write access to the virtual container
107 //@{
108
109 /// insert an interval of integers into the virtual container
110 void insert(Interval interval);
111
112 /// insert a single integer into the virtual container
113 void insert(int a);
114
115 /// insert an interval of integers into the virtual container
116 void insert(int a, int b);
117
118 /// insert intervals by parsing a string representing a
119 /// whitespace-delimited list of closed intervals:
120 /// parse(" [-3,2] 5 8-9 10- "); // insert(-3,2); insert(5); insert(8,9); insert(10,INT_MAX);
121 void parse(const std::string& intervalList);
122 //@}
123
124 /// \name const iterator interface to the virtual container
125 //@{
129 //@}
130
131 /// true iff IntegerSet is empty
132 bool empty() const {return intervals_.empty();}
133
134 /// true iff n is in the IntegerSet
135 bool contains(int n) const;
136
137 /// true iff n is an upper bound for the IntegerSet
138 bool hasUpperBound(int n) const;
139
140 /// returns the number of intervals in the set
141 size_t intervalCount() const;
142
143 /// returns the number of integers in the set
144 size_t size() const;
145
146 private:
147
149
150 friend PWIZ_API_DECL std::ostream& operator<<(std::ostream& os, const IntegerSet& integerSet);
151};
152
153
154} // namespace util
155} // namespace pwiz
156
157
158#endif // _INTEGERSET_HPP_
159
#define PWIZ_API_DECL
Definition Export.hpp:32
forward iterator providing readonly access to the virtual container
bool operator!=(const Iterator &that) const
const Iterator operator++(int)
Iterator(const IntegerSet &integerSet)
initialized to beginning of the IntegerSet
std::forward_iterator_tag iterator_category
bool operator==(const Iterator &that) const
Intervals::const_iterator it_
Iterator()
default constructed Iterator marks end of any IntegerSet
Intervals::const_iterator end_
a virtual container of integers, accessible via an iterator interface, stored as union of intervals
IntegerSet(int a)
construction with a single integer
void insert(Interval interval)
insert an interval of integers into the virtual container
const_iterator end() const
void insert(int a)
insert a single integer into the virtual container
void insert(int a, int b)
insert an interval of integers into the virtual container
IntegerSet(int a, int b)
construction with a single interval
friend PWIZ_API_DECL std::ostream & operator<<(std::ostream &os, const IntegerSet &integerSet)
IntegerSet()
default construction
bool empty() const
true iff IntegerSet is empty
const_iterator begin() const
size_t size() const
returns the number of integers in the set
bool hasUpperBound(int n) const
true iff n is an upper bound for the IntegerSet
std::list< Interval > Intervals
collection of Interval objects
bool contains(int n) const
true iff n is in the IntegerSet
size_t intervalCount() const
returns the number of intervals in the set
void parse(const std::string &intervalList)
insert intervals by parsing a string representing a whitespace-delimited list of closed intervals: pa...
a single closed interval of integers
friend PWIZ_API_DECL std::ostream & operator<<(std::ostream &os, const Interval &interval)
friend PWIZ_API_DECL std::istream & operator>>(std::istream &is, Interval &interval)