42 #ifndef SP_VEC_INDEX_LOOKUP_CLASS_DEF_H
43 #define SP_VEC_INDEX_LOOKUP_CLASS_DEF_H
45 #include "AbstractLinAlgPack_SpVecIndexLookupClassDecl.hpp"
46 #include "AbstractLinAlgPack_compare_element_indexes.hpp"
51 template<
class T_Element>
60 if(index == index_cached_)
61 return poss_type(adjust_cached_poss(uplow),ele_rel_cached_);
62 if(index == index_cached_ + 1 && ele_rel_cached_ == AFTER_ELE
63 && uplow == LOWER_ELE)
70 if( poss_cached_ == nz_ - 1 )
71 return poss_type( poss_cached_ , AFTER_ELE );
74 if( ele_[poss_cached_+1].index() == index )
75 return poss_type( poss_cached_+1 , EQUAL_TO_ELE );
78 if( ele_[poss_cached_+1].index() > index )
79 return poss_type( poss_cached_+1 , BEFORE_ELE );
81 if(index == index_cached_ - 1 && ele_rel_cached_ == BEFORE_ELE
82 && uplow == UPPER_ELE)
89 if( poss_cached_ == 0 )
90 return poss_type( poss_cached_ , BEFORE_ELE );
93 if( ele_[poss_cached_+1].index() == index )
94 return poss_type( poss_cached_-1 , EQUAL_TO_ELE );
97 return poss_type( poss_cached_ - 1, AFTER_ELE);
102 poss_type poss = binary_ele_search(index,uplow);
105 if(poss.poss != 0 && poss.poss != nz() - 1) {
106 index_cached_ = index;
107 poss_cached_ = poss.poss;
108 ele_rel_cached_ = poss.rel;
114 template<
class T_Element>
115 AbstractLinAlgPack::size_type
119 typedef T_Element* itr_t;
121 const std::pair<itr_t,itr_t> p = std::equal_range( ele(), ele() + nz()
124 if( p.second - p.first == 1 )
125 return p.first - ele();
130 const itr_t itr = std::find_if( ele(), ele() + nz()
136 template<
class T_Element>
141 "SpVecIndexLookup<T_Element>::validate_state(): Error, ele()->index() + offset() < 1");
144 template<
class T_Element>
145 AbstractLinAlgPack::size_type
147 UpperLower uplow)
const
149 if(ele_rel_cached_ == EQUAL_TO_ELE)
return poss_cached_;
152 switch(ele_rel_cached_) {
156 return poss_cached_ + 1;
160 switch(ele_rel_cached_) {
162 return poss_cached_ - 1;
171 template<
class T_Element>
174 index_type index, UpperLower uplow)
const
177 poss_type poss_returned;
179 size_type lower_poss = 0, upper_poss = nz_ - 1;
183 typename T_Element::index_type lower_index = ele()[lower_poss].index() + offset();
184 if(index <= lower_index) {
185 if(index == lower_index) poss_returned.rel = EQUAL_TO_ELE;
186 else poss_returned.rel = BEFORE_ELE;
187 poss_returned.poss = lower_poss;
188 return poss_returned;
191 typename T_Element::index_type upper_index = ele()[upper_poss].index() + offset();
192 if(index >= upper_index) {
193 if(index == upper_index) poss_returned.rel = EQUAL_TO_ELE;
194 else poss_returned.rel = AFTER_ELE;
195 poss_returned.poss = upper_poss;
196 return poss_returned;
202 if(upper_poss == lower_poss + 1) {
204 if(uplow == LOWER_ELE) {
205 poss_returned.rel = BEFORE_ELE;
206 poss_returned.poss = upper_poss;
207 return poss_returned;
210 poss_returned.rel = AFTER_ELE;
211 poss_returned.poss = lower_poss;
212 return poss_returned;
217 size_type mid_poss = (upper_poss - lower_poss) / 2 + lower_poss;
218 typename T_Element::index_type mid_index = ele()[mid_poss].index() + offset();
220 if(mid_index == index) {
221 poss_returned.rel = EQUAL_TO_ELE;
222 poss_returned.poss = mid_poss;
223 return poss_returned;
227 if(index < mid_index) {
228 upper_poss = mid_poss;
229 upper_index = mid_index;
233 lower_poss = mid_poss;
234 lower_index = mid_index;
239 #endif // SP_VEC_INDEX_LOOKUP_CLASS_DEF_H
poss_type find_poss(index_type index, UpperLower uplow) const
Lookup an element and cache the result if a binary search was performed.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
element_type::index_type index_type
ele1.index() < ele2.index()
Struct with members: size_type poss; ElementRelation rel;.
Sparse Vector Index Lookup and Caching class.
size_type find_element(index_type index, bool is_sorted) const
Lookup an element.
void validate_state() const
Called by client to ensure that the internal state is valid.