ProteoWizard
OrderedPair.hpp
Go to the documentation of this file.
1 //
2 // $Id: OrderedPair.hpp 1832 2010-02-22 22:21:00Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2009 Center for Applied Molecular Medicine
8 // University of Southern California, Los Angeles, CA
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 _ORDEREDPAIR_HPP_
25 #define _ORDEREDPAIR_HPP_
26 
27 
29 #include <vector>
30 #include <iostream>
31 #include <stdexcept>
32 
33 
34 namespace pwiz {
35 namespace math {
36 
37 
38 #pragma pack(push, 1)
40 {
41  double x;
42  double y;
43 
44  OrderedPair(double _x = 0, double _y = 0) : x(_x), y(_y) {}
45 };
46 #pragma pack(pop)
47 
48 
49 inline std::ostream& operator<<(std::ostream& os, const OrderedPair& p)
50 {
51  os << "(" << p.x << "," << p.y << ")";
52  return os;
53 }
54 
55 
56 inline std::istream& operator>>(std::istream& is, OrderedPair& p)
57 {
58  char open='\0', comma='\0', close='\0';
59  is >> open >> p.x >> comma >> p.y >> close;
60  if (!is) return is;
61  if (open!='(' || comma!=',' || close!=')')
62  throw std::runtime_error("[OrderedPair::operator>>] Unexpected input.");
63  return is;
64 }
65 
66 
67 inline bool operator==(const OrderedPair& a, const OrderedPair& b)
68 {
69  return a.x==b.x && a.y==b.y;
70 }
71 
72 
73 inline bool operator!=(const OrderedPair& a, const OrderedPair& b)
74 {
75  return !(a == b);
76 }
77 
78 
79 ///
80 /// wrapper class for accessing contiguous data as a container of OrderedPairs;
81 /// note that it does not own the underlying data
82 ///
84 {
85  public:
86 
87  /// constructor for wrapping array of contiguous data
88  OrderedPairContainerRef(const void* begin, const void* end)
89  : begin_(reinterpret_cast<const OrderedPair*>(begin)),
90  end_(reinterpret_cast<const OrderedPair*>(end))
91  {}
92 
93  /// template constructor for automatic conversion from vector;
94  /// e.g. vector<double>, vector<OrderedPair>, vector<CustomPair>
95  template<typename T>
96  OrderedPairContainerRef(const std::vector<T>& v)
97  : begin_(reinterpret_cast<const OrderedPair*>(&v[0])),
98  end_(reinterpret_cast<const OrderedPair*>(&v[0]+v.size()))
99  {}
100 
101  typedef const OrderedPair* const_iterator;
102 
103  const_iterator begin() const {return begin_;}
104  const_iterator end() const {return end_;}
105  size_t size() const {return end_-begin_;}
106  const OrderedPair& operator[](size_t index) const {return *(begin_+index);}
107 
108  private:
109 
112 };
113 
114 
115 } // namespace math
116 } // namespace pwiz
117 
118 
119 #endif // _ORDEREDPAIR_HPP_
120