comparison oldclasslib/include/pair.h @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Parsing
3 *
4 * Template class to define pair arithmetic.
5 *
6 * The class is completely defined within this file.
7 * There is no supporting CPP module.
8 *
9 * Multiplication, division and comparison are element-wise.
10 * Note that !(p < q) does not imply p >= q.
11 *
12 * Pairs of integers are convenient for manipulating screen coordinates.
13 *
14 * Copyright 1993 Parsifal Software. All Rights Reserved.
15 *
16 * This software is provided 'as-is', without any express or implied
17 * warranty. In no event will the authors be held liable for any damages
18 * arising from the use of this software.
19 *
20 * Permission is granted to anyone to use this software for any purpose,
21 * including commercial applications, and to alter it and redistribute it
22 * freely, subject to the following restrictions:
23 *
24 * 1. The origin of this software must not be misrepresented; you must not
25 * claim that you wrote the original software. If you use this software
26 * in a product, an acknowledgment in the product documentation would be
27 * appreciated but is not required.
28 * 2. Altered source versions must be plainly marked as such, and must not be
29 * misrepresented as being the original software.
30 * 3. This notice may not be removed or altered from any source distribution.
31 */
32
33 #ifndef PAIR_H
34 #define PAIR_H
35
36 // Some forward decls required in current C++
37 template <class T> class pair;
38 template <class T> pair <T> operator *(const T k, const pair <T> &p);
39 template <class T> pair <T> max(const pair <T> &p, const pair <T> &q);
40 template <class T> pair <T> min(const pair <T> &p, const pair <T> &q);
41
42
43 template <class T>
44 class pair {
45 public:
46 T x,y;
47 pair(T a, T b) { x = a; y = b; }
48 pair() { x = y = 0; }
49
50 pair<T> operator + (const pair<T> &p) const {
51 pair<T> r;
52 r.x = x + p.x;
53 r.y = y + p.y;
54 return r;
55 }
56 pair<T> operator - (const pair<T> &p) const {
57 pair<T> r;
58 r.x = x - p.x;
59 r.y = y - p.y;
60 return r;
61 }
62 pair<T> operator * (const pair<T> &p) const {
63 pair<T> r;
64 r.x = x * p.x;
65 r.y = y * p.y;
66 return r;
67 }
68 pair<T> operator / (const pair<T> &p) const {
69 pair<T> r;
70 r.x = x / p.x;
71 r.y = y / p.y;
72 return r;
73 }
74 int operator < (const pair<T> &p) const { return x < p.x && y < p.y; }
75 int operator > (const pair<T> &p) const { return x > p.x && y > p.y; }
76 int operator <= (const pair<T> &p) const { return x <= p.x && y <= p.y; }
77 int operator >= (const pair<T> &p) const { return x >= p.x && y >= p.y; }
78 int operator == (const pair<T> &p) const { return x == p.x && y == p.y; }
79 int operator != (const pair<T> &p) const { return x != p.x || y != p.y; }
80
81 /*friend pair<T> operator *<> (const T k, const pair<T> &p);*/
82
83 pair<T> operator / (const T k) const {
84 pair<T> r;
85 r.x = x / k;
86 r.y = y / k;
87 return r;
88 }
89
90 /*friend pair<T> max<>(const pair<T> &p, const pair<T> &q);*/
91 /*friend pair<T> min<>(const pair<T> &p, const pair<T> &q);*/
92 };
93
94 template <class T>
95 inline pair <T> operator *(const T k, const pair <T> &p) {
96 pair <T> r;
97 r.x = k*p.x;
98 r.y = k*p.y;
99 return r;
100 }
101
102 template <class T>
103 inline pair <T> max(const pair <T> &p, const pair <T> &q) {
104 pair<T> r = p;
105 if (q.x > r.x) r.x = q.x;
106 if (q.y > r.y) r.y = q.y;
107 return r;
108 }
109
110 template <class T>
111 inline pair <T> min(const pair <T> &p, const pair <T> &q) {
112 pair<T> r = p;
113 if (q.x < r.x) r.x = q.x;
114 if (q.y < r.y) r.y = q.y;
115 return r;
116 }
117 #endif