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