Mercurial > ~dholland > hg > ag > index.cgi
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/oldclasslib/include/pair.h Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,117 @@ +/* + * AnaGram, A System for Syntax Directed Parsing + * + * Template class to define pair arithmetic. + * + * The class is completely defined within this file. + * There is no supporting CPP module. + * + * Multiplication, division and comparison are element-wise. + * Note that !(p < q) does not imply p >= q. + * + * Pairs of integers are convenient for manipulating screen coordinates. + * + * Copyright 1993 Parsifal Software. All Rights Reserved. + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +#ifndef PAIR_H +#define PAIR_H + +// Some forward decls required in current C++ +template <class T> class pair; +template <class T> pair <T> operator *(const T k, const pair <T> &p); +template <class T> pair <T> max(const pair <T> &p, const pair <T> &q); +template <class T> pair <T> min(const pair <T> &p, const pair <T> &q); + + +template <class T> +class pair { +public: + T x,y; + pair(T a, T b) { x = a; y = b; } + pair() { x = y = 0; } + + pair<T> operator + (const pair<T> &p) const { + pair<T> r; + r.x = x + p.x; + r.y = y + p.y; + return r; + } + pair<T> operator - (const pair<T> &p) const { + pair<T> r; + r.x = x - p.x; + r.y = y - p.y; + return r; + } + pair<T> operator * (const pair<T> &p) const { + pair<T> r; + r.x = x * p.x; + r.y = y * p.y; + return r; + } + pair<T> operator / (const pair<T> &p) const { + pair<T> r; + r.x = x / p.x; + r.y = y / p.y; + return r; + } + int operator < (const pair<T> &p) const { return x < p.x && y < p.y; } + int operator > (const pair<T> &p) const { return x > p.x && y > p.y; } + int operator <= (const pair<T> &p) const { return x <= p.x && y <= p.y; } + int operator >= (const pair<T> &p) const { return x >= p.x && y >= p.y; } + int operator == (const pair<T> &p) const { return x == p.x && y == p.y; } + int operator != (const pair<T> &p) const { return x != p.x || y != p.y; } + + /*friend pair<T> operator *<> (const T k, const pair<T> &p);*/ + + pair<T> operator / (const T k) const { + pair<T> r; + r.x = x / k; + r.y = y / k; + return r; + } + + /*friend pair<T> max<>(const pair<T> &p, const pair<T> &q);*/ + /*friend pair<T> min<>(const pair<T> &p, const pair<T> &q);*/ +}; + +template <class T> +inline pair <T> operator *(const T k, const pair <T> &p) { + pair <T> r; + r.x = k*p.x; + r.y = k*p.y; + return r; +} + +template <class T> +inline pair <T> max(const pair <T> &p, const pair <T> &q) { + pair<T> r = p; + if (q.x > r.x) r.x = q.x; + if (q.y > r.y) r.y = q.y; + return r; +} + +template <class T> +inline pair <T> min(const pair <T> &p, const pair <T> &q) { + pair<T> r = p; + if (q.x < r.x) r.x = q.x; + if (q.y < r.y) r.y = q.y; + return r; +} +#endif