KrisLibrary  1.0.0
IntTriple.h
1 #ifndef INT_TRIPLE_H
2 #define INT_TRIPLE_H
3 
4 #include <iosfwd>
5 
9 struct IntTriple
10 {
11  inline IntTriple() {}
12  inline IntTriple(int _a,int _b, int _c) :a(_a),b(_b),c(_c) {}
13  inline IntTriple(const IntTriple& t) :a(t.a),b(t.b),c(t.c) {}
14  inline bool operator == (const IntTriple& t) const
15  { return a==t.a&&b==t.b&&c==t.c; }
16  inline bool operator != (const IntTriple& t) const { return !operator==(t); }
17  inline bool operator < (const IntTriple& t) const
18  { return a<t.a || (a==t.a && (b<t.b || (b==t.b && c<t.c))); }
19  inline void set(int _a,int _b,int _c) { a=_a; b=_b; c=_c; }
20  inline int operator[](int i) const { return data[i]; } //i={0,1,2}
21  inline int& operator[](int i) { return data[i]; } //i={0,1,2}
22  inline int getIndex(int x) const {
23  for(int i=0;i<3;i++) if(x==data[i]) return i;
24  return -1;
25  }
26  inline bool contains(int x) const { return a==x||b==x||c==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<3;i++) if(x==data[i]) n++;
34  return n;
35  }
36  inline void getCompliment(int i,int& v1,int& v2) const {
37  v1=data[(i+1)%3];
38  v2=data[(i+2)%3];
39  }
40  inline int getCompliment(int i1,int i2) const {
41  return data[3-i1-i2];
42  }
43  inline void operator += (int ofs) { a+=ofs; b+=ofs; c+=ofs; }
44  inline void operator -= (int ofs) { a-=ofs; b-=ofs; c-=ofs; }
45 
46  union {
47  int data[3];
48  struct { int a,b,c; };
49  };
50 };
51 
52 std::ostream& operator << (std::ostream& out,const IntTriple& t);
53 std::istream& operator >> (std::istream& in,IntTriple& t);
54 
55 #endif
A lightweight integer 3-tuple class.
Definition: IntTriple.h:9