// $Id$  -*- C++ -*-
// BoxExtend class

// Copyright (C) 1995 Technische Universitaet Braunschweig, Germany.
// Copyright (C) 2004 Free Software Foundation, Inc.
// Written by Andreas Zeller <zeller@gnu.org>.
// 
// This file is part of DDD.
// 
// DDD is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
// 
// DDD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with DDD -- see the file COPYING.
// If not, see <http://www.gnu.org/licenses/>.
// 
// DDD is the data display debugger.
// For details, see the DDD World-Wide-Web page, 
// `http://www.gnu.org/software/ddd/',
// or send a mail to the DDD developers <ddd@gnu.org>.

#ifndef _DDD_BoxExtend_h
#define _DDD_BoxExtend_h

// A `BoxExtend' denotes the extensibility of a box - that is, how
// much the box extends to fill up available space.  An extensibility
// of 0 means that the box will not extend at all.  The bigger the
// extensibility, the more remaining space is assigned to the box.
// For example, let A and B be two boxes; A has a horizontal
// extensibility of 1, B has a horizontal extensibility of 2.  If A
// and B are horizontally concatenated, A gets 1/3 of the remaining
// space and B gets 2/3 of the remaining space.

#include "BoxPoint.h"
#include "misc.h" // min(), max()

class BoxExtend: public BoxPoint {
public:
    // Constructors
    BoxExtend(BoxCoordinate x_extend, BoxCoordinate y_extend):
	BoxPoint(x_extend, y_extend)
    {}
    BoxExtend(BoxCoordinate c): 
	BoxPoint(c)
    {}
    BoxExtend():
	BoxPoint()
    {}
    BoxExtend(const BoxPoint& p):
	BoxPoint(p)
    {}

    // Horizontal concatenation
    BoxExtend operator & (const BoxExtend& e) const
    {
        return
	  (isValid() && e.isValid())
	  ? BoxExtend(point[X] + e.point[X],
		      min(point[Y],e.point[Y]))
	  : BoxExtend();
    }

    // Vertical concatenation
    BoxExtend operator | (const BoxExtend& e) const
    {
        return
	  (isValid() && e.isValid())
	  ? BoxExtend(min(point[X], e.point[X]),
		      point[Y] + e.point[Y])
	  : BoxExtend();
    }

    // Stacked concatenation
    BoxExtend operator ^ (const BoxExtend& e) const
    {
	return
	  (isValid() && e.isValid())
	  ? BoxExtend(max(point[X], e.point[X]),
		      max(point[Y], e.point[Y]))
	  : BoxExtend();
    }

    // Assignment versions
    void operator &= (const BoxExtend& e)
    {
	if (isValid() && e.isValid())
	{
	    point[X] += e.point[X];
	    point[Y] = min(point[Y], e.point[Y]);
	}
    }
    void operator |= (const BoxExtend& e) 
    {
	if (isValid() && e.isValid())
	{
	    point[X] = min(point[X], e.point[X]);
	    point[Y] += e.point[Y];
	}
    }
    void operator ^= (const BoxExtend& e) 
    {
	if (isValid() && e.isValid())
	{
	    point[X] = max(point[X], e.point[X]);
	    point[Y] = max(point[Y], e.point[Y]);
	}
    }

    // Set extensibility to zero
    void fix (BoxDimension dimension) 
    {
	if (isValid())
	    point[dimension] = 0;
    }
};

// I/O
inline std::ostream& operator << (std::ostream& stream, const BoxExtend& extend)
{
    const BoxPoint& p = extend;
    return operator << (stream, p);
}

#endif // _DDD_BoxExtend_h
// DON'T ADD ANYTHING BEHIND THIS #endif
