OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Indexer.h
Go to the documentation of this file.
1#ifndef OPAL_Indexer_HH
2#define OPAL_Indexer_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: Indexer.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.3 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: Indexer<T>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2002/01/17 22:18:36 $
17// $Author: jsberg $
18//
19// ------------------------------------------------------------------------
20
21#include <cerrno>
22#include <cmath>
23#include <iosfwd>
24#include <vector>
28
29namespace Expressions {
30
31 // Template class Indexer
32 // ----------------------------------------------------------------------
34 // This expression first evaluates the array operand expression and the
35 // scalar index expression. Then it applies the index to the array and
36 // returns the selected component.
37
38 template <class T>
39 class Indexer : public Scalar<T> {
40 public:
42 // Use array expression and index into the array.
44
46 virtual ~Indexer();
47
49 virtual Scalar<T>* clone() const;
50
52 virtual T evaluate() const;
53
55 virtual void print(std::ostream& str, int precedence = 99) const;
56
57 private:
58 // Not implemented.
60 void operator=(const Indexer&);
61
62 // The two operands.
65 };
66
67 // Implementation
68 // ----------------------------------------------------------------------
69
70 template <class T>
71 inline Indexer<T>::Indexer(const Indexer& rhs)
72 : Scalar<T>(rhs), lft(rhs.lft->clone()), rgt(rhs.rgt->clone()) {}
73
74 template <class T>
76 : lft(left), rgt(right) {}
77
78 template <class T>
80
81 template <class T>
82 inline Scalar<T>* Indexer<T>::clone() const {
83 return new Indexer<T>(*this);
84 }
85
86 template <class T>
87 inline T Indexer<T>::evaluate() const {
88 std::vector<T> op1 = lft->evaluate();
89 int op2 = int(std::round(rgt->evaluate()));
90
91 if (op2 <= 0 || static_cast<unsigned>(op2) > op1.size()) {
92 throw CLRangeError("Expressions::Indexer()", "Index out of range.");
93 }
94
95 return op1[op2 - 1];
96 }
97
98 template <class T>
99 inline void Indexer<T>::print(std::ostream& os, int /*precedence*/) const {
100 lft->print(os, 0);
101 os << '[';
102 rgt->print(os, 0);
103 os << ']';
104 }
105
106} // namespace Expressions
107
108#endif // OPAL_Indexer_HH
double T
Definition OPALTypes.h:8
Range error.
A scalar expression to retrieve an indexed component from an array.
Definition Indexer.h:39
virtual ~Indexer()
Definition Indexer.h:79
virtual Scalar< T > * clone() const
Make clone.
Definition Indexer.h:82
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition Indexer.h:99
void operator=(const Indexer &)
PtrToArray< T > lft
Definition Indexer.h:63
virtual T evaluate() const
Evaluate.
Definition Indexer.h:87
PtrToScalar< double > rgt
Definition Indexer.h:64
Indexer(const Indexer< T > &)
A pointer to an array expression.
A pointer to a scalar expression.
Representation objects and parsers for attribute expressions.