/*
* This file is part of rasdaman community.
*
* Rasdaman community 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.
*
* Rasdaman community 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 rasdaman community. If not, see .
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see
* or contact Peter Baumann via .
/
/**
* SOURCE: iterator.cc
*
* MODULE: rasodmg
* CLASS: r_Iterator
*
* COMMENTS:
* None
*/
static const char rcsiditerator[] = "@(#)rasodmg, r_Iterator: $Id: iterator.cc,v 1.18 2002/08/23 11:18:44 schatz Exp $";
#include
using namespace std;
#include "rasodmg/iterator.hh"
#ifdef EARLY_TEMPLATE
#ifndef __EXECUTABLE__
#define __EXECUTABLE__
#define ITERATOR_NOT_SET
#endif
#endif
#include "rasodmg/collection.hh"
#ifdef ITERATOR_NOT_SET
#undef __EXECUTABLE__
#endif
template
r_Iterator::r_Iterator()
{
// use default constructor only with assignment operator!
collection = 0;
ptr = 0;
ndone = 0;
}
template
r_Iterator::r_Iterator( const r_Iterator& iter )
{
collection = iter.collection;
ptr = iter.ptr;
ndone = iter.ndone;
}
template
r_Iterator::r_Iterator( r_Collection& source, int removed_objects )
{
collection = &source;
// sorry for this awful cast but there is
// no standard conversion of r_Collection::CNode* to r_Collection::CNode*
if( removed_objects )
ptr = (typename r_Collection::CNode*)source.removed_objects;
else
ptr = (typename r_Collection::CNode*)source.coll;
ndone = (ptr->elem != 0);
}
template
r_Iterator::~r_Iterator()
{
}
template
r_Iterator&
r_Iterator::operator=( const r_Iterator& iter )
{
if( this != &iter )
{
collection = iter.collection;
ptr = iter.ptr;
ndone = iter.ndone;
}
return *this;
}
template
int
r_Iterator::is_equal(const r_Iterator& iter) const
{
if ( collection == iter.collection)
if ( ptr == iter.ptr )
return 1;
return 0;
}
template
int
operator==( const r_Iterator& iter1, const r_Iterator& iter2 )
{
return iter1.is_equal(iter2);
}
template
int
operator!=( const r_Iterator& iter1, const r_Iterator& iter2 )
{
return !iter1.is_equal(iter2);
}
template
r_Iterator&
r_Iterator::operator++()
{
// ++prefix operator
if ( !ndone )
throw r_Error( r_Error::r_Error_IteratorExhausted );
if ( ptr->next != 0 )
ptr = ptr->next;
else
ndone = 0;
return *this;
}
template
r_Iterator
r_Iterator::operator++( int )
{
// postfix++ operator
// create a copy of this, increment the original and return the copy
r_Iterator result( *this );
operator++();
return result;
}
template
T
r_Iterator::operator*()
throw( r_Error )
{
if ( !ndone || ptr->elem == 0 )
{
r_Error err = r_Error( r_Error::r_Error_IteratorExhausted );
throw err;
}
// The following line was return *(ptr->elem) but the HP compiler had problems
// while instantiating the code. CNode::elem was of a different type than T.
return *((T*)ptr->elem);
}
template
T
r_Iterator::get_element() const
throw( r_Error )
{
if ( !ndone || ptr->elem == 0 )
throw r_Error( r_Error::r_Error_IteratorExhausted );
else
return *(ptr->elem);
}
template
int
r_Iterator::next( T& element )
{
if ( !ndone || ptr->elem == 0 ) return 0;
element = *(ptr->elem);
advance();
return 1;
}
template
void
r_Iterator::reset( int removed_objects )
{
if( removed_objects )
ptr = (typename r_Collection::CNode*)collection->removed_objects;
else
ptr = (typename r_Collection::CNode*)collection->coll;
ndone = (ptr->elem != 0);
}
template
void
r_Iterator::advance()
{
if ( !ndone )
throw r_Error( r_Error::r_Error_IteratorExhausted );
if ( ptr->next != 0 )
ptr = ptr->next;
else
ndone = 0;
}