// Emacs: -*- C++ -*-

//
//	Copyright 1993, Center for Computer Vision and Visualization,
//	University of Florida.  All rights reserved.
//


//
// $Log: reduce_fn_nz,v $
// Revision 1.4  1994/07/25  17:26:39  thoth
// Name sanitization
//
// Revision 1.3  1994/01/07  15:12:35  thoth
// Image class is now CoreImage and named image types are
// Image<P,T>.
//
// Revision 1.2  1993/12/29  17:33:18  thoth
// New operator scheme that prevents the need for trivial Image conversions.
//
// Revision 1.1  1993/09/15  13:04:51  thoth
// Initial revision
//
// Revision 1.2  93/05/27  11:49:03  thoth
// Copyright Notices
// 
// Revision 1.1  93/03/10  13:49:13  thoth
// Initial revision
// 

static ATYPE vec_reduce_OPNAME(const IA_VectorI<IA_Point<int>, LTYPE> *arg)
{
    const LTYPE *ls = arg->vec;

    ATYPE	rval=*(ls++);

    const unsigned sz = arg->domain().card();
    for (unsigned i=1; i<sz; i++) {
	rval = SUBOP( rval, (*(ls++)));
    }

    return rval;
}

#ifndef const_reduce_ATYPE_LTYPE_OPNAME
#define const_reduce_ATYPE_LTYPE_OPNAME
static ATYPE const_reduce_OPNAME(unsigned sz, LTYPE arg)
{
    ATYPE	rval=arg;
    for (unsigned i=1; i<sz; i++) {
	rval = SUBOP(rval, arg);
    }
    return rval;
}
#endif

static ATYPE iter_reduce_OPNAME(const IA_CoreImage<IA_Point<int>, LTYPE> &arg)
{
    IA_IVIter<IA_Point<int>, LTYPE>	iter(arg);

    LTYPE	temp;
    iter(temp);

    ATYPE	rval=temp;

    while(iter(temp)) {
	rval = SUBOP(rval, temp);
    }

    return rval;
}

ATYPE OP ( const IA_CoreImage<IA_Point<int>, LTYPE> &arg_)
{
    if (arg_.domain().card()==0)
	return ( ZERO );
    IA_Image<IA_Point<int>, LTYPE>	arg(arg_);

    if (arg.type() == IA_VectorI<IA_Point<int>, LTYPE>::s_type()) {
	return vec_reduce_OPNAME((IA_VectorI<IA_Point<int>, LTYPE> *)arg.bip);
    } else if (arg.type() == IA_ConstI<IA_Point<int>, LTYPE>::s_type()) {
	return const_reduce_OPNAME(arg.domain().card(),
				   ((IA_ConstI<IA_Point<int>, LTYPE> *)arg.bip)->value);
    } else {
	return iter_reduce_OPNAME(arg);
    }
}
