1 | /* |
---|
2 | * $Id: vecmax.cc,v 1.4 2005/06/02 18:56:52 julianc Exp $ |
---|
3 | * |
---|
4 | * Copyright (C) 1997 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
5 | * All rights reserved. Please see <blitz/blitz.h> for terms and |
---|
6 | * conditions of use. |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef BZ_VECMAX_CC |
---|
11 | #define BZ_VECMAX_CC |
---|
12 | |
---|
13 | #ifndef BZ_VECGLOBS_H |
---|
14 | #error <blitz/vecmax.cc> must be included via <blitz/vecglobs.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | BZ_NAMESPACE(blitz) |
---|
18 | |
---|
19 | template<typename P_expr> |
---|
20 | inline |
---|
21 | Extremum<_bz_typename P_expr::T_numtype, int> _bz_vec_max(P_expr vector) |
---|
22 | { |
---|
23 | typedef _bz_typename P_expr::T_numtype T_numtype; |
---|
24 | |
---|
25 | T_numtype maxValue = vector(0); |
---|
26 | int maxIndex = 0; |
---|
27 | int length = vector._bz_suggestLength(); |
---|
28 | |
---|
29 | if (vector._bz_hasFastAccess()) |
---|
30 | { |
---|
31 | for (int i=1; i < length; ++i) |
---|
32 | { |
---|
33 | T_numtype value = vector._bz_fastAccess(i); |
---|
34 | if (value > maxValue) |
---|
35 | { |
---|
36 | maxValue = value; |
---|
37 | maxIndex = i; |
---|
38 | } |
---|
39 | } |
---|
40 | } |
---|
41 | else { |
---|
42 | for (int i=1; i < length; ++i) |
---|
43 | { |
---|
44 | T_numtype value = vector(i); |
---|
45 | if (value > maxValue) |
---|
46 | { |
---|
47 | maxValue = value; |
---|
48 | maxIndex = i; |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | return Extremum<T_numtype, int>(maxValue, maxIndex); |
---|
54 | } |
---|
55 | |
---|
56 | // max(vector) |
---|
57 | template<typename P_numtype> |
---|
58 | inline |
---|
59 | Extremum<P_numtype, int> (max)(const Vector<P_numtype>& x) |
---|
60 | { |
---|
61 | return _bz_vec_max(x._bz_asVecExpr()); |
---|
62 | } |
---|
63 | |
---|
64 | // max(expr) |
---|
65 | template<typename P_expr> |
---|
66 | inline |
---|
67 | Extremum<_bz_typename P_expr::T_numtype,int> (max)(_bz_VecExpr<P_expr> x) |
---|
68 | { |
---|
69 | return _bz_vec_max(x); |
---|
70 | } |
---|
71 | |
---|
72 | // max(vecpick) |
---|
73 | template<typename P_numtype> |
---|
74 | inline |
---|
75 | Extremum<P_numtype, int> (max)(const VectorPick<P_numtype>& x) |
---|
76 | { |
---|
77 | return _bz_vec_max(x._bz_asVecExpr()); |
---|
78 | } |
---|
79 | |
---|
80 | // max(TinyVector) |
---|
81 | template<typename P_numtype, int N_length> |
---|
82 | inline |
---|
83 | Extremum<P_numtype, int> (max)(const TinyVector<P_numtype, N_length>& x) |
---|
84 | { |
---|
85 | return _bz_vec_max(x._bz_asVecExpr()); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | // maxIndex(vector) |
---|
90 | template<typename P_numtype> |
---|
91 | inline |
---|
92 | int maxIndex(const Vector<P_numtype>& x) |
---|
93 | { |
---|
94 | return _bz_vec_max(x).index(); |
---|
95 | } |
---|
96 | |
---|
97 | // maxIndex(expr) |
---|
98 | template<typename P_expr> |
---|
99 | inline |
---|
100 | int maxIndex(_bz_VecExpr<P_expr> x) |
---|
101 | { |
---|
102 | return _bz_vec_max(x._bz_asVecExpr()).index(); |
---|
103 | } |
---|
104 | |
---|
105 | // maxIndex(vecpick) |
---|
106 | template<typename P_numtype> |
---|
107 | int maxIndex(const VectorPick<P_numtype>& x) |
---|
108 | { |
---|
109 | return _bz_vec_max(x._bz_asVecExpr()).index(); |
---|
110 | } |
---|
111 | |
---|
112 | // maxIndex(TinyVector) |
---|
113 | template<typename P_numtype, int N_length> |
---|
114 | int maxIndex(const TinyVector<P_numtype, N_length>& x) |
---|
115 | { |
---|
116 | return _bz_vec_max(x._bz_asVecExpr()).index(); |
---|
117 | } |
---|
118 | |
---|
119 | // maxValue(vector) |
---|
120 | template<typename P_numtype> |
---|
121 | inline |
---|
122 | int maxValue(const Vector<P_numtype>& x) |
---|
123 | { |
---|
124 | return _bz_vec_max(x._bz_asVecExpr()).value(); |
---|
125 | } |
---|
126 | |
---|
127 | // maxValue(expr) |
---|
128 | template<typename P_expr> |
---|
129 | inline |
---|
130 | int maxValue(_bz_VecExpr<P_expr> x) |
---|
131 | { |
---|
132 | return _bz_vec_max(x).value(); |
---|
133 | } |
---|
134 | |
---|
135 | // maxValue(vecpick) |
---|
136 | template<typename P_numtype> |
---|
137 | int maxValue(const VectorPick<P_numtype>& x) |
---|
138 | { |
---|
139 | return _bz_vec_max(x._bz_asVecExpr()).value(); |
---|
140 | } |
---|
141 | |
---|
142 | // maxValue(TinyVector) |
---|
143 | template<typename P_numtype, int N_length> |
---|
144 | int maxValue(const TinyVector<P_numtype, N_length>& x) |
---|
145 | { |
---|
146 | return _bz_vec_max(x._bz_asVecExpr()).value(); |
---|
147 | } |
---|
148 | |
---|
149 | BZ_NAMESPACE_END |
---|
150 | |
---|
151 | #endif // BZ_VECMAX_CC |
---|
152 | |
---|