op_unique_meat.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 op_unique
  16. //! @{
  17. template<typename T1>
  18. inline
  19. bool
  20. op_unique::apply_helper(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const bool P_is_row)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const uword n_elem = P.get_n_elem();
  25. if(n_elem == 0)
  26. {
  27. if(P_is_row)
  28. {
  29. out.set_size(1,0);
  30. }
  31. else
  32. {
  33. out.set_size(0,1);
  34. }
  35. return true;
  36. }
  37. if(n_elem == 1)
  38. {
  39. const eT tmp = (Proxy<T1>::use_at) ? P.at(0,0) : P[0];
  40. out.set_size(1, 1);
  41. out[0] = tmp;
  42. return true;
  43. }
  44. Mat<eT> X(n_elem,1);
  45. eT* X_mem = X.memptr();
  46. if(Proxy<T1>::use_at == false)
  47. {
  48. typename Proxy<T1>::ea_type Pea = P.get_ea();
  49. for(uword i=0; i<n_elem; ++i)
  50. {
  51. const eT val = Pea[i];
  52. if(arma_isnan(val)) { out.soft_reset(); return false; }
  53. X_mem[i] = val;
  54. }
  55. }
  56. else
  57. {
  58. const uword n_rows = P.get_n_rows();
  59. const uword n_cols = P.get_n_cols();
  60. for(uword col=0; col < n_cols; ++col)
  61. for(uword row=0; row < n_rows; ++row)
  62. {
  63. const eT val = P.at(row,col);
  64. if(arma_isnan(val)) { out.soft_reset(); return false; }
  65. (*X_mem) = val; X_mem++;
  66. }
  67. X_mem = X.memptr();
  68. }
  69. arma_unique_comparator<eT> comparator;
  70. std::sort( X.begin(), X.end(), comparator );
  71. uword N_unique = 1;
  72. for(uword i=1; i < n_elem; ++i)
  73. {
  74. const eT a = X_mem[i-1];
  75. const eT b = X_mem[i ];
  76. const eT diff = a - b;
  77. if(diff != eT(0)) { ++N_unique; }
  78. }
  79. if(P_is_row)
  80. {
  81. out.set_size(1, N_unique);
  82. }
  83. else
  84. {
  85. out.set_size(N_unique, 1);
  86. }
  87. eT* out_mem = out.memptr();
  88. if(n_elem > 0) { (*out_mem) = X_mem[0]; out_mem++; }
  89. for(uword i=1; i < n_elem; ++i)
  90. {
  91. const eT a = X_mem[i-1];
  92. const eT b = X_mem[i ];
  93. const eT diff = a - b;
  94. if(diff != eT(0)) { (*out_mem) = b; out_mem++; }
  95. }
  96. return true;
  97. }
  98. template<typename T1>
  99. inline
  100. void
  101. op_unique::apply(Mat<typename T1::elem_type>& out, const Op<T1, op_unique>& in)
  102. {
  103. arma_extra_debug_sigprint();
  104. const Proxy<T1> P(in.m);
  105. const bool all_non_nan = op_unique::apply_helper(out, P, false);
  106. arma_debug_check( (all_non_nan == false), "unique(): detected NaN" );
  107. }
  108. template<typename T1>
  109. inline
  110. void
  111. op_unique_vec::apply(Mat<typename T1::elem_type>& out, const Op<T1, op_unique_vec>& in)
  112. {
  113. arma_extra_debug_sigprint();
  114. const Proxy<T1> P(in.m);
  115. const bool P_is_row = (T1::is_xvec) ? bool(P.get_n_rows() == 1) : bool(T1::is_row);
  116. const bool all_non_nan = op_unique::apply_helper(out, P, P_is_row);
  117. arma_debug_check( (all_non_nan == false), "unique(): detected NaN" );
  118. }
  119. //! @}