3131
3232#include " metaphysicl/dynamicsparsenumberbase_decl.h"
3333
34+ #include " metaphysicl/metaphysicl_algorithm.h"
3435#include " metaphysicl/metaphysicl_common.h"
3536#include " metaphysicl/metaphysicl_math.h"
3637
@@ -142,42 +143,15 @@ Indices&
142143DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::nude_indices()
143144{ return _indices; }
144145
145- template <typename C, typename Val>
146- METAPHYSICL_INLINE auto lower_bound_pointer (C & container, const Val & value)
147- {
148- // Adapting https://en.cppreference.com/w/cpp/algorithm/lower_bound.html implementation to use
149- // pointers for portability
150- typedef decltype (container.data ()) Ptr;
151- Ptr lower_bound_ptr = container.data (), work_ptr;
152- std::ptrdiff_t num_remaining_elements = container.size (), step;
153- while (num_remaining_elements > 0 )
154- {
155- work_ptr = lower_bound_ptr;
156- step = num_remaining_elements / 2 ;
157- work_ptr += step;
158- if (*work_ptr < value)
159- {
160- lower_bound_ptr = ++work_ptr;
161- // We can discard both the current element and the other half of the range
162- num_remaining_elements -= step + 1 ;
163- }
164- else
165- // Discard the other half of the range
166- num_remaining_elements = step;
167- }
168-
169- return lower_bound_ptr;
170- }
171-
172146template <typename Data, typename Indices, template <class ...> class SubType , class ... SubTypeArgs>
173147METAPHYSICL_INLINE
174148std::size_t
175149DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::runtime_index_query(index_value_type i) const
176150{
177- auto ptr = MetaPhysicL::lower_bound_pointer (_indices, i);
178- if (ptr == ( _indices.data () + _indices. size ()) || *ptr != i)
151+ auto it = detail::lower_bound (_indices. begin (), _indices. end () , i);
152+ if (it == _indices.end () || *it != i)
179153 return MetaPhysicL::numeric_limits<std::size_t >::max ();
180- std::size_t offset = ptr - _indices.data ();
154+ std::size_t offset = it - _indices.begin ();
181155 metaphysicl_assert_equal_to (_indices[offset], i);
182156 return offset;
183157}
@@ -187,8 +161,7 @@ METAPHYSICL_INLINE
187161std::size_t
188162DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::runtime_index_of(index_value_type i) const
189163{
190- auto it =
191- std::lower_bound (_indices.begin (), _indices.end (), i);
164+ auto it = detail::lower_bound (_indices.begin (), _indices.end (), i);
192165 metaphysicl_assert (it != _indices.end ());
193166 std::size_t offset = it - _indices.begin ();
194167 metaphysicl_assert_equal_to (_indices[offset], i);
@@ -245,8 +218,7 @@ METAPHYSICL_INLINE
245218typename DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::value_type&
246219DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::insert(unsigned int i)
247220{
248- auto upper_it =
249- std::lower_bound (_indices.begin (), _indices.end (), i);
221+ auto upper_it = detail::lower_bound (_indices.begin (), _indices.end (), i);
250222 std::size_t offset = upper_it - _indices.begin ();
251223
252224 // If we don't have entry i, insert it. Yes this is O(N).
@@ -255,8 +227,8 @@ DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::insert(unsigned
255227 {
256228 std::size_t old_size = this ->size ();
257229 this ->resize (old_size+1 );
258- std ::copy_backward (_indices.begin ()+offset, _indices.begin ()+old_size, _indices.end ());
259- std ::copy_backward (_data.begin ()+offset, _data.begin ()+old_size, _data.end ());
230+ detail ::copy_backward (_indices.begin ()+offset, _indices.begin ()+old_size, _indices.end ());
231+ detail ::copy_backward (_data.begin ()+offset, _data.begin ()+old_size, _data.end ());
260232 _indices[offset] = i;
261233 _data[offset] = 0 ;
262234 }
@@ -553,10 +525,10 @@ void
553525DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::sparsity_trim (const value_type tolerance)
554526{
555527 metaphysicl_assert
556- (std ::adjacent_find (_indices.begin (), _indices.end ()) ==
528+ (detail ::adjacent_find (_indices.begin (), _indices.end ()) ==
557529 _indices.end ());
558530#ifdef METAPHYSICL_HAVE_CXX11
559- metaphysicl_assert (std ::is_sorted (_indices.begin (), _indices.end ()));
531+ metaphysicl_assert (detail ::is_sorted (_indices.begin (), _indices.end ()));
560532#endif
561533 metaphysicl_assert (tolerance >= 0 );
562534
@@ -567,7 +539,7 @@ DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::sparsity_trim (
567539 auto index_it = _indices.begin ();
568540 auto data_it = _data.begin ();
569541 for (; index_it != _indices.end (); ++index_it, ++data_it)
570- if (std ::abs (*data_it) > tolerance)
542+ if (MetaPhysicL::math ::abs (*data_it) > tolerance)
571543 ++used_indices;
572544 }
573545#endif
@@ -584,7 +556,7 @@ DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::sparsity_trim (
584556
585557 for (auto i_it = _indices.begin ();
586558 i_it != _indices.end (); ++i_it, ++d_it)
587- if (std ::abs (*d_it) > tolerance)
559+ if (MetaPhysicL::math ::abs (*d_it) > tolerance)
588560 {
589561 *mi_it = *i_it;
590562 *md_it = *d_it;
@@ -790,18 +762,18 @@ if_else(
790762 const DynamicSparseNumberBase<Data2, Indices2, SubType, SubTypeArgs2...> & if_false)
791763{
792764 metaphysicl_assert
793- (std ::adjacent_find (condition.nude_indices ().begin (), condition.nude_indices ().end ()) ==
765+ (detail ::adjacent_find (condition.nude_indices ().begin (), condition.nude_indices ().end ()) ==
794766 condition.nude_indices ().end ());
795767 metaphysicl_assert
796- (std ::adjacent_find (if_true.nude_indices ().begin (), if_true.nude_indices ().end ()) ==
768+ (detail ::adjacent_find (if_true.nude_indices ().begin (), if_true.nude_indices ().end ()) ==
797769 if_true.nude_indices ().end ());
798770 metaphysicl_assert
799- (std ::adjacent_find (if_false.nude_indices ().begin (), if_false.nude_indices ().end ()) ==
771+ (detail ::adjacent_find (if_false.nude_indices ().begin (), if_false.nude_indices ().end ()) ==
800772 if_false.nude_indices ().end ());
801773#ifdef METAPHYSICL_HAVE_CXX11
802- metaphysicl_assert (std ::is_sorted (condition.nude_indices ().begin (), condition.nude_indices ().end ()));
803- metaphysicl_assert (std ::is_sorted (if_true.nude_indices ().begin (), if_true.nude_indices ().end ()));
804- metaphysicl_assert (std ::is_sorted (if_false.nude_indices ().begin (), if_false.nude_indices ().end ()));
774+ metaphysicl_assert (detail ::is_sorted (condition.nude_indices ().begin (), condition.nude_indices ().end ()));
775+ metaphysicl_assert (detail ::is_sorted (if_true.nude_indices ().begin (), if_true.nude_indices ().end ()));
776+ metaphysicl_assert (detail ::is_sorted (if_false.nude_indices ().begin (), if_false.nude_indices ().end ()));
805777#endif
806778
807779 typedef
@@ -947,10 +919,10 @@ if_else(
947919 }
948920
949921 metaphysicl_assert
950- (std ::adjacent_find (returnval.nude_indices ().begin (), returnval.nude_indices ().end ()) ==
922+ (detail ::adjacent_find (returnval.nude_indices ().begin (), returnval.nude_indices ().end ()) ==
951923 returnval.nude_indices ().end ());
952924#ifdef METAPHYSICL_HAVE_CXX11
953- metaphysicl_assert (std ::is_sorted (returnval.nude_indices ().begin (), returnval.nude_indices ().end ()));
925+ metaphysicl_assert (detail ::is_sorted (returnval.nude_indices ().begin (), returnval.nude_indices ().end ()));
954926#endif
955927
956928 return returnval;
0 commit comments