Merge branch 'gh/maint-clean' into ht/-include
[shapes.git] / source / singlelistrange.h
blob05062bfe782fdc84de235a664083c5a3ff81a14e
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2010 Henrik Tidefelt
19 #include "containertypes.h"
20 #include "shapesexceptions.h"
21 #include "continuations.h"
22 #include "globals.h"
24 /* This file is divided in two parts. The first part contains the declarations.
26 namespace Shapes
28 namespace Lang
31 template< class T >
32 class SingleListRange : public Lang::SingleList
34 typedef typename T::ValueType elem_type;
35 elem_type begin_;
36 elem_type step_;
37 size_t count_;
38 public:
39 SingleListRange( elem_type begin, elem_type step, size_t count );
40 virtual ~SingleListRange( );
41 virtual bool isNull( ) const;
42 virtual void foldl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
43 virtual void foldr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
44 virtual void foldsl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
45 virtual void foldsr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
46 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
48 void expand( std::vector< elem_type > * dst ) const;
55 /* This is the start of the second part of the file, containing the implementation of the templates declared in the first part.
57 namespace Shapes
60 template< class T >
61 Lang::SingleListRange< T >::SingleListRange( elem_type begin, elem_type step, size_t count )
62 : begin_( begin ), step_( step ), count_( count )
63 { }
65 template< class T >
66 Lang::SingleListRange< T >::~SingleListRange( )
67 { }
69 template< class T >
70 bool
71 Lang::SingleListRange< T >::isNull( ) const
73 return count_ == 0;
76 template< class T >
77 void
78 Lang::SingleListRange< T >::foldl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const
80 if( count_ == 0 )
82 Kernel::ContRef cont = evalState->cont_;
83 cont->takeHandle( nullResult,
84 evalState );
85 return;
88 evalState->cont_ = Kernel::ContRef( new Kernel::SingleFoldLCont( RefCountPtr< const Lang::SingleList >( new Lang::SingleListRange< T >( begin_ + step_, step_, count_ - 1 ) ),
89 op, evalState->dyn_, evalState->cont_, callLoc ) );
91 op->call( op, evalState, nullResult, Helpers::newValHandle( new T( begin_ ) ), callLoc );
94 template< class T >
95 void
96 Lang::SingleListRange< T >::foldr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const
98 if( count_ == 0 )
100 Kernel::ContRef cont = evalState->cont_;
101 cont->takeHandle( nullResult,
102 evalState );
103 return;
106 evalState->cont_ = Kernel::ContRef( new Kernel::SingleFoldRCont( Helpers::newValHandle( new T( begin_ ) ), op, evalState->dyn_, evalState->cont_, callLoc ) );
108 Lang::SingleListRange< T >( begin_ + step_, step_, count_ - 1 ).foldr( evalState, op, nullResult, callLoc );
111 template< class T >
112 void
113 Lang::SingleListRange< T >::foldsl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const
115 if( count_ == 0 )
117 Kernel::ContRef cont = evalState->cont_;
118 cont->takeHandle( nullResult,
119 evalState );
120 return;
123 evalState->cont_ = Kernel::ContRef( new Kernel::SingleFoldSLCont( RefCountPtr< const Lang::SingleList >( new Lang::SingleListRange< T >( begin_ + step_, step_, count_ - 1 ) ),
124 op, state, evalState->dyn_, evalState->cont_, callLoc ) );
126 op->call( op, evalState, nullResult, Helpers::newValHandle( new T( begin_ ) ), state, callLoc );
129 template< class T >
130 void
131 Lang::SingleListRange< T >::foldsr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const
133 if( count_ == 0 )
135 Kernel::ContRef cont = evalState->cont_;
136 cont->takeHandle( nullResult,
137 evalState );
138 return;
141 evalState->cont_ = Kernel::ContRef( new Kernel::SingleFoldSRCont( Helpers::newValHandle( new T( begin_ ) ), op, state, evalState->dyn_, evalState->cont_, callLoc ) );
143 Lang::SingleListRange< T >( begin_ + step_, step_, count_ - 1 ).foldsr( evalState, op, nullResult, state, callLoc );
146 template< class T >
147 void
148 Lang::SingleListRange< T >::expand( std::vector< elem_type > * dst ) const
150 dst->reserve( dst->size( ) + count_ );
151 size_t c = count_;
152 elem_type i = begin_;
153 while( c > 0 )
155 dst->push_back( i );
156 i += step_;
157 --c;