/****************************************************************************
 * This file was derived from the CNCL library by the Pablo research group.
 *
 * The complete CNCL library is available at:
 *       http://www.comnets.rwth-aachen.de/doc/cncl.html
 *
 * The CNCL copyright notice is included here:
 *
 * |_|_|_  |_|_    |_    |_|_|_  |_               C O M M U N I C A T I O N
 *|_       |_  |_  |_  |_        |_                         N E T W O R K S
 *|_       |_  |_  |_  |_        |_                               C L A S S
 * |_|_|_  |_    |_|_    |_|_|_  |_|_|_|_                     L I B R A R Y
 *
 *
 *
 * Copyright (C) 1992-1998   Communication Networks
 *                           Aachen University of Technology
 *                           D-52056 Aachen
 *                           Germany
 *                           Email: cncl-adm@comnets.rwth-aachen.de
 *
 *
 * This library is free  software; you can  redistribute it and/or modify
 * it  under   the terms  of the GNU   Library  General Public License as
 * published by the  Free Software Foundation;  either  version 2 of  the
 * License, or  (at  your option)  any  later  version.  This  library 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 Library General Public
 * Library General Public License along with  this library; if not, write
 * to the Free Software Foundation,  675  Mass Ave, Cambridge, MA  02139,
 * USA.
 ****************************************************************************/

// -*- C++ -*-
/*****************************************************************************
 *
 * Class: FlArrayObject  --- 1 dimensional FlObjPtr array class
 *
 *****************************************************************************/

#include "FlArrayObject.h"

using std::endl;


/***** Private functions *****************************************************/

void FlArrayObject::init(size_t sz)
{
    asize = sz;
    array = new FlObjPtr[sz];
    for (size_t i = 0; i < sz; i++)
	array[i] = def_val;
}



void FlArrayObject::delete_array()
{
    asize = 0;
    if(array)
	delete [] array;
    array = NIL;
}



/***** Constructors **********************************************************/

FlArrayObject::FlArrayObject(size_t sz, FlObjPtr def)
{
    def_val = def;
    init(sz);
}



FlArrayObject::FlArrayObject(const FlArrayObject &a)
{
    init(a.asize);
    for (size_t i = 0; i < asize; i++)
	array[i] = a.array[i];
}


FlArrayObject::~FlArrayObject()
{
    FlArrayObject::delete_array();
}



FlArrayObject &FlArrayObject::operator = (const FlArrayObject &a)
{
    delete_array();
    init(a.asize);
    for (size_t i = 0; i < asize; i++)
	array[i] = a.array[i];
    return *this;
}
    


/***** public functions ******************************************************/

void FlArrayObject::size(size_t sz)  // Set size of array
{
    if (array == NIL)
	init(sz);
    else if (sz != asize)
    {
	FlObjPtr *temp = array;
	array = new FlObjPtr[sz];
	for (size_t i = 0; i < sz; i++)
	    if (i < asize)
		array[i] = temp[i];
	    else
		array[i] = def_val;
	//*** modified
	delete [] temp;
	asize = sz;
    }
}



/***** Default I/O member function for FuzzyLib classes **************************/

// FlNormal output
void FlArrayObject::print(ostream &strm) const
{
    strm << "[ ";
    for(size_t i=0; i<asize; i++)
    {
	strm << array[i];
	if(i != asize-1)
	    strm << ", ";
    }
    strm << " ]";
}

// Debug output
void FlArrayObject::dump(ostream &strm) const
{
    strm << "FlArrayObject { $Revision: 1.3 $" << endl;
    strm << "size=" << asize
	 << " array=" << std::hex << (unsigned long)(array) << std::dec << endl;
    strm << "values=" << this << endl;
    strm << "}" << endl;
}

