#ifndef __X10_LANG_FINISHSTATE__STATEFULREDUCER_H
#define __X10_LANG_FINISHSTATE__STATEFULREDUCER_H

#include <x10rt.h>


#define X10_LANG_OBJECT_H_NODEPS
#include <x10/lang/Object.h>
#undef X10_LANG_OBJECT_H_NODEPS
#define X10_UTIL_INDEXEDMEMORYCHUNK_H_NODEPS
#include <x10/util/IndexedMemoryChunk.h>
#undef X10_UTIL_INDEXEDMEMORYCHUNK_H_NODEPS
#define X10_UTIL_INDEXEDMEMORYCHUNK_H_NODEPS
#include <x10/util/IndexedMemoryChunk.h>
#undef X10_UTIL_INDEXEDMEMORYCHUNK_H_NODEPS
namespace x10 { namespace lang { 
template<class TPMGL(T)> class Reducible;
} } 
namespace x10 { namespace util { 
template<class TPMGL(T)> class IndexedMemoryChunk;
} } 
namespace x10 { namespace lang { 
class Boolean;
} } 
namespace x10 { namespace lang { 
class Runtime;
} } 
namespace x10 { namespace lang { 
class Int;
} } 
namespace x10 { namespace lang { 
class Zero;
} } 
namespace x10 { namespace lang { 

template<class TPMGL(T)> class FinishState__StatefulReducer;
template <> class FinishState__StatefulReducer<void>;
template<class TPMGL(T)> class FinishState__StatefulReducer : public x10::lang::Object
  {
    public:
    RTT_H_DECLS_CLASS
    
    x10aux::ref<x10::lang::Reducible<TPMGL(T)> > FMGL(reducer);
    
    TPMGL(T) FMGL(result);
    
    x10::util::IndexedMemoryChunk<TPMGL(T) > FMGL(resultRail);
    
    x10::util::IndexedMemoryChunk<x10_boolean > FMGL(workerFlag);
    
    void _constructor(x10aux::ref<x10::lang::Reducible<TPMGL(T)> > r);
    
    static x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> > _make(
             x10aux::ref<x10::lang::Reducible<TPMGL(T)> > r);
    
    virtual void accept(TPMGL(T) t);
    virtual void accept(TPMGL(T) t, x10_int id);
    virtual void placeMerge();
    virtual TPMGL(T) result();
    virtual void reset();
    virtual x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >
      x10__lang__FinishState__StatefulReducer____x10__lang__FinishState__StatefulReducer__this(
      );
    void __fieldInitializers43524();
    
    // Serialization
    public: static const x10aux::serialization_id_t _serialization_id;
    
    public: virtual x10aux::serialization_id_t _get_serialization_id() {
         return _serialization_id;
    }
    
    public: virtual void _serialize_body(x10aux::serialization_buffer& buf);
    
    public: static x10aux::ref<x10::lang::Reference> _deserializer(x10aux::deserialization_buffer& buf);
    
    public: void _deserialize_body(x10aux::deserialization_buffer& buf);
    
};
template <> class FinishState__StatefulReducer<void> : public x10::lang::Object
{
    public:
    static x10aux::RuntimeType rtt;
    static const x10aux::RuntimeType* getRTT() { return & rtt; }
    
};

} } 
#endif // X10_LANG_FINISHSTATE__STATEFULREDUCER_H

namespace x10 { namespace lang { 
template<class TPMGL(T)> class FinishState__StatefulReducer;
} } 

#ifndef X10_LANG_FINISHSTATE__STATEFULREDUCER_H_NODEPS
#define X10_LANG_FINISHSTATE__STATEFULREDUCER_H_NODEPS
#include <x10/lang/Object.h>
#include <x10/lang/Reducible.h>
#include <x10/util/IndexedMemoryChunk.h>
#include <x10/lang/Boolean.h>
#include <x10/lang/Runtime.h>
#include <x10/lang/Int.h>
#include <x10/lang/Zero.h>
#ifndef X10_LANG_FINISHSTATE__STATEFULREDUCER_H_GENERICS
#define X10_LANG_FINISHSTATE__STATEFULREDUCER_H_GENERICS
#endif // X10_LANG_FINISHSTATE__STATEFULREDUCER_H_GENERICS
#ifndef X10_LANG_FINISHSTATE__STATEFULREDUCER_H_IMPLEMENTATION
#define X10_LANG_FINISHSTATE__STATEFULREDUCER_H_IMPLEMENTATION
#include <x10/lang/FinishState__StatefulReducer.h>


//#line 555 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10FieldDecl_c

//#line 556 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10FieldDecl_c

//#line 557 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10FieldDecl_c

//#line 558 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10FieldDecl_c

//#line 559 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10ConstructorDecl_c
template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_constructor(
                           x10aux::ref<x10::lang::Reducible<TPMGL(T)> > r) {
    
    //#line 559 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10ConstructorCall_c
    (this)->::x10::lang::Object::_constructor();
    
    //#line 559 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.AssignPropertyCall_c
    
    //#line 554 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10Call_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->x10::lang::FinishState__StatefulReducer<TPMGL(T)>::__fieldInitializers43524();
    
    //#line 560 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
      FMGL(reducer) = r;
    
    //#line 561 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10LocalDecl_c
    TPMGL(T) zero = x10::lang::Reducible<TPMGL(T)>::zero(x10aux::nullCheck(((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                                                             FMGL(reducer)));
    
    //#line 562 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
      FMGL(result) = zero;
    
    //#line 563 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
      FMGL(resultRail) = x10::util::IndexedMemoryChunk<void>::allocate<TPMGL(T) >(x10::lang::Runtime::
                                                                                    FMGL(MAX_THREADS), 8, false, false);
    
    //#line 564 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10LocalDecl_c
    x10_int i43781min43910 = ((x10_int)0);
    
    //#line 564 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10LocalDecl_c
    x10_int i43781max43911 = ((x10_int) (((((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                             FMGL(resultRail))->length()) - (((x10_int)1))));
    
    //#line 564 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": polyglot.ast.For_c
    {
        x10_int i43912;
        for (
             //#line 564 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10LocalDecl_c
             i43912 = i43781min43910; ((i43912) <= (i43781max43911));
             
             //#line 564 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10LocalAssign_c
             i43912 = ((x10_int) ((i43912) + (((x10_int)1)))))
        {
            
            //#line 564 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10LocalDecl_c
            x10_int i43913 = i43912;
            
            //#line 565 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10Call_c
            (((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
               FMGL(resultRail))->__set(i43913, zero);
        }
    }
    
}
template<class TPMGL(T)> x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> > x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_make(
                           x10aux::ref<x10::lang::Reducible<TPMGL(T)> > r)
{
    x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> > this_ = new (memset(x10aux::alloc<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >(), 0, sizeof(x10::lang::FinishState__StatefulReducer<TPMGL(T)>))) x10::lang::FinishState__StatefulReducer<TPMGL(T)>();
    this_->_constructor(r);
    return this_;
}



//#line 568 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10MethodDecl_c
template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::accept(
  TPMGL(T) t) {
    
    //#line 569 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
      FMGL(result) = x10::lang::Reducible<TPMGL(T)>::__apply(x10aux::nullCheck(((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                                                                 FMGL(reducer)), 
                       ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                         FMGL(result), t);
}

//#line 571 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10MethodDecl_c
template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::accept(
  TPMGL(T) t, x10_int id) {
    
    //#line 572 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10If_c
    if (((id) >= (((x10_int)0))) && ((id) < (x10::lang::Runtime::
                                               FMGL(MAX_THREADS))))
    {
        
        //#line 573 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10Call_c
        (((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
           FMGL(resultRail))->__set(id, x10::lang::Reducible<TPMGL(T)>::__apply(x10aux::nullCheck(((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                                                                                    FMGL(reducer)), 
                                          (((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                             FMGL(resultRail))->__apply(id),
                                          t));
        
        //#line 574 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10Call_c
        (((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
           FMGL(workerFlag))->__set(id, true);
    }
    
}

//#line 577 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10MethodDecl_c
template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::placeMerge(
  ) {
    
    //#line 578 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": polyglot.ast.For_c
    {
        x10_int i;
        for (
             //#line 578 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10LocalDecl_c
             i = ((x10_int)0); ((i) < (x10::lang::Runtime::
                                         FMGL(MAX_THREADS)));
             
             //#line 578 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10LocalAssign_c
             i = ((x10_int) ((i) + (((x10_int)1))))) {
            
            //#line 579 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10If_c
            if ((((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                   FMGL(workerFlag))->__apply(i)) {
                
                //#line 580 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
                ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                  FMGL(result) = x10::lang::Reducible<TPMGL(T)>::__apply(x10aux::nullCheck(((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                                                                             FMGL(reducer)), 
                                   ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                     FMGL(result), (((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                                      FMGL(resultRail))->__apply(i));
                
                //#line 581 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10Call_c
                (((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                   FMGL(resultRail))->__set(i, x10::lang::Reducible<TPMGL(T)>::zero(x10aux::nullCheck(((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                                                                                        FMGL(reducer))));
            }
            
        }
    }
    
}

//#line 585 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10MethodDecl_c
template<class TPMGL(T)> TPMGL(T) x10::lang::FinishState__StatefulReducer<TPMGL(T)>::result(
  ) {
    
    //#line 585 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10Return_c
    return ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
             FMGL(result);
    
}

//#line 586 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10MethodDecl_c
template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::reset(
  ) {
    
    //#line 587 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
      FMGL(result) = x10::lang::Reducible<TPMGL(T)>::zero(x10aux::nullCheck(((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
                                                                              FMGL(reducer)));
}

//#line 554 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10MethodDecl_c
template<class TPMGL(T)> x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >
  x10::lang::FinishState__StatefulReducer<TPMGL(T)>::x10__lang__FinishState__StatefulReducer____x10__lang__FinishState__StatefulReducer__this(
  ) {
    
    //#line 554 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10Return_c
    return ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this);
    
}

//#line 554 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": x10.ast.X10MethodDecl_c
template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::__fieldInitializers43524(
  ) {
    
    //#line 554 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
      FMGL(resultRail) = x10aux::zeroValue<x10::util::IndexedMemoryChunk<TPMGL(T) > >();
    
    //#line 554 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/lang/FinishState.x10": Eval of x10.ast.X10FieldAssign_c
    ((x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >)this)->
      FMGL(workerFlag) = x10::util::IndexedMemoryChunk<void>::allocate<x10_boolean >(x10::lang::Runtime::
                                                                                       FMGL(MAX_THREADS), 8, false, true);
}
template<class TPMGL(T)> const x10aux::serialization_id_t x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_serialization_id = 
    x10aux::DeserializationDispatcher::addDeserializer(x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_deserializer, x10aux::CLOSURE_KIND_NOT_ASYNC);

template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_serialize_body(x10aux::serialization_buffer& buf) {
    x10::lang::Object::_serialize_body(buf);
    buf.write(this->FMGL(reducer));
    buf.write(this->FMGL(result));
    buf.write(this->FMGL(resultRail));
    buf.write(this->FMGL(workerFlag));
    
}

template<class TPMGL(T)> x10aux::ref<x10::lang::Reference> x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_deserializer(x10aux::deserialization_buffer& buf) {
    x10aux::ref<x10::lang::FinishState__StatefulReducer<TPMGL(T)> > this_ = new (memset(x10aux::alloc<x10::lang::FinishState__StatefulReducer<TPMGL(T)> >(), 0, sizeof(x10::lang::FinishState__StatefulReducer<TPMGL(T)>))) x10::lang::FinishState__StatefulReducer<TPMGL(T)>();
    buf.record_reference(this_);
    this_->_deserialize_body(buf);
    return this_;
}

template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_deserialize_body(x10aux::deserialization_buffer& buf) {
    x10::lang::Object::_deserialize_body(buf);
    FMGL(reducer) = buf.read<x10aux::ref<x10::lang::Reducible<TPMGL(T)> > >();
    FMGL(result) = buf.read<TPMGL(T)>();
    FMGL(resultRail) = buf.read<x10::util::IndexedMemoryChunk<TPMGL(T) > >();
    FMGL(workerFlag) = buf.read<x10::util::IndexedMemoryChunk<x10_boolean > >();
}

template<class TPMGL(T)> x10aux::RuntimeType x10::lang::FinishState__StatefulReducer<TPMGL(T)>::rtt;
template<class TPMGL(T)> void x10::lang::FinishState__StatefulReducer<TPMGL(T)>::_initRTT() {
    const x10aux::RuntimeType *canonical = x10aux::getRTT<x10::lang::FinishState__StatefulReducer<void> >();
    if (rtt.initStageOne(canonical)) return;
    const x10aux::RuntimeType* parents[1] = { x10aux::getRTT<x10::lang::Object>()};
    const x10aux::RuntimeType* params[1] = { x10aux::getRTT<TPMGL(T)>()};
    x10aux::RuntimeType::Variance variances[1] = { x10aux::RuntimeType::invariant};
    const char *baseName = "x10.lang.FinishState.StatefulReducer";
    rtt.initStageTwo(baseName, x10aux::RuntimeType::class_kind, 1, parents, 1, params, variances);
}
#endif // X10_LANG_FINISHSTATE__STATEFULREDUCER_H_IMPLEMENTATION
#endif // __X10_LANG_FINISHSTATE__STATEFULREDUCER_H_NODEPS
