File: rangeseq.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (211 lines) | stat: -rw-r--r-- 4,707 bytes parent folder | download | duplicates (2)
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
/*
   FALCON - The Falcon Programming Language.
   FILE: rangeseq.cpp

   Virtual sequence that can be used to iterate over ranges.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Thu, 06 Aug 2009 22:10:00 +020

   -------------------------------------------------------------------
   (C) Copyright 2009: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/


#include <falcon/rangeseq.h>
#include <falcon/error.h>
#include <falcon/eng_messages.h>
#include <falcon/iterator.h>
#include <falcon/corerange.h>

namespace Falcon {

RangeSeq::RangeSeq( const CoreRange &rng )
{
   if ( rng.isOpen() )
      throw new ParamError( ErrorParam( e_param_range, __LINE__ )
            .origin( e_orig_runtime )
            .extra( "open range") );

   m_start = rng.start();
   m_end = rng.end();
   m_step = rng.step();

   // force the step to be non-default
   if( m_step == 0 )
      m_step = m_start <= m_end ? 1 : -1;
}

RangeSeq::RangeSeq( int64 s, int64 e, int64 step ):
   m_start(s),
   m_end(e),
   m_step(step)
{
   // force the step to be non-default
   if( m_step == 0 )
      m_step = m_start <= m_end ? 1 : -1;
}

RangeSeq::~RangeSeq()
{}

RangeSeq* RangeSeq::clone() const
{
   return new RangeSeq( *this );
}

const Item &RangeSeq::front() const
{
   m_number = m_start;
   return m_number;
}

const Item &RangeSeq::back() const
{
   m_number = m_end;
   return m_number;
}


void RangeSeq::clear()
{
   m_start = m_end;
   m_step = 1;
}

bool RangeSeq::empty() const
{
   return m_step > 0 ? m_start >= m_end : m_start < m_end;
}

void RangeSeq::append( const Item &data )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
         .origin( e_orig_runtime ).extra( "RangeSeq::append" ) );
}

void RangeSeq::prepend( const Item &data )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
         .origin( e_orig_runtime ).extra( "RangeSeq::prepend" ) );
}

void RangeSeq::getIterator( Iterator& tgt, bool tail ) const
{
   Sequence::getIterator( tgt, tail );
   tgt.position( m_start );
}

void RangeSeq::copyIterator( Iterator& tgt, const Iterator& source ) const
{
   Sequence::copyIterator( tgt, source );
   tgt.position( source.position() );
}

void RangeSeq::insert( Iterator &, const Item & )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
         .origin( e_orig_runtime ).extra( "RangeSeq::insert" ) );
}

void RangeSeq::erase( Iterator & )
{
   throw new CodeError( ErrorParam( e_not_implemented, __LINE__ )
         .origin( e_orig_runtime ).extra( "RangeSeq::erase" ) );
}


bool RangeSeq::hasNext( const Iterator &iter ) const
{
   return m_step > 0 ?
         iter.position() + m_step < m_end :
         iter.position() + m_step >= m_end;
}

bool RangeSeq::hasPrev( const Iterator &iter ) const
{
   return m_step > 0 ?
         iter.position() - m_step > m_start :
         iter.position() - m_step < m_end;

}

bool RangeSeq::hasCurrent( const Iterator &iter ) const
{
   return m_step > 0 ?
         iter.position() < m_end :
         iter.position() >= m_end;
}

bool RangeSeq::next( Iterator &iter ) const
{
   if( m_step > 0 )
   {
      if ( iter.position() < m_end )
         iter.position( iter.position() + m_step );
      return iter.position() < m_end;
   }
   else
   {
      if ( iter.position() >= m_end )
         iter.position( iter.position() + m_step );
      return iter.position() >= m_end;
   }
}

bool RangeSeq::prev( Iterator &iter ) const
{
   if( m_step > 0 )
   {
      if ( iter.position() > m_start )
      {
         iter.position( iter.position() - m_step );
         return true;
      }
      else {
         // move past end
         if( (m_end - m_start) % m_step == 0 )
            iter.position( m_end );
         else
            iter.position( ((m_end - m_start)/ m_step + 1 )* m_step + m_start);

         return false;
      }
   }
   else
   {
      if ( iter.position() < m_start )
      {
         iter.position( iter.position() + m_step );
         return true;
      }
      else
      {
         iter.position( ((m_start - m_end)/ m_step - 1 )* m_step + m_start);
         return false;
      }
   }
}

Item& RangeSeq::getCurrent( const Iterator &iter )
{
   m_number = iter.position();
   return m_number;
}

Item& RangeSeq::getCurrentKey( const Iterator &iter )
{
   throw new CodeError( ErrorParam( e_non_dict_seq, __LINE__ )
            .origin( e_orig_runtime ).extra( "RangeSeq::getCurrentKey" ) );
}

bool RangeSeq::equalIterator( const Iterator &first, const Iterator &second ) const
{
   return first.position() == second.position();
}

}

/* end of rangeseq.cpp */