haiku/src/libs/alm/Column.cpp
Ingo Weinhold 0306945545 Patch by Christof Lutteroth:
* copyright headers for the files of the libraries linprog and alm
* new class Summand for representing summands in a linear constraint
* merged class SoftConstraint into class Constraint; Constraint now
  supports both soft and hard constraint functionality
* new AddConstraint methods in class LinearSpec for directly setting
  constraints with 1 to 4 summands
* code cleanups by using aforementioned AddConstraint methods
* a new very simple test application for alm
* some style corrections


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24108 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-02-25 01:54:05 +00:00

215 lines
3.9 KiB
C++

/*
* Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
* Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
* Distributed under the terms of the MIT License.
*/
#include "Column.h"
#include "BALMLayout.h"
#include "OperatorType.h"
#include "XTab.h"
#include <SupportDefs.h>
/**
* The left boundary of the column.
*/
XTab*
Column::Left() const
{
return fLeft;
}
/**
* The right boundary of the column.
*/
XTab*
Column::Right() const
{
return fRight;
}
/**
* Gets the column directly to the left of this column.
*/
Column*
Column::Previous() const
{
return fPrevious;
}
/**
* Sets the column directly to the left of this column.
* May be null.
*/
void
Column::SetPrevious(Column* value)
{
// if there should be no column directly left of this column, then we have to
// separate any such column and can remove any constraint that was used
// to glue this column to it
if (value == NULL) {
if (fPrevious == NULL) return;
fPrevious->fNext = NULL;
fPrevious->fNextGlue = NULL;
fPrevious = NULL;
delete fPreviousGlue;
fPreviousGlue = NULL;
return;
}
// otherwise we have to set up the pointers and the glue constraint accordingly
if (value->fNext != NULL)
value->SetNext(NULL);
if (fPrevious != NULL)
SetPrevious(NULL);
fPrevious = value;
fPrevious->fNext = this;
value->fNextGlue = value->fRight->IsEqual(fLeft);
fPreviousGlue = value->fNextGlue;
}
/**
* Gets the column directly to the right of this column.
*/
Column*
Column::Next() const
{
return fNext;
}
/**
* Sets the column directly to the right of this column.
* May be null.
*/
void
Column::SetNext(Column* value)
{
// if there should be no column directly right of this column, then we have to
// separate any such column and can remove any constraint that was used
// to glue this column to it
if (value == NULL) {
if (fNext == NULL) return;
fNext->fPrevious = NULL;
fNext->fPreviousGlue = NULL;
fNext = NULL;
delete fNextGlue;
fNextGlue = NULL;
return;
}
// otherwise we have to set up the pointers and the glue constraint accordingly
if (value->fPrevious != NULL)
value->SetPrevious(NULL);
if (fNext != NULL)
SetNext(NULL);
fNext = value;
fNext->fPrevious = this;
value->fPreviousGlue = fRight->IsEqual(value->fLeft);
fNextGlue = value->fPreviousGlue;
}
//~ string Column::ToString() {
//~ return "Column(" + fLeft.ToString() + ", " + fRight.ToString() + ")";
//~ }
/**
* Inserts the given column directly to the left of this column.
*
* @param column the column to insert
*/
void
Column::InsertBefore(Column* column)
{
SetPrevious(column->fPrevious);
SetNext(column);
}
/**
* Inserts the given column directly to the right of this column.
*
* @param column the column to insert
*/
void
Column::InsertAfter(Column* column)
{
SetNext(column->fNext);
SetPrevious(column);
}
/**
* Constrains this column to have the same width as the given column.
*
* @param column the column that should have the same width
* @return the resulting same-width constraint
*/
Constraint*
Column::HasSameWidthAs(Column* column)
{
Constraint* constraint = fLS->AddConstraint(
-1.0, fLeft, 1.0, fRight, 1.0, column->fLeft, -1.0, column->fRight,
OperatorType(EQ), 0.0);
fConstraints->AddItem(constraint);
return constraint;
}
/**
* Gets the constraints.
*/
BList*
Column::Constraints() const
{
return fConstraints;
}
/**
* Sets the constraints.
*/
void
Column::SetConstraints(BList* constraints)
{
fConstraints = constraints;
}
/**
* Destructor.
* Removes the column from the specification.
*/
Column::~Column()
{
if (fPrevious != NULL)
fPrevious->SetNext(fNext);
for (int32 i = 0; i < fConstraints->CountItems(); i++)
delete (Constraint*)fConstraints->ItemAt(i);
delete fLeft;
delete fRight;
}
/**
* Constructor.
*/
Column::Column(BALMLayout* ls)
{
fLS = ls;
fLeft = new XTab(ls);
fRight = new XTab(ls);
fConstraints = new BList(1);
}