27 #ifndef MLN_UTIL_ARRAY_HH
28 # define MLN_UTIL_ARRAY_HH
41 # include <mln/core/concept/function.hh>
42 # include <mln/core/concept/proxy.hh>
43 # include <mln/core/concept/iterator.hh>
45 # include <mln/fun/internal/selector.hh>
67 template <
typename T1,
typename T2>
69 from_to_(
const util::array<T1>& from, util::array<T2>&
to);
71 template <
typename T1,
typename T2>
73 from_to_(
const fun::i2v::array<T1>& from, util::array<T2>&
to);
85 template <
typename T>
class array_fwd_iter;
86 template <
typename T>
class array_bkd_iter;
100 :
public fun::internal::selector_from_result_<T, array<T> >::ret
113 typedef typename std::vector<T>::const_reference ro_result;
114 typedef typename std::vector<T>::reference mutable_result;
141 array(
unsigned n,
const T& value);
151 void resize(
unsigned n,
const T& value);
158 template <
typename U>
168 unsigned size()
const;
191 ro_result
last()
const;
194 mutable_result
last();
201 void fill(
const T& value);
207 std::vector<T>& hook_std_vector_();
218 template <
typename T>
219 std::ostream& operator<<(std::ostream& ostr,
223 template <
typename T>
230 template <
typename T>
231 class array_fwd_iter :
public Proxy< array_fwd_iter<T> >,
232 public mln::internal::proxy_impl< const T&,
236 typedef typename array<T>::ro_result subj_t;
248 void change_target(
const array<T>& a);
257 bool is_valid()
const;
269 unsigned index_()
const;
281 template <
typename T>
282 class array_bkd_iter :
public Proxy< array_bkd_iter<T> >,
283 public mln::internal::proxy_impl< const T&,
287 typedef typename array<T>::ro_result subj_t;
295 array_bkd_iter(
const array<T>& a);
299 void change_target(
const array<T>& a);
308 bool is_valid()
const;
320 unsigned index_()
const;
333 template <
typename T,
typename E>
334 struct subject_impl<const util::array<T>&, E>
336 typedef typename util::array<T>::ro_result ro_result;
338 unsigned nelements()
const;
339 unsigned size()
const;
340 bool is_empty()
const;
341 ro_result operator()(
unsigned i)
const;
342 ro_result operator[](
unsigned i)
const;
343 const std::vector<T>& std_vector()
const;
346 const E& exact_()
const;
350 template <
typename T,
typename E>
351 struct subject_impl<util::array<T>&, E>
352 : subject_impl<const util::array<T>&, E>
354 typedef typename util::array<T>::mutable_result mutable_result;
356 void reserve(
unsigned n);
358 void resize(
unsigned n,
const T& value);
360 util::array<T>& append(
const T& elt);
362 template <
typename U>
363 util::array<T>& append(
const util::array<U>& other);
365 mutable_result operator()(
unsigned i);
366 mutable_result operator[](
unsigned i);
370 void fill(
const T& value);
372 std::vector<T>& hook_std_vector_();
382 # ifndef MLN_INCLUDE_ONLY
393 template <
typename T1,
typename T2>
395 from_to_(
const util::array<T1>& from, util::array<T2>&
to)
397 to.resize(from.nelements());
399 for (
unsigned i = 0; i < from.nelements(); ++i)
400 to[i] = convert::to<T2>(from[i]);
403 template <
typename T1,
typename T2>
405 from_to_(
const fun::i2v::array<T1>& from, util::array<T2>& to)
407 to.resize(from.size());
409 for (
unsigned i = 0; i < from.size(); ++i)
410 to[i] = convert::to<T2>(from(i));
425 template <
typename T>
431 template <
typename T>
438 template <
typename T>
445 template <
typename T>
453 template <
typename T>
461 template <
typename T>
469 template <
typename T>
478 template <
typename T>
479 template <
typename U>
492 template <
typename T>
498 mln_postcondition(is_empty());
501 template <
typename T>
506 std::fill(v_.begin(), v_.end(), value);
509 template <
typename T>
517 template <
typename T>
525 template <
typename T>
527 typename array<T>::ro_result
533 template <
typename T>
535 typename array<T>::mutable_result
541 template <
typename T>
543 typename array<T>::ro_result
546 mln_precondition(i < nelements());
550 template <
typename T>
552 typename array<T>::mutable_result
555 mln_precondition(i < nelements());
559 template <
typename T>
561 typename array<T>::ro_result
564 return v_[nelements() - 1];
567 template <
typename T>
569 typename array<T>::mutable_result
572 return v_[nelements() - 1];
575 template <
typename T>
580 return nelements() == 0;
583 template <
typename T>
585 const std::vector<T>&
591 template <
typename T>
599 template <
typename T>
604 return sizeof(*this) + nelements() *
sizeof(T);
612 template <
typename T>
614 array_fwd_iter<T>::array_fwd_iter()
619 template <
typename T>
621 array_fwd_iter<T>::array_fwd_iter(
const array<T>& a)
626 template <
typename T>
629 array_fwd_iter<T>::change_target(
const array<T>& a)
635 template <
typename T>
638 array_fwd_iter<T>::start()
640 mln_precondition(a_ != 0);
644 template <
typename T>
647 array_fwd_iter<T>::next()
649 mln_precondition(is_valid());
653 template <
typename T>
656 array_fwd_iter<T>::is_valid()
const
658 return a_ != 0 && i_ != a_->nelements();
661 template <
typename T>
664 array_fwd_iter<T>::invalidate()
667 i_ = a_->nelements();
668 mln_postcondition(! is_valid());
671 template <
typename T>
674 array_fwd_iter<T>::element()
const
676 mln_precondition(is_valid());
677 return a_->operator[](i_);
680 template <
typename T>
682 typename array_fwd_iter<T>::subj_t
683 array_fwd_iter<T>::subj_()
685 mln_precondition(is_valid());
686 return a_->operator[](i_);
689 template <
typename T>
692 array_fwd_iter<T>::index_()
const
702 template <
typename T>
704 array_bkd_iter<T>::array_bkd_iter()
709 template <
typename T>
711 array_bkd_iter<T>::array_bkd_iter(
const array<T>& a)
716 template <
typename T>
719 array_bkd_iter<T>::change_target(
const array<T>& a)
725 template <
typename T>
728 array_bkd_iter<T>::start()
730 mln_precondition(a_ != 0);
731 if (! a_->is_empty())
732 i_ = a_->nelements() - 1;
735 template <
typename T>
738 array_bkd_iter<T>::next()
740 mln_precondition(is_valid());
747 template <
typename T>
750 array_bkd_iter<T>::is_valid()
const
752 return a_ != 0 && i_ != a_->nelements();
755 template <
typename T>
758 array_bkd_iter<T>::invalidate()
761 i_ = a_->nelements();
762 mln_postcondition(! is_valid());
765 template <
typename T>
768 array_bkd_iter<T>::element()
const
770 mln_precondition(is_valid());
771 return a_->operator[](i_);
774 template <
typename T>
776 typename array_bkd_iter<T>::subj_t
777 array_bkd_iter<T>::subj_()
779 mln_precondition(is_valid());
780 return a_->operator[](i_);
783 template <
typename T>
786 array_bkd_iter<T>::index_()
const
795 template <
typename T>
796 std::ostream& operator<<(std::ostream& ostr,
801 for (
unsigned i = 0; i < n; ++i)
814 template <
typename T>
827 template <
typename T,
typename E>
830 subject_impl<util::array<T>&, E>::reserve(
unsigned n)
832 exact_().get_subject().reserve(n);
835 template <
typename T,
typename E>
838 subject_impl<util::array<T>&, E>::resize(
unsigned n)
840 exact_().get_subject().resize(n);
843 template <
typename T,
typename E>
846 subject_impl<util::array<T>&, E>::resize(
unsigned n,
const T& value)
848 exact_().get_subject().resize(n, value);
851 template <
typename T,
typename E>
854 subject_impl<util::array<T>&, E>::append(
const T& elt)
856 return exact_().get_subject().append(elt);
859 template <
typename T,
typename E>
860 template <
typename U>
863 subject_impl<util::array<T>&, E>::append(
const util::array<U>& other)
865 return exact_().get_subject().append(other);
868 template <
typename T,
typename E>
870 typename subject_impl<util::array<T>&, E>::mutable_result
871 subject_impl<util::array<T>&, E>::operator()(
unsigned i)
873 return exact_().get_subject()(i);
876 template <
typename T,
typename E>
878 typename subject_impl<util::array<T>&, E>::mutable_result
879 subject_impl<util::array<T>&, E>::operator[](
unsigned i)
881 return exact_().get_subject()[i];
884 template <
typename T,
typename E>
887 subject_impl<util::array<T>&, E>::clear()
889 exact_().get_subject().clear();
892 template <
typename T,
typename E>
895 subject_impl<util::array<T>&, E>::fill(
const T& value)
897 exact_().get_subject().fill(value);
900 template <
typename T,
typename E>
903 subject_impl<util::array<T>&, E>::hook_std_vector_()
905 return exact_().get_subject().hook_std_vector_();
908 template <
typename T,
typename E>
911 subject_impl<util::array<T>&, E >::exact_()
913 return internal::force_exact<E>(*this);
917 template <
typename T,
typename E>
920 subject_impl<const util::array<T>&, E>::size()
const
922 return exact_().get_subject().size();
925 template <
typename T,
typename E>
928 subject_impl<const util::array<T>&, E>::nelements()
const
930 return exact_().get_subject().nelements();
933 template <
typename T,
typename E>
936 subject_impl<const util::array<T>&, E>::is_empty()
const
938 return exact_().get_subject().is_empty();
941 template <
typename T,
typename E>
943 typename subject_impl<const util::array<T>&, E>::ro_result
944 subject_impl<const util::array<T>&, E>::operator()(
unsigned i)
const
946 return exact_().get_subject()(i);
949 template <
typename T,
typename E>
951 typename subject_impl<const util::array<T>&, E>::ro_result
952 subject_impl<const util::array<T>&, E>::operator[](
unsigned i)
const
954 return exact_().get_subject()[i];
957 template <
typename T,
typename E>
959 const std::vector<T>&
960 subject_impl<const util::array<T>&, E>::std_vector()
const
962 return exact_().get_subject().std_vector();
965 template <
typename T,
typename E>
968 subject_impl<const util::array<T>&, E >::exact_()
const
970 return internal::force_exact<const E>(*this);
976 # endif // ! MLN_INCLUDE_ONLY
981 #endif // ! MLN_UTIL_ARRAY_HH