AList.h 3.05 KB
Newer Older
gsell's avatar
gsell committed
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
#ifndef OPAL_AList_HH
#define OPAL_AList_HH

// ------------------------------------------------------------------------
// $RCSfile: AList.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.2 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Template class: AList<T>
//
// ------------------------------------------------------------------------
//
// $Date: 2002/01/17 22:18:36 $
// $Author: jsberg $
//
// ------------------------------------------------------------------------

#include "AbstractObjects/Expressions.h"
#include "Expressions/SConstant.h"
#include <iostream>
#include <vector>


namespace Expressions {

    // Template Class AList
    // ----------------------------------------------------------------------
    /// An array expression defined by a list of scalar expressions.
    //  Each scalar expression is evaluated separately, and its result is
    //  appended to the result array.

    template <class T>
36
    class AList: public OArray<T> {
gsell's avatar
gsell committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    public:

        /// Default constructor.
        //  Construct empty array.
        AList();

        /// Constructor.
        //  Use an array of scalar expressions.
        explicit AList(const ArrayOfPtrs<T> &);

        AList(const AList &);
        virtual ~AList();

        /// Make clone.
52
        virtual OArray<T> *clone() const;
gsell's avatar
gsell committed
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

        /// Evaluate.
        virtual std::vector<T> evaluate() const;

        /// Print array expression.
        virtual void print(std::ostream &, int precedence = 99) const;

    protected:

        /// The vector of expressions.
        ArrayOfPtrs<T> itsValue;

    private:

        // Not implemented.
        void operator=(const AList &);
    };


    // Implementation
    // ------------------------------------------------------------------------

    template <class T>
    AList<T>::AList():
        itsValue()
    {}


    template <class T>
    AList<T>::AList(const AList<T> &rhs):
83
        OArray<T>(rhs),
gsell's avatar
gsell committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        itsValue(rhs.itsValue)
    {}


    template <class T>
    AList<T>::AList(const ArrayOfPtrs<T> &value):
        itsValue(value)
    {}


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


    template <class T>
100
    OArray<T> *AList<T>::clone() const {
gsell's avatar
gsell committed
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
        return new AList<T>(*this);
    }


    template <class T>
    std::vector<T> AList<T>::evaluate() const {
        std::vector<T> result(itsValue.size(), T(0));

        for(typename ArrayOfPtrs<T>::size_type i = 0; i < itsValue.size(); ++i) {
            result[i] = itsValue[i]->evaluate();
        }

        return result;
    }


    template <class T>
    void AList<T>::print(std::ostream &os, int precedence) const {
        os << '{';
        typename ArrayOfPtrs<T>::const_iterator i = itsValue.begin();

        while(i != itsValue.end()) {
            (*i)->print(os, 0);
            if(++i == itsValue.end()) break;
            os << ',';
        }

        os << '}';
    }

}

#endif // OPAL_AList_HH