summaryrefslogtreecommitdiffstats
path: root/rasodmg/iterator.cc
blob: cd9e167b648ebc96ae4ecb302558894b459d4444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* 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 <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * 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 <iostream>
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<class T>
r_Iterator<T>::r_Iterator()
{
   // use default constructor only with assignment operator!
   collection = 0;
   ptr = 0;
   ndone = 0;
}

template<class T>
r_Iterator<T>::r_Iterator( const r_Iterator<T>& iter )
{
   collection = iter.collection;
   ptr = iter.ptr;
   ndone = iter.ndone;
}

template<class T>
r_Iterator<T>::r_Iterator( r_Collection<T>& source, int removed_objects )
{
  collection = &source;
  // sorry for this awful cast but there is
  // no standard conversion of r_Collection<T>::CNode* to r_Collection::CNode*
  if( removed_objects )
    ptr = (typename r_Collection<T>::CNode*)source.removed_objects;    
  else
    ptr = (typename r_Collection<T>::CNode*)source.coll;

  ndone = (ptr->elem != 0);
}

template<class T>
r_Iterator<T>::~r_Iterator()
{
}

template<class T>
r_Iterator<T>&
r_Iterator<T>::operator=( const r_Iterator<T>& iter )
{
  if( this != &iter )
  {
    collection = iter.collection;
    ptr        = iter.ptr;
    ndone      = iter.ndone;
  }
  
  return *this;
}

template<class T>
int
r_Iterator<T>::is_equal(const r_Iterator<T>& iter) const
{
  if ( collection == iter.collection)
    if ( ptr == iter.ptr )
      return 1;
  return 0;
}


template<class T>
int 
operator==( const r_Iterator<T>& iter1, const r_Iterator<T>& iter2 )
{
  return iter1.is_equal(iter2);
}

template<class T>
int 
operator!=( const r_Iterator<T>& iter1, const r_Iterator<T>& iter2 )
{
  return !iter1.is_equal(iter2);
}

template<class T>
r_Iterator<T>& 
r_Iterator<T>::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<class T>
r_Iterator<T>
r_Iterator<T>::operator++( int )
{
  // postfix++ operator
  // create a copy of this, increment the original and return the copy
  r_Iterator<T> result( *this );
  
  operator++();

  return result;
}

template<class T>
T 
r_Iterator<T>::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<class T>
T
r_Iterator<T>::get_element() const
  throw( r_Error )
{
  if ( !ndone || ptr->elem == 0 )
    throw r_Error( r_Error::r_Error_IteratorExhausted );
  else
    return *(ptr->elem);
}

template<class T>
int
r_Iterator<T>::next( T& element )
{
  if ( !ndone || ptr->elem == 0 ) return 0;
  element = *(ptr->elem);
  advance();
  return 1;
}

template<class T>
void
r_Iterator<T>::reset( int removed_objects )
{
  if( removed_objects )
    ptr = (typename r_Collection<T>::CNode*)collection->removed_objects;
  else
    ptr = (typename r_Collection<T>::CNode*)collection->coll;

  ndone = (ptr->elem != 0);
}

template<class T>
void
r_Iterator<T>::advance()
{
  if ( !ndone )
    throw r_Error( r_Error::r_Error_IteratorExhausted );
  if ( ptr->next != 0 )
    ptr = ptr->next;
  else
    ndone = 0;
}