view oldclasslib/include/pair.h @ 24:a4899cdfc2d6 default tip

Obfuscate the regexps to strip off the IBM compiler's copyright banners. I don't want bots scanning github to think they're real copyright notices because that could cause real problems.
author David A. Holland
date Mon, 13 Jun 2022 00:40:23 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * 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