KrisLibrary  1.0.0
IntPair.h
1 #ifndef INT_PAIR_H
2 #define INT_PAIR_H
3 
4 #include <iosfwd>
5 
9 struct IntPair
10 {
11  inline IntPair() {}
12  inline IntPair(int _a,int _b) :a(_a),b(_b) {}
13  inline IntPair(const IntPair& t) :a(t.a),b(t.b) {}
14  inline bool operator == (const IntPair& t) const
15  { return a==t.a&&b==t.b; }
16  inline bool operator != (const IntPair& t) const { return !operator==(t); }
17  inline bool operator < (const IntPair& t) const
18  { return a<t.a || (a==t.a && b<t.b); }
19  inline void set(int _a,int _b) { a=_a; b=_b; }
20  inline int operator[](int i) const { return data[i]; } //i={0,1}
21  inline int& operator[](int i) { return data[i]; } //i={0,1}
22  inline int getIndex(int x) const {
23  for(int i=0;i<2;i++) if(x==data[i]) return i;
24  return -1;
25  }
26  inline bool contains(int x) const { return a==x||b==x; }
27  inline bool contains(int x,int& index) const {
28  index=getIndex(x);
29  return (index>=0);
30  }
31  inline int count(int x) const {
32  int n=0;
33  for(int i=0;i<2;i++) if(x==data[i]) n++;
34  return n;
35  }
36  inline void operator += (int ofs) { a+=ofs; b+=ofs; }
37  inline void operator -= (int ofs) { a-=ofs; b-=ofs; }
38 
39  union {
40  int data[2];
41  struct { int a,b; };
42  };
43 };
44 
45 std::ostream& operator << (std::ostream& out,const IntPair& t);
46 std::istream& operator >> (std::istream& in,IntPair& t);
47 
48 #endif
A lightweight integer 2-tuple class.
Definition: IntPair.h:9