KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_int_vector.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  | |
3  | Copyright (c) 2007-2010, by members of University of Arizona Computer |
4  | Vision group (the authors) including |
5  | Kobus Barnard, Luca Del Pero, Kyle Simek, Andrew Predoehl. |
6  | |
7  | For use outside the University of Arizona Computer Vision group please |
8  | contact Kobus Barnard. |
9  | |
10  * ======================================================================== */
11 
12 /* $Id: l_int_vector.h 18278 2014-11-25 01:42:10Z ksimek $ */
13 
14 #ifndef L_CPP_INT_VECTOR_WRAP_H
15 #define L_CPP_INT_VECTOR_WRAP_H
16 
39 #include "l/l_debug.h"
40 #include "l/l_int_vector.h"
41 #include "l/l_sys_io.h"
42 #include "l/l_sys_lib.h"
43 
44 #include "l_cpp/l_util.h"
45 #include "l_cpp/l_exception.h"
46 #include <vector>
47 
48 
49 #ifdef KJB_HAVE_BST_SERIAL
50 #include <boost/serialization/access.hpp>
51 #endif
52 
53 #include <boost/concept_check.hpp>
54 
55 
56 // forward declarations
57 namespace boost {
58 namespace archive {
59  class text_iarchive;
60  class text_oarchive;
61 } // namespace archive
62 } // namespace boost
63 
64 namespace kjb {
65 
71 class Int_matrix;
72 
84 {
85 #ifdef KJB_HAVE_BST_SERIAL
86  friend class boost::serialization::access;
87 #endif
88 
89 public:
90 
91 
92  /* Class users: to make your code as maintanable as possible, use the
93  * *_type typedefs below instead of directly referencing the specific
94  * type.
95  */
96 
97  typedef int Value_type;
98  typedef kjb_c::Int_vector Impl_type;
99  typedef Int_matrix Mat_type;
102 
103  // Below are typdefs required to adhere to the stl Container concept
104  typedef int value_type;
105  typedef int* pointer;
106  typedef const int* const_pointer;
107  typedef int& reference;
108  typedef int const_reference;
109  //typedef __gnu_cxx::__normal_iterator<value_type*, Int_vector> iterator;
110  //typedef __gnu_cxx::__normal_iterator<const value_type*, Int_vector>
111  // const_iterator;
113  typedef const value_type* const_iterator;
114  typedef std::reverse_iterator<iterator> reverse_iterator;
115  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
116  typedef int size_type;
117  typedef int difference_type;
118 
119 
120 private:
121  Impl_type* m_vector;
122 
123 
124 
130  void throw_bad_bounds( int ) const;
131 
138  void m_ensure_capacity(size_type c);
139 
146  template<typename InputIterator_>
147  void m_initialize_dispatch(InputIterator_ begin_,
148  InputIterator_ end_, std::input_iterator_tag);
149 
156  template<typename ForwardIterator_>
157  void m_initialize_dispatch(ForwardIterator_ begin_, ForwardIterator_ end_, std::forward_iterator_tag)
158  {
159  const size_type n = std::distance(begin_, end_);
160  m_ensure_capacity(n);
161 
162  m_vector->length = n;
163 
164  std::copy(begin_, end_, this->begin());
165  }
166 public:
167 
168  /* -------------------------------------------------------------------
169  * CONSTRUCTORS
170  * ------------------------------------------------------------------- */
171 
177  explicit Int_vector(int length = 0)
178  : m_vector(0)
179  {
180  // Test program was HERE.
181  ETX( kjb_c::get_target_int_vector(&m_vector, length) );
182  }
183 
184  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
185 
193  explicit Int_vector(unsigned length)
194  : m_vector(0)
195  {
196  // Test program was HERE.
197  ETX( kjb_c::get_target_int_vector(&m_vector, static_cast<int>(length)) );
198  }
199 
200  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
201 
209  explicit Int_vector(unsigned long length)
210  : m_vector(0)
211  {
212  // Test program was HERE.
213  ETX( kjb_c::get_target_int_vector(&m_vector, static_cast<int>(length)) );
214  }
215 
216  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
217 
222  : m_vector(0)
223  {
224  // Test program was HERE.
225  ETX( kjb_c::get_initialized_int_vector(&m_vector, length, num) );
226  }
227 
228  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
229 
233  Int_vector(int length, const Value_type* data)
234  : m_vector(0)
235  {
236  // Test program was HERE.
237  ETX( kjb_c::get_target_int_vector(&m_vector, length) );
238  std::copy( data, data + length, m_vector -> elements );
239  }
240 
241  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
242 
243 
244  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
245 
246 
247  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
248 
268  : m_vector( vec_ptr )
269  {
270  if ( 0 == vec_ptr )
271  {
272  // Test program was HERE.
273  ETX( kjb_c::get_target_int_vector(&m_vector, 0) );
274  }
275  //else
276  //{
277  // // Test program was HERE.
278  //}
279  }
280 
281  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
282 
287  Int_vector(const std::vector<int>& src)
288  : m_vector( NULL )
289  {
290  Int_vector result(src.begin(), src.end());
291  swap(result);
292  }
293 
294 
295  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
296 
306  explicit Int_vector(const Impl_type& vec_ref)
307  : m_vector(0)
308  {
309  // Test program was HERE.
310  ETX( kjb_c::copy_int_vector(&m_vector, &vec_ref) );
311  }
312 
313  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
314 
319  explicit Int_vector(const Mat_type& src);
320 
321  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
322 
326  Int_vector(const std::string& file_name);
327 
328  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
329 
336  Int_vector( const Int_vector& vec_ref )
337  : m_vector(0)
338  {
339  // Test program was HERE.
340  ETX( kjb_c::copy_int_vector(&m_vector, vec_ref.m_vector) );
341  }
342 
343  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
344 
345  /* ---------------------------------------------------------------------
346  * "STL CONSTRUCTORS"
347  *
348  * These constructors are required for compliance with STL's
349  * "Sequence" concept
350  *
351  * ------------------------------------------------------------------- */
352 
361  template<typename InputIterator_>
362  Int_vector(InputIterator_ begin_, InputIterator_ end_)
363  : m_vector(0)
364  {
365  ETX( kjb_c::get_target_int_vector(&m_vector, 8) );
366  m_vector->length = 0;
367 
368  typedef typename std::iterator_traits<InputIterator_>::
369  iterator_category IterCategory_;
370  m_initialize_dispatch(begin_, end_, IterCategory_());
371  }
372 
373 
374  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
375 
380  {
381  // Test program was HERE.
382  kjb_c::free_int_vector(m_vector);
383  }
384 
385  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
386 
390  int get_length() const
391  {
392  // Test program was HERE.
393  return m_vector -> length;
394  }
395 
399  size_type size() const
400  {
401  return get_length();
402  }
403 
408  {
409  return INT_MAX;
410  }
411 
415  bool empty() const
416  {
417  return size() == 0;
418  }
419 
420  // iterators
427  {
428  return iterator(m_vector->elements);
429  }
430 
437  {
438  return const_iterator(m_vector->elements);
439  }
440 
447  {
448  return iterator(m_vector->elements + m_vector->length);
449  }
450 
457  {
458  return const_iterator(m_vector->elements + m_vector->length);
459  }
460 
467  {
468  return reverse_iterator(end());
469  }
470 
477  {
478  return const_reverse_iterator(end());
479  }
480 
487  {
488  return reverse_iterator(begin());
489  }
490 
497  {
498  return const_reverse_iterator(begin());
499  }
500 
501  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
502 
507  void reserve(int capacity)
508  {
509  m_ensure_capacity(capacity);
510  }
511  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
512 
521 
522  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
523 
532  {
533  // Test program was HERE.
534  return randomize( get_length() );
535  }
536 
537  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
538 
539 
540  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
541 
542 
543  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
544 
545 
546  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
547 
548 
549  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
550 
551 
552  /* -------------------------------------------------------------------
553  * ASSIGNMENT OPERATORS
554  * ------------------------------------------------------------------- */
555 
560  Int_vector& operator=(const Impl_type& vec_ref)
561  {
562  if ( m_vector != &vec_ref )
563  {
564  // Test program was HERE.
565  ETX( kjb_c::copy_int_vector(&m_vector, &vec_ref) );
566  }
567  //else
568  //{
569  // // Test program was HERE.
570  //}
571  return *this;
572  }
573 
574  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
575 
581  {
582  // Test program was HERE.
583  // call assignment operator for kjb_c::Int_vector
584  return operator=( *src.m_vector );
585  }
586 
587 
588  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
589 
590 
591  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
592 
597  const Impl_type* get_c_vector() const
598  {
599  // Test program was HERE.
600  return m_vector;
601  }
602 
603  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
604 
611  {
612  // Test program was HERE.
613  return m_vector;
614  }
615  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
616 
620  void swap( Int_vector& other )
621  {
622  // Test program was HERE.
623  std::swap( m_vector, other.m_vector );
624  }
625 
626  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
627 
631  Int_vector& resize(int new_length, Value_type pad = Value_type(0) );
632 
633  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
634 
642  {
643  // Test program was HERE.
644  return m_vector -> elements[ i ];
645  }
646 
647  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
648 
656  {
657  // Test program was HERE.
658  return m_vector -> elements[ i ];
659  }
660 
661  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
662 
669  {
670  // Test program was HERE.
671  return operator[]( i );
672  }
673 
674  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
675 
682  {
683  // Test program was HERE.
684  return operator[]( i );
685  }
686 
687  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
688 
693  void check_bounds( int i ) const
694  {
695  // Test program was HERE.
696  if ( i < 0 || get_length() <= i )
697  {
698  throw_bad_bounds( i );
699  }
700  }
701 
702  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
703 
709  Value_type& at( int i )
710  {
711  // Test program was HERE.
712  check_bounds( i );
713  return operator[]( i );
714  }
715 
716  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
717 
723  Value_type at( int i ) const
724  {
725  // Test program was HERE.
726  check_bounds( i );
727  return operator[]( i );
728  }
729 
730  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
731 
735  iterator insert(iterator position, value_type t);
736 
737  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
738 
742  void insert(iterator position, size_type N, value_type t);
743 
744  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
745 
749  template<typename InputIterator>
750  void insert(iterator position, InputIterator begin_, InputIterator end_);
751 
752  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
753 
759  iterator erase(iterator position);
760 
761  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
762 
769  void erase(iterator begin_, iterator end_);
770 
771  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
772 
778  void clear()
779  {
780  m_vector->length = 0;
781  }
782 
783 
784  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
790  {
791  return operator[](0);
792  }
793 
794  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
795 
801  {
802  return operator[](0);
803  }
804  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
805 
811  {
812  return operator[](size() - 1);
813  }
814 
815  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
816 
822  {
823  return operator[](size() - 1);
824  }
825 
826  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
827 
832  {
833  m_ensure_capacity(m_vector->length + 1);
834 
835  m_vector->length++;
836  KJB(ASSERT(m_vector->length <= m_vector->max_length));
837  m_vector->elements[m_vector->length - 1] = x;
838  }
839 
845  void pop_back()
846  {
847  m_vector->length--;
848  }
849 
857  void write_row( const char* filename = 0 ) const
858  {
859  // Test program was HERE.
860  ETX(kjb_c::write_row_int_vector( m_vector, filename ));
861  }
862 
863  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
864 
872  void write_col( const char* filename = 0 ) const
873  {
874  // Test program was HERE.
875  ETX(kjb_c::write_col_int_vector_with_header( m_vector, filename ));
876  }
877 
878  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
879 
888  void write( const char* filename = 0 ) const
889  {
890  // Test program was HERE.
891  write_row( filename );
892  }
893 
894  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
895 
896  /* ---------------------------------------------------------------------
897  * ARITHMETIC OPERATORS
898  *
899  * Modifying operators; i.e., operators that modify the object. These
900  * are member functions, as per the standard implementation. Operators
901  * that do not modify the vector are non-members. Also, their corresponding
902  * named methods (subtract, add, etc).
903  * ------------------------------------------------------------------- */
904 
909  {
910  // Test program was HERE.
911  ETX( kjb_c::ow_multiply_int_vector_by_int_scalar(m_vector, op2) );
912  return *this;
913  }
914 
915  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
916 
921  {
922  // Test program was HERE.
923  return operator*=( op2 );
924  }
925 
926 
927  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
928 
933 
934  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
935 
940  {
941  // Test program was HERE.
942  return operator/=( op2 );
943  }
944 
945  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
946 
951  {
952  // Test program was HERE.
953  ETX( kjb_c::ow_add_int_vectors(m_vector, op2.m_vector) );
954  return *this;
955  }
956 
957  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
958 
962  Int_vector& add (const Int_vector& op2)
963  {
964  // Test program was HERE.
965  return operator+=( op2 );
966  }
967 
968  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
969 
974  {
975  // Test program was HERE.
976  ETX( kjb_c::ow_subtract_int_vectors(m_vector, op2.m_vector) );
977  return *this;
978  }
979 
980  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
981 
986  {
987  // Test program was HERE.
988  return operator-=( op2 );
989  }
990 
991  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
992 
997  {
998  // Test program was HERE.
999  return operator*=( -1 );
1000  }
1001 
1002  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1003 
1010  Vec_type& mapcar( Mapper );
1011 
1012  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1013 
1017  Value_type min() const
1018  {
1019  // Test program was HERE.
1020  return kjb_c::min_int_vector_element(m_vector);
1021  }
1022 
1023  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1024 
1044  Value_type min( int* min_index ) const
1045  {
1046  if ( 0 == get_length() )
1047  {
1048  // Test program was HERE.
1050  "Int_vector is empty; min is undefined" );
1051  }
1052  if ( 0 == min_index )
1053  {
1054  // Test program was HERE.
1055  return min();
1056  }
1057  // Test program was HERE.
1058  Value_type min_val;
1059  *min_index = kjb_c::get_min_int_vector_element(m_vector, &min_val);
1060  return min_val;
1061  }
1062 
1063  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1064 
1068  Value_type max() const
1069  {
1070  // Test program was HERE.
1071  return kjb_c::max_int_vector_element(m_vector);
1072  }
1073 
1074  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1075 
1095  Value_type max( int* max_index ) const
1096  {
1097  if ( 0 == get_length() )
1098  {
1099  // Test program was HERE.
1101  "Int_vector is empty; max is undefined" );
1102  }
1103  if ( 0 == max_index )
1104  {
1105  // Test program was HERE.
1106  return max();
1107  }
1108  // Test program was HERE.
1109  Value_type max_val;
1110  *max_index = kjb_c::get_max_int_vector_element(m_vector, &max_val);
1111  return max_val;
1112  }
1113 
1114  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1115 
1116 
1117 
1118  /* -------------------------------------------------------------------
1119  * VECTOR-SPECIFIC METHODS
1120  * ------------------------------------------------------------------- */
1121 
1127  Int_vector& cross_with(const Int_vector& op2);
1128 
1129  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1130 
1142  kjb::Int_matrix hat() const;
1143 
1144  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1145 
1149  double magnitude() const
1150  {
1151  // Test program was HERE.
1152  return kjb_c::int_vector_magnitude(m_vector);
1153  }
1154 
1155  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1156 
1160  long int magnitude_squared() const
1161  {
1162  // Test program was HERE.
1163  return kjb_c::sum_int_vector_squared_elements(m_vector);
1164  }
1165 
1166  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1167 
1168 private:
1169  void serialize(boost::archive::text_iarchive &ar, const unsigned int version);
1170  void serialize(boost::archive::text_oarchive &ar, const unsigned int version);
1171 };
1172 
1173 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1174 
1175 /* ---------------------------------------------------------------------
1176  * TEMPLATE MEMBER FUNCTIONS
1177  *
1178  * Template member function definitions go here.
1179  * ------------------------------------------------------------------- */
1180 
1181 template<typename InputIterator_>
1182 void Int_vector::m_initialize_dispatch
1183 (
1184  InputIterator_ begin_,
1185  InputIterator_ end_,
1186  std::input_iterator_tag
1187 )
1188 {
1189  for(; begin_ != end_; ++begin_)
1190  {
1191  push_back(*begin_);
1192  }
1193 }
1194 
1195 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1196 
1197 template<typename InputIterator>
1198 void Int_vector::insert
1200  iterator position,
1201  InputIterator begin_,
1202  InputIterator end_
1203 )
1204 {
1205  const size_type N = end_ - begin_;
1206 
1207  m_ensure_capacity(m_vector->length + N);
1208 
1209  m_vector->length += N;
1210 
1211  reverse_iterator rit;
1212  for(rit = rbegin(); rit.base() != position; rit++)
1213  {
1214  *rit = *rit + N; //
1215  }
1216 
1217  iterator dest_it = position;
1218  InputIterator src_it = begin_;
1219  for(; src_it != end_; src_it++)
1220  {
1221  *dest_it++ = *src_it;
1222  }
1223 }
1224 
1225 /* ---------------------------------------------------------------------
1226  * "NAMED CONSTRUCTORS"
1227  *
1228  * The "named constructor idiom" is when we use functions to create
1229  * objects. The benefit is that the names indicate the purpose of the
1230  * method. The following functions act as named constructors.
1231  * ------------------------------------------------------------------- */
1232 
1236 inline
1238 {
1239  // Test program was HERE.
1240  Int_vector result( length );
1241  result.randomize();
1242  return result;
1243 }
1244 
1245 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1246 
1251 inline
1253 {
1254  // Test program was HERE.
1255  kjb_c::Int_vector* outvec = 0;
1256  ETX_2( kjb_c::copy_int_vector_section(&outvec,
1257  iv.get_c_vector(), begin, length), "Failure in create_vector_from_vector_section : allocation error or bad indices");
1258  return Int_vector(outvec);
1259 }
1260 
1261 /* ---------------------------------------------------------------------
1262  * ARITHMETIC OPERATORS
1263  *
1264  * Modifying operators; i.e., operators that modify the object. These
1265  * are member functions, as per the standard implementation. Operators
1266  * that do not modify the vector are non-members. Also, their corresponding
1267  * named methods (subtract, add, etc).
1268  * ------------------------------------------------------------------- */
1269 
1280 Int_vector operator*(const Int_vector& op1, const Int_matrix& op2);
1281 
1282 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1283 
1290 Int_vector operator*( const Int_matrix& op1, const Int_vector& op2 );
1291 
1292 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1293 
1297 inline
1299 {
1300  // Test program was HERE.
1301  return Int_vector(op1) *= op2;
1302 }
1303 
1304 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1305 
1312 inline
1314 {
1315  // Test program was HERE.
1316  return op2 * op1;
1317 }
1318 
1319 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1320 
1324 inline
1326 {
1327  // Test program was HERE.
1328  return Int_vector(op1) /= op2;
1329 }
1330 
1331 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1332 
1336 inline
1338 {
1339  // Test program was HERE.
1340  return Int_vector(op1) += op2;
1341 }
1342 
1343 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1344 
1349 inline
1351 {
1352  // Test program was HERE.
1353  return Int_vector(op1) -= op2;
1354 }
1355 
1360 inline
1362 {
1363  // Test program was HERE.
1364  return op1 * (-1);
1365 }
1366 
1367 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1368 
1375 std::ostream& operator<<(std::ostream& out, const Int_vector& m);
1376 
1377 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1378 
1379 /* ---------------------------------------------------------------------
1380  * COMPARISON OPERATORS
1381  *
1382  * Comparison operators (==, !=).
1383  * ------------------------------------------------------------------- */
1384 
1389 bool operator==(const Int_vector& op1, const Int_vector::Impl_type& op2);
1390 
1391 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1392 
1399 inline
1400 bool operator==(const Int_vector::Impl_type& op1, const Int_vector& op2)
1401 {
1402  // Test program was HERE.
1403  return op2 == op1;
1404 }
1405 
1406 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1407 
1412 inline
1413 bool operator!=(const Int_vector& op1, const Int_vector::Impl_type &op2)
1414 {
1415  // Test program was HERE.
1416  return !(op1 == op2);
1417 }
1418 
1419 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1420 
1427 inline
1428 bool operator!=(const Int_vector::Impl_type& op1, const Int_vector& op2)
1429 {
1430  // Test program was HERE.
1431  return op2 != op1;
1432 }
1433 
1434 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1435 
1440 inline
1441 bool operator==(const Int_vector& op1, const Int_vector& op2)
1442 {
1443  // Test program was HERE.
1444  return op1 == *op2.get_c_vector();
1445 }
1446 
1447 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1448 
1453 inline
1454 bool operator!=(const Int_vector& op1, const Int_vector &op2)
1455 {
1456  // Test program was HERE.
1457  return op1 != *op2.get_c_vector();
1458 }
1459 
1460 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1461 
1462 /* ---------------------------------------------------------------------
1463  * ORDERING OPERATORS
1464  * Ordering operators (<, >=, etc).
1465  * ------------------------------------------------------------------- */
1466 
1470 bool operator<(const Int_vector& op1, const Int_vector &op2);
1471 
1475 inline
1476 bool operator<=(const Int_vector& op1, const Int_vector& op2)
1477 {
1478  return (op1 < op2) || (op1 == op2);
1479 }
1480 
1484 inline
1485 bool operator>(const Int_vector& op1, const Int_vector& op2)
1486 {
1487  return !(op1 < op2) && !(op1 == op2);
1488 }
1489 
1493 inline
1494 bool operator>=(const Int_vector& op1, const Int_vector& op2)
1495 {
1496  return !(op1 < op2);
1497 }
1498 
1499 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1500 
1501 /* ---------------------------------------------------------------------
1502  * OTHER STUFF
1503  * ------------------------------------------------------------------- */
1504 
1511 inline
1513 {
1514  if ( op1.get_length() != op2.get_length() )
1515  {
1516  // Test program was HERE.
1518  }
1519  //else
1520  //{
1521  // // Test program was HERE.
1522  //}
1523  return kjb_c::max_abs_int_vector_difference( op1.get_c_vector(), op2.get_c_vector() );
1524 }
1525 
1526 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1527 
1531 inline
1532 long int dot(const Int_vector& op1, const Int_vector& op2)
1533 {
1534  // Test program was HERE.
1535  long int result;
1536  ETX( kjb_c::get_int_dot_product(op1.get_c_vector(), op2.get_c_vector(), &result) );
1537  return result;
1538 }
1539 
1540 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1541 
1547 Int_vector cross(const Int_vector& op1, const Int_vector& op2);
1548 
1549 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1550 
1557 inline
1558 double norm2(const Int_vector& op1)
1559 {
1560  return op1.magnitude();
1561 }
1562 
1568 inline
1569 double vector_distance(const Int_vector& op1, const Int_vector& op2)
1570 {
1571  Int_vector temp = op1 - op2;
1572  return temp.magnitude();
1573 }
1574 
1580 inline
1581 double vector_distance_squared(const Int_vector& op1, const Int_vector& op2)
1582 {
1583  Int_vector temp = op1 - op2;
1584  return temp.magnitude_squared();
1585 }
1586 
1589 } //namespace
1590 
1591 #endif
1592 
reverse_iterator rend()
Definition: l_int_vector.h:486
const_reference front() const
Returns the first element of the vector.
Definition: l_int_vector.h:800
Int_vector & randomize(int length)
Clobber current vector; resize and fill with random values.
Definition: l_int_vector.cpp:160
double vector_distance(const Int_vector &op1, const Int_vector &op2)
Compute the Euclidian distance between two vectors.
Definition: l_int_vector.h:1569
Int_vector cross(const Int_vector &op1, const Int_vector &op2)
Compute cross product of op1 and op2.
Definition: l_int_vector.cpp:381
#define ETX(a)
Definition: l_exception.h:67
double vector_distance_squared(const Int_vector &op1, const Int_vector &op2)
Compute the square of the Euclidian distance between two vectors.
Definition: l_int_vector.h:1581
Int_vector & operator+=(const Int_vector &op2)
Add vector to self, in-place, e.g., v += delta_v.
Definition: l_int_vector.h:950
int difference_type
Definition: l_int_vector.h:117
void push_back(Value_type x)
inserts an element at the back of the vector in amortized constant time.
Definition: l_int_vector.h:831
Int_vector & divide(Value_type op2)
Scalar integer division of self, in-place, just like v /= 2.
Definition: l_int_vector.h:939
void write_col(const char *filename=0) const
Write vector as a column to a file, or to standard output.
Definition: l_int_vector.h:872
Int_vector create_random_int_vector(int length)
Construct a vector with random contents.
Definition: l_int_vector.h:1237
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
int * pointer
Definition: l_int_vector.h:105
Int_vector(Impl_type *vec_ptr)
Conversion ctor: claim ownership of an existing int vector pointer (i.e., make a shallow copy)...
Definition: l_int_vector.h:267
int value_type
Definition: l_int_vector.h:104
value_type * iterator
Definition: l_int_vector.h:112
This class implements matrices, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_matrix.h:71
Int_vector Vec_type
the associated vector type
Definition: l_int_vector.h:100
Int_matrix::Value_type max_abs_difference(const Int_matrix &op1, const Int_matrix &op2)
Find the largest difference between two matrices.
Definition: l_int_matrix.h:1364
Value_type min(int *min_index) const
Find minimum element in the vector, both its value and index.
Definition: l_int_vector.h:1044
Int_vector & negate()
Negate self, in-place, just like v *= (-1).
Definition: l_int_vector.h:996
kjb_c::Int_vector Impl_type
the underlying implementation
Definition: l_int_vector.h:98
#define KJB(x)
Definition: l_util.h:9
const_iterator end() const
Definition: l_int_vector.h:456
Int_vector & operator=(const Int_vector &src)
Assignment operator: assign from a kjb::Int_vector, a C++ object.
Definition: l_int_vector.h:580
int Value_type
data type of the elements
Definition: l_int_vector.h:97
bool operator<(const Face_detection &f1, const Face_detection &f2)
Compares to boxes using middle of box. Needed because we have associated containers of these...
Definition: d_facecom.cpp:147
int & reference
Definition: l_int_vector.h:107
Image operator-(const Image &im1, const Image &im2)
Subtract two images.
Definition: i_image.h:843
#define KJB_THROW(ex)
Definition: l_exception.h:46
#define ASSERT(condition, message)
Definition: Assert.h:45
bool operator>=(const Int_vector &op1, const Int_vector &op2)
Test lexicographic ordering between vectors.
Definition: l_int_vector.h:1494
void write(const char *filename=0) const
Write vector to a file, or to standard output.
Definition: l_int_vector.h:888
const_reverse_iterator rend() const
Definition: l_int_vector.h:496
void clear()
delete all elements from vector
Definition: l_int_vector.h:778
size_t length(const C &cner)
Counts the total number of elements in a 2D STL-style container.
Definition: l_util.h:17
Object thrown when a function cannot generate a valid result.
Definition: l_exception.h:516
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: l_int_vector.h:399
Image operator+(const Image &op1, const Image &op2)
Add two images.
Definition: i_image.h:834
reference back()
Returns the last element of the vector.
Definition: l_int_vector.h:810
iterator end()
Definition: l_int_vector.h:446
Value_type(* Mapper)(Value_type)
element transformer fun
Definition: l_int_vector.h:101
Value_type operator()(int i) const
Fortran-style vector subscript, e.g., A(10), to get an rvalue.
Definition: l_int_vector.h:681
std::reverse_iterator< iterator > reverse_iterator
Definition: l_int_vector.h:114
void check_bounds(int i) const
Test whether a subscript is valid, throw an exception if not.
Definition: l_int_vector.h:693
Int_vector & operator/=(Value_type op2)
Scalar integer division of self, in-place.
Definition: l_int_vector.cpp:304
Int_vector & resize(int new_length, Value_type pad=Value_type(0))
Resize vector, retaining previous values.
Definition: l_int_vector.cpp:147
void write_row(const char *filename=0) const
Write vector as a row to a file, or to standard output.
Definition: l_int_vector.h:857
Int_vector & operator*=(Value_type op2)
Scalar multiply self, in-place, e.g., v *= 6.
Definition: l_int_vector.h:908
bool empty() const
Returns true iff size is zero. Required to comply with stl Container concept.
Definition: l_int_vector.h:415
Int_vector(unsigned long length)
Allocate vector of given length; contents are uninitialized.
Definition: l_int_vector.h:209
Int_vector(const Impl_type &vec_ref)
Ctor copies contents (i.e., deep copy) of an existing vector.
Definition: l_int_vector.h:306
std::reverse_iterator< const_iterator > const_reverse_iterator
const Iterator type
Definition: l_int_vector.h:115
reference front()
Returns the first element of the vector.
Definition: l_int_vector.h:789
x
Definition: APPgetLargeConnectedEdges.m:100
Vec_type & mapcar(Mapper)
Transform the elements of a vector.
Definition: l_int_vector.cpp:323
This class implements vectors, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_vector.h:83
Int_vector & add(const Int_vector &op2)
Add vector to self, in-place, just like v += delta_v.
Definition: l_int_vector.h:962
bool operator>(const Int_vector &op1, const Int_vector &op2)
Test lexicographic ordering between vectors.
Definition: l_int_vector.h:1485
Value_type min() const
Return the value of the minimum element in the vector.
Definition: l_int_vector.h:1017
Value_type & operator()(int i)
Fortran-style vector subscript, e.g., A(10), to get an lvalue.
Definition: l_int_vector.h:668
const value_type * const_iterator
Definition: l_int_vector.h:113
Int_vector(unsigned length)
Allocate vector of given length; contents are uninitialized.
Definition: l_int_vector.h:193
std::ofstream & operator<<(std::ofstream &out, const Quaternion &q)
Definition: turntable_camera.cpp:77
Image operator/(const Image &op1, double op2)
Scale an image in channel space, yielding a new image.
Definition: i_image.h:825
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
bool operator!=(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for any difference between two matrices.
Definition: l_int_matrix.h:1274
int size_type
Definition: l_int_vector.h:116
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
iterator insert(iterator position, value_type t)
A copy of t is inserted before position.
Int_vector & subtract(const Int_vector &op2)
Subtract vector from self, in-place, just like v -= delta_v.
Definition: l_int_vector.h:985
bool operator==(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for exact equality between two matrices.
Definition: l_int_matrix.cpp:218
Int_vector create_vector_from_vector_section(const Int_vector &iv, int begin, int length)
Construct a vector by deep-copying a section of another vector.
Definition: l_int_vector.h:1252
#define ETX_2(a, msg)
Definition: l_exception.h:78
kjb::Int_matrix hat() const
Construct an "equivalent" skew-symmetric matrix from 3-vector.
Definition: l_int_vector.cpp:337
reverse_iterator rbegin()
Definition: l_int_vector.h:466
Value_type max() const
Return the value of the maximum element in the vector.
Definition: l_int_vector.h:1068
Int_vector(const Int_vector &vec_ref)
Copy ctor – calls the kjb_c function to copy an int vector.
Definition: l_int_vector.h:336
Int_vector & multiply(Value_type op2)
Scalar multiply self, in-place, just like v *= 6.
Definition: l_int_vector.h:920
Value_type max(int *max_index) const
Find maximum element in the vector, both its value and index.
Definition: l_int_vector.h:1095
Int_vector(int length, const Value_type *data)
Initialize from an array of int, of given length.
Definition: l_int_vector.h:233
Int_vector(const std::vector< int > &src)
Conversion ctor: Create from stl-style vector.
Definition: l_int_vector.h:287
Value_type at(int i) const
Safely subscript vector, e.g., A.at(10), to get an rvalue.
Definition: l_int_vector.h:723
size_type max_size() const
Maximum size vector can ever have. Currently defined as INT_MAX.
Definition: l_int_vector.h:407
void swap(Int_vector &other)
Swap the representations of two vectors.
Definition: l_int_vector.h:620
const_iterator begin() const
Definition: l_int_vector.h:436
const_reference back() const
Returns the last element of the vector.
Definition: l_int_vector.h:821
double norm2(const Int_vector &op1)
Compute l2-norm of vector.
Definition: l_int_vector.h:1558
Int_matrix Mat_type
the associated matrix type
Definition: l_int_vector.h:99
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
iterator erase(iterator position)
erase element at position and shift all elements after p up by 1.
Value_type operator[](int i) const
Subscript vector like a C array, e.g., A[10], returning an rvalue.
Definition: l_int_vector.h:655
Int_vector & operator-=(const Int_vector &op2)
Subtract vector from self, in-place, e.g., v -= delta_v.
Definition: l_int_vector.h:973
const Impl_type * get_c_vector() const
Get const pointer to the underlying kjb_c::Int_vector C struct.
Definition: l_int_vector.h:597
Int_vector & cross_with(const Int_vector &op2)
Compute (in place) cross product of this vector and op2.
Definition: l_int_vector.cpp:364
iterator begin()
Definition: l_int_vector.h:426
int const_reference
Definition: l_int_vector.h:108
void pop_back()
Returns the last element of the vector.
Definition: l_int_vector.h:845
long int dot(const Int_vector &op1, const Int_vector &op2)
Returns dot product of this and op2.
Definition: l_int_vector.h:1532
int get_length() const
Return the length of the vector.
Definition: l_int_vector.h:390
for m
Definition: APPgetLargeConnectedEdges.m:64
Support for error handling exception classes in libKJB.
Impl_type * get_underlying_representation_with_guilt()
Get pointer to the underlying kjb_c::Vector C struct.
Definition: l_int_vector.h:610
void reserve(int capacity)
Definition: l_int_vector.h:507
const_reverse_iterator rbegin() const
Definition: l_int_vector.h:476
const int * const_pointer
Definition: l_int_vector.h:106
Value_type & operator[](int i)
Subscript vector like a C array, e.g., A[10], returning an lvalue.
Definition: l_int_vector.h:641
Value_type & at(int i)
Safely subscript vector, e.g., A.at(10), to get an lvalue.
Definition: l_int_vector.h:709
bool operator<=(const Int_vector &op1, const Int_vector &op2)
Test lexicographic ordering between vectors.
Definition: l_int_vector.h:1476
Int_vector(int length, Value_type num)
Allocate vector of given length; initialize all elts to 'num'.
Definition: l_int_vector.h:221
Int_vector(InputIterator_ begin_, InputIterator_ end_)
"Range constructor"
Definition: l_int_vector.h:362
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
Int_vector & operator=(const Impl_type &vec_ref)
Assignment operator: assign from a kjb_c::Int_vector, a C struct.
Definition: l_int_vector.h:560
double magnitude() const
Return this vector's magnitude.
Definition: l_int_vector.h:1149
Int_vector & randomize()
Clobber current vector contents with random values.
Definition: l_int_vector.h:531
Int_vector(int length=0)
Allocate vector of given length; contents are uninitialized.
Definition: l_int_vector.h:177
~Int_vector()
Destructor – which just calls the KJB destructor.
Definition: l_int_vector.h:379
long int magnitude_squared() const
Return this vector's squared magnitude.
Definition: l_int_vector.h:1160