void SparseVector<T_Element,T_Alloc>::insert_element(element_type ele) { assert_space(1); assert_is_sorted(); // Find the insertion point if( nz() ) { typedef SparseVectorUtilityPack::SpVecIndexLookup<T_Element> SpVecIndexLookup; typedef typename SpVecIndexLookup::poss_type poss_type; index_lookup_.validate_state(); poss_type poss = ( nz() ? index_lookup_.find_poss(ele.index(), SpVecIndexLookup::LOWER_ELE) : poss_type(0,SpVecIndexLookup::BEFORE_ELE) ); // Make sure this element does not already exist! #ifdef TEUCHOS_DEBUG TEUCHOS_TEST_FOR_EXCEPTION( nz() && poss.rel == SpVecIndexLookup::EQUAL_TO_ELE, std::length_error ,"SparseVector<...>::insert_element(...) : Error, this index" " all ready exists!" ); #endif const size_type insert_poss = (poss.rel == SpVecIndexLookup::BEFORE_ELE ? poss.poss : poss.poss+1); // Copy elements out of the way to make room for inserted element std::copy_backward( // This assumes element_type supports assignment! index_lookup_.ele() + insert_poss, index_lookup_.ele() + index_lookup_.nz() , index_lookup_.ele() + index_lookup_.nz() + 1 ); index_lookup_.ele()[insert_poss] = ele; index_lookup_.incr_nz(); } else { // The first element we are adding! index_lookup_.ele()[0] = ele; index_lookup_.incr_nz(); } }