From eda5bc26f44ee9a6f83dcf8c91f17296d7fc509d Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 12 Feb 2024 14:52:43 +0100 Subject: Move into version control --- .../include/armadillo_bits/glue_intersect_meat.hpp | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/armadillo/include/armadillo_bits/glue_intersect_meat.hpp (limited to 'src/armadillo/include/armadillo_bits/glue_intersect_meat.hpp') diff --git a/src/armadillo/include/armadillo_bits/glue_intersect_meat.hpp b/src/armadillo/include/armadillo_bits/glue_intersect_meat.hpp new file mode 100644 index 0000000..21a2b6d --- /dev/null +++ b/src/armadillo/include/armadillo_bits/glue_intersect_meat.hpp @@ -0,0 +1,148 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au) +// Copyright 2008-2016 National ICT Australia (NICTA) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + + +//! \addtogroup glue_intersect +//! @{ + + + +template +inline +void +glue_intersect::apply(Mat& out, const Glue& X) + { + arma_extra_debug_sigprint(); + + uvec iA; + uvec iB; + + glue_intersect::apply(out, iA, iB, X.A, X.B, false); + } + + + +template +inline +void +glue_intersect::apply(Mat& out, uvec& iA, uvec& iB, const Base& A_expr, const Base& B_expr, const bool calc_indx) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const quasi_unwrap UA(A_expr.get_ref()); + const quasi_unwrap UB(B_expr.get_ref()); + + if(UA.M.is_empty() || UB.M.is_empty()) + { + out.reset(); + iA.reset(); + iB.reset(); + return; + } + + uvec A_uniq_indx; + uvec B_uniq_indx; + + Mat A_uniq; + Mat B_uniq; + + if(calc_indx) + { + A_uniq_indx = find_unique(UA.M); + B_uniq_indx = find_unique(UB.M); + + A_uniq = UA.M.elem(A_uniq_indx); + B_uniq = UB.M.elem(B_uniq_indx); + } + else + { + A_uniq = unique(UA.M); + B_uniq = unique(UB.M); + } + + const uword C_n_elem = A_uniq.n_elem + B_uniq.n_elem; + + Col C(C_n_elem, arma_nozeros_indicator()); + + arrayops::copy(C.memptr(), A_uniq.memptr(), A_uniq.n_elem); + arrayops::copy(C.memptr() + A_uniq.n_elem, B_uniq.memptr(), B_uniq.n_elem); + + uvec C_sorted_indx; + Col C_sorted; + + if(calc_indx) + { + C_sorted_indx = stable_sort_index(C); + C_sorted = C.elem(C_sorted_indx); + } + else + { + C_sorted = sort(C); + } + + const eT* C_sorted_mem = C_sorted.memptr(); + + uvec jj(C_n_elem, arma_nozeros_indicator()); // worst case length + + uword* jj_mem = jj.memptr(); + uword jj_count = 0; + + for(uword i=0; i < (C_n_elem-1); ++i) + { + if( C_sorted_mem[i] == C_sorted_mem[i+1] ) + { + jj_mem[jj_count] = i; + ++jj_count; + } + } + + if(jj_count == 0) + { + out.reset(); + iA.reset(); + iB.reset(); + return; + } + + const uvec ii(jj.memptr(), jj_count, false); + + if(UA.M.is_rowvec() && UB.M.is_rowvec()) + { + out.set_size(1, ii.n_elem); + + Mat out_alias(out.memptr(), ii.n_elem, 1, false, true); + + // NOTE: this relies on .elem() not changing the size of the output and not reallocating memory for the output + out_alias = C_sorted.elem(ii); + } + else + { + out = C_sorted.elem(ii); + } + + if(calc_indx) + { + iA = A_uniq_indx.elem(C_sorted_indx.elem(ii ) ); + iB = B_uniq_indx.elem(C_sorted_indx.elem(ii+1) - A_uniq.n_elem); + } + } + + + +//! @} -- cgit v1.2.1