glue_intersect_meat.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup glue_intersect
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. glue_intersect::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_intersect>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. uvec iA;
  24. uvec iB;
  25. glue_intersect::apply(out, iA, iB, X.A, X.B, false);
  26. }
  27. template<typename T1, typename T2>
  28. inline
  29. void
  30. glue_intersect::apply(Mat<typename T1::elem_type>& out, uvec& iA, uvec& iB, const Base<typename T1::elem_type,T1>& A_expr, const Base<typename T1::elem_type,T2>& B_expr, const bool calc_indx)
  31. {
  32. arma_extra_debug_sigprint();
  33. typedef typename T1::elem_type eT;
  34. const quasi_unwrap<T1> UA(A_expr.get_ref());
  35. const quasi_unwrap<T2> UB(B_expr.get_ref());
  36. if(UA.M.is_empty() || UB.M.is_empty())
  37. {
  38. out.reset();
  39. iA.reset();
  40. iB.reset();
  41. return;
  42. }
  43. uvec A_uniq_indx;
  44. uvec B_uniq_indx;
  45. Mat<eT> A_uniq;
  46. Mat<eT> B_uniq;
  47. if(calc_indx)
  48. {
  49. A_uniq_indx = find_unique(UA.M);
  50. B_uniq_indx = find_unique(UB.M);
  51. A_uniq = UA.M.elem(A_uniq_indx);
  52. B_uniq = UB.M.elem(B_uniq_indx);
  53. }
  54. else
  55. {
  56. A_uniq = unique(UA.M);
  57. B_uniq = unique(UB.M);
  58. }
  59. const uword C_n_elem = A_uniq.n_elem + B_uniq.n_elem;
  60. Col<eT> C(C_n_elem);
  61. arrayops::copy(C.memptr(), A_uniq.memptr(), A_uniq.n_elem);
  62. arrayops::copy(C.memptr() + A_uniq.n_elem, B_uniq.memptr(), B_uniq.n_elem);
  63. uvec C_sorted_indx;
  64. Col<eT> C_sorted;
  65. if(calc_indx)
  66. {
  67. C_sorted_indx = stable_sort_index(C);
  68. C_sorted = C.elem(C_sorted_indx);
  69. }
  70. else
  71. {
  72. C_sorted = sort(C);
  73. }
  74. const eT* C_sorted_mem = C_sorted.memptr();
  75. uvec jj(C_n_elem); // worst case length
  76. uword* jj_mem = jj.memptr();
  77. uword jj_count = 0;
  78. for(uword i=0; i < (C_n_elem-1); ++i)
  79. {
  80. if( C_sorted_mem[i] == C_sorted_mem[i+1] )
  81. {
  82. jj_mem[jj_count] = i;
  83. ++jj_count;
  84. }
  85. }
  86. if(jj_count == 0)
  87. {
  88. out.reset();
  89. iA.reset();
  90. iB.reset();
  91. return;
  92. }
  93. const uvec ii(jj.memptr(), jj_count, false);
  94. if(UA.M.is_rowvec() && UB.M.is_rowvec())
  95. {
  96. out.set_size(1, ii.n_elem);
  97. Mat<eT> out_alias(out.memptr(), ii.n_elem, 1, false, true);
  98. // NOTE: this relies on .elem() not changing the size of the output and not reallocating memory for the output
  99. out_alias = C_sorted.elem(ii);
  100. }
  101. else
  102. {
  103. out = C_sorted.elem(ii);
  104. }
  105. if(calc_indx)
  106. {
  107. iA = A_uniq_indx.elem(C_sorted_indx.elem(ii ) );
  108. iB = B_uniq_indx.elem(C_sorted_indx.elem(ii+1) - A_uniq.n_elem);
  109. }
  110. }
  111. //! @}