@@ -756,36 +756,51 @@ template <typename T, typename TagT, typename LabelT> std::vector<uint32_t> Inde
756756}
757757
758758// Find common filter between a node's labels and a given set of labels, while
759- // taking into account universal label
759+ // taking into account universal label.
760+ // Note: incoming_labels should be sorted.
760761template <typename T, typename TagT, typename LabelT>
761762bool Index<T, TagT, LabelT>::detect_common_filters (uint32_t point_id, bool search_invocation,
762763 const std::vector<LabelT> &incoming_labels)
763764{
764765 auto &curr_node_labels = _location_to_labels[point_id];
765- std::vector<LabelT> common_filters;
766- std::set_intersection (incoming_labels.begin (), incoming_labels.end (), curr_node_labels.begin (),
767- curr_node_labels.end (), std::back_inserter (common_filters));
768- if (common_filters.size () > 0 )
766+ // Check for intersection between incoming_labels and curr_node_labels
767+ // using two-pointer approach (both vectors are sorted)
768+ auto it_inc = incoming_labels.begin ();
769+ auto it_curr = curr_node_labels.begin ();
770+
771+ while (it_inc != incoming_labels.end () && it_curr != curr_node_labels.end ())
769772 {
770- // This is to reduce the repetitive calls. If common_filters size is > 0 ,
771- // we dont need to check further for universal label
772- return true ;
773+ if (*it_inc < *it_curr)
774+ {
775+ ++it_inc;
776+ }
777+ else if (*it_curr < *it_inc)
778+ {
779+ ++it_curr;
780+ }
781+ else
782+ {
783+ // common label found
784+ return true ;
785+ }
773786 }
787+ // intersection empty; proceed to check the universal label logic
788+
774789 if (_use_universal_label)
775790 {
776791 if (!search_invocation)
777792 {
778793 if (std::find (incoming_labels.begin (), incoming_labels.end (), _universal_label) != incoming_labels.end () ||
779794 std::find (curr_node_labels.begin (), curr_node_labels.end (), _universal_label) != curr_node_labels.end ())
780- common_filters. push_back (_universal_label) ;
795+ return true ;
781796 }
782797 else
783798 {
784799 if (std::find (curr_node_labels.begin (), curr_node_labels.end (), _universal_label) != curr_node_labels.end ())
785- common_filters. push_back (_universal_label) ;
800+ return true ;
786801 }
787802 }
788- return (common_filters. size () > 0 ) ;
803+ return false ;
789804}
790805
791806template <typename T, typename TagT, typename LabelT>
0 commit comments