vnl_scalar_join_iterator.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_scalar_join_iterator.h
2 #ifndef vnl_scalar_join_iterator_h_
3 #define vnl_scalar_join_iterator_h_
4 //:
5 // \file
6 // \brief Database join on matrix columns
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 27 Dec 96
9 //
10 // vnl_scalar_join_iterator implements a fast database join on columns
11 // of matrices of scalars. "Scalar" here really means that the
12 // objects have comparison operators. The cost is O(n log n) where
13 // n is the number of rows, all for the two sorts in the ctor.
14 //
15 // CAVEAT: The current implementation fudges multiple occurrences
16 // of the same key in the source column. For example,
17 // \verbatim
18 // join 1 3 and 3 5 on columns 2 and 1 respectively
19 // 2 3 3 6
20 // \endverbatim
21 // should give
22 // \verbatim
23 // 1 3 3 5
24 // 1 3 3 6
25 // 2 3 3 5
26 // 2 3 3 6
27 // \endverbatim
28 // and it doesn't. Contact awf if you need this to work.
29 //
30 // \verbatim
31 // Modifications
32 // LSB (Manchester) Documentation Tidied
33 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
34 // \endverbatim
35 //
36 //-----------------------------------------------------------------------------
37 
38 #include <list>
39 #ifdef _MSC_VER
40 # include <vcl_msvc_warnings.h>
41 #endif
42 #include <vnl/vnl_matrix.h>
43 #include "vnl/vnl_export.h"
44 
45 template <class T>
47 
48 //: Database join on matrix columns.
49 // vnl_scalar_join_iterator implements a fast database join on columns
50 // of matrices of scalars. "Scalar" here really means that the
51 // objects have comparison operators. The cost is O(n log n) where
52 // n is the number of rows, all for the two sorts in the ctor.
53 //
54 // CAVEAT: The current implementation fudges multiple occurrences
55 // of the same key in the source column. For example,
56 // \verbatim
57 // join 1 3 and 3 5 on columns 2 and 1 respectively
58 // 2 3 3 6
59 // \endverbatim
60 // should give
61 // \verbatim
62 // 1 3 3 5
63 // 1 3 3 6
64 // 2 3 3 5
65 // 2 3 3 6
66 // \endverbatim
67 // and it doesn't. Contact awf if you need this to work.
68 
69 template <class T>
70 class VNL_EXPORT vnl_scalar_join_iterator
71 {
72  private:
73 
74  protected:
75  unsigned n1;
76  unsigned n2;
77  std::list<vnl_scalar_join_iterator_indexed_pair<T> >* pI1;
78  std::list<vnl_scalar_join_iterator_indexed_pair<T> >* pI2;
79  std::list<vnl_scalar_join_iterator_indexed_pair<T> >& I1;
80  std::list<vnl_scalar_join_iterator_indexed_pair<T> >& I2;
81  typename std::list<vnl_scalar_join_iterator_indexed_pair<T> >::iterator index1;
82  typename std::list<vnl_scalar_join_iterator_indexed_pair<T> >::iterator index2;
83 
84  public:
85 
86  //: Initialize this iterator to the join of relation1(:,column1) and relation2(:,column2).
87  // The algorithm sorts an array of pointers to each row and
88  // traversal of the iterator runs through these to produce the join.
89  // After construction the row1() and row2() methods indicate the first pair.
90  vnl_scalar_join_iterator(const vnl_matrix<T>& relation1, unsigned column1,
91  const vnl_matrix<T>& relation2, unsigned column2);
92 
94 
95 
96  //: Return true if all pairs have been seen.
97  explicit operator bool () const
98  { return (!done())? true : false; }
99 
100  //: Return false if all pairs have been seen.
101  bool operator!() const
102  { return (!done())? false : true; }
103 
104  //: Advance to the next pair. This is prefix ++.
105  inline vnl_scalar_join_iterator<T>& operator ++ () { next(); return *this; }
106 
107  bool done() const;
108  void next();
109 
110  //: Return the index of the current row in the first relation.
111  unsigned row1() const;
112  //: Return the index of the current row in the second relation.
113  unsigned row2() const;
114 
115  private:
116  // Postfix ++ is private as it would be costly to implement.
117  vnl_scalar_join_iterator<T> operator++ (int);
118 
119 };
120 
121 //: Helper class to hold the sorted arrays of indices.
122 template <class T>
124 {
125  public:
126  const T* object;
127  int original_index;
128 
130  vnl_scalar_join_iterator_indexed_pair(const T* object_, int original_index_):object(object_), original_index(original_index_) {}
131 
134 };
135 
136 #endif // vnl_scalar_join_iterator_h_
An ordinary mathematical matrix.
Database join on matrix columns.
Helper class to hold the sorted arrays of indices.
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
bool operator==(const vnl_amoeba_SimplexCorner &a, const vnl_amoeba_SimplexCorner &b)
Definition: vnl_amoeba.cxx:143
bool operator<(long r1, vnl_bignum const &r2)
Definition: vnl_bignum.h:425
vnl_scalar_join_iterator_indexed_pair(const T *object_, int original_index_)