OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
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
24#include <cerrno>
25#include <cmath>
26#include <iosfwd>
27#include <vector>
28
29
30namespace Expressions {
31
32 // Template class Indexer
33 // ----------------------------------------------------------------------
35 // This expression first evaluates the array operand expression and the
36 // scalar index expression. Then it applies the index to the array and
37 // returns the selected component.
38
39 template <class T>
40 class Indexer: public Scalar<T> {
41
42 public:
43
45 // Use array expression and index into the array.
47
48 Indexer(const Indexer<T> &);
49 virtual ~Indexer();
50
52 virtual Scalar<T> *clone() const;
53
55 virtual T evaluate() const;
56
58 virtual void print(std::ostream &str, int precedence = 99) const;
59
60 private:
61
62 // Not implemented.
64 void operator=(const Indexer &);
65
66 // The two operands.
69 };
70
71
72 // Implementation
73 // ----------------------------------------------------------------------
74
75 template <class T> inline
77 Scalar<T>(rhs),
78 lft(rhs.lft->clone()), rgt(rhs.rgt->clone())
79 {}
80
81
82 template <class T> inline
84 lft(left), rgt(right)
85 {}
86
87
88 template <class T> inline
90 {}
91
92
93 template <class T> inline
95 return new Indexer<T>(*this);
96 }
97
98
99 template <class T> inline
101 std::vector<T> op1 = lft->evaluate();
102 int op2 = int(std::round(rgt->evaluate()));
103
104 if(op2 <= 0 || static_cast<unsigned>(op2) > op1.size()) {
105 throw CLRangeError("Expressions::Indexer()", "Index out of range.");
106 }
107
108 return op1[op2-1];
109 }
110
111
112 template <class T> inline
113 void Indexer<T>::print(std::ostream &os, int /*precedence*/) const {
114 lft->print(os, 0);
115 os << '[';
116 rgt->print(os, 0);
117 os << ']';
118 }
119
120}
121
122#endif // OPAL_Indexer_HH
Representation objects and parsers for attribute expressions.
Definition: Expressions.h:64
A scalar expression.
Definition: Expressions.h:71
A pointer to an array expression.
Definition: Expressions.h:180
Range error.
Definition: CLRangeError.h:33
A scalar expression to retrieve an indexed component from an array.
Definition: Indexer.h:40
virtual ~Indexer()
Definition: Indexer.h:89
virtual Scalar< T > * clone() const
Make clone.
Definition: Indexer.h:94
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition: Indexer.h:113
void operator=(const Indexer &)
PtrToArray< T > lft
Definition: Indexer.h:67
virtual T evaluate() const
Evaluate.
Definition: Indexer.h:100
PtrToScalar< double > rgt
Definition: Indexer.h:68