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
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"
24 /* This file is divided in two parts. The first part contains the declarations.
32 class SingleListRange
: public Lang::SingleList
34 typedef typename
T::ValueType elem_type
;
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.
61 Lang::SingleListRange
< T
>::SingleListRange( elem_type begin
, elem_type step
, size_t count
)
62 : begin_( begin
), step_( step
), count_( count
)
66 Lang::SingleListRange
< T
>::~SingleListRange( )
71 Lang::SingleListRange
< T
>::isNull( ) const
78 Lang::SingleListRange
< T
>::foldl( Kernel::EvalState
* evalState
, const RefCountPtr
< const Lang::Function
> & op
, const Kernel::VariableHandle
& nullResult
, const Ast::SourceLocation
& callLoc
) const
82 Kernel::ContRef cont
= evalState
->cont_
;
83 cont
->takeHandle( nullResult
,
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
);
96 Lang::SingleListRange
< T
>::foldr( Kernel::EvalState
* evalState
, const RefCountPtr
< const Lang::Function
> & op
, const Kernel::VariableHandle
& nullResult
, const Ast::SourceLocation
& callLoc
) const
100 Kernel::ContRef cont
= evalState
->cont_
;
101 cont
->takeHandle( nullResult
,
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
);
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
117 Kernel::ContRef cont
= evalState
->cont_
;
118 cont
->takeHandle( nullResult
,
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
);
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
135 Kernel::ContRef cont
= evalState
->cont_
;
136 cont
->takeHandle( nullResult
,
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
);
148 Lang::SingleListRange
< T
>::expand( std::vector
< elem_type
> * dst
) const
150 dst
->reserve( dst
->size( ) + count_
);
152 elem_type i
= begin_
;