Skip to content

Commit

Permalink
Rename all odd visitor functions to proper observers
Browse files Browse the repository at this point in the history
  • Loading branch information
lczech committed Jan 24, 2024
1 parent 84d4f23 commit e62934b
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 91 deletions.
8 changes: 4 additions & 4 deletions lib/genesis/population/functions/variant_input_iterator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Genesis - A toolkit for working with phylogenetic data.
Copyright (C) 2014-2023 Lucas Czech
Copyright (C) 2014-2024 Lucas Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -134,10 +134,10 @@ std::function<void(Variant&)> make_variant_input_iterator_sample_name_filter_tra
}

// =================================================================================================
// Visitors
// Observers
// =================================================================================================

std::function<void(Variant const&)> make_variant_input_iterator_sequence_order_visitor(
std::function<void(Variant const&)> make_variant_input_iterator_sequence_order_observer(
std::shared_ptr<genesis::sequence::SequenceDict> sequence_dict,
bool check_sequence_lengths
) {
Expand Down Expand Up @@ -191,7 +191,7 @@ std::function<void(Variant const&)> make_variant_input_iterator_sequence_order_v
};
}

std::function<void(Variant const&)> make_variant_input_iterator_sequence_length_visitor(
std::function<void(Variant const&)> make_variant_input_iterator_sequence_length_observer(
std::shared_ptr<genesis::sequence::SequenceDict> sequence_dict
) {
return [ sequence_dict ]( Variant const& variant ) {
Expand Down
20 changes: 10 additions & 10 deletions lib/genesis/population/functions/variant_input_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
Genesis - A toolkit for working with phylogenetic data.
Copyright (C) 2014-2023 Lucas Czech
Copyright (C) 2014-2024 Lucas Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -87,7 +87,7 @@ std::function<void(Variant&)> make_variant_input_iterator_sample_name_filter_tra
);

// =================================================================================================
// Visitors
// Observers
// =================================================================================================

/**
Expand Down Expand Up @@ -123,9 +123,9 @@ std::function<void(Variant&)> make_variant_input_iterator_sample_name_filter_tra
* // Create a VariantInputIterator, for example from a sync file
* auto variant_iterator = make_variant_input_iterator_from_sync_file( sync_file );
*
* // Add the visitor that checks order, using a
* variant_iterator.add_visitor(
* make_variant_input_iterator_sequence_order_visitor(
* // Add the observer that checks order, using a
* variant_iterator.add_observer(
* make_variant_input_iterator_sequence_order_observer(
* sequence_dict, true
* )
* );
Expand All @@ -135,7 +135,7 @@ std::function<void(Variant&)> make_variant_input_iterator_sample_name_filter_tra
* // ...
* }
*
* @see See @link ::genesis::utils::LambdaIterator::add_visitor() LambdaIterator::add_visitor()@endlink
* @see See @link ::genesis::utils::LambdaIterator::add_observer() LambdaIterator::add_observer()@endlink
* for the function of the underlying iterator that accepts the returned function from here.
*
* @see make_variant_input_iterator_from_vector(),
Expand All @@ -147,7 +147,7 @@ std::function<void(Variant&)> make_variant_input_iterator_sample_name_filter_tra
* make_variant_input_iterator_from_individual_vcf_file(),
* make_variant_input_iterator_from_variant_parallel_input_iterator()
*/
std::function<void(Variant const&)> make_variant_input_iterator_sequence_order_visitor(
std::function<void(Variant const&)> make_variant_input_iterator_sequence_order_observer(
std::shared_ptr<genesis::sequence::SequenceDict> sequence_dict = {},
bool check_sequence_lengths = true
);
Expand All @@ -156,13 +156,13 @@ std::function<void(Variant const&)> make_variant_input_iterator_sequence_order_v
* @brief Helper function to check that some Variant input has positions that agree with those
* reported in a SequenceDict.
*
* Similar to make_variant_input_iterator_sequence_order_visitor(), but without the sequence order
* Similar to make_variant_input_iterator_sequence_order_observer(), but without the sequence order
* check. Meant for situations where this order check is either not necessary, or already done in
* some other way, for example in a VariantParallelInputIterator.
*
* See make_variant_input_iterator_sequence_order_visitor() for details on usage.
* See make_variant_input_iterator_sequence_order_observer() for details on usage.
*/
std::function<void(Variant const&)> make_variant_input_iterator_sequence_length_visitor(
std::function<void(Variant const&)> make_variant_input_iterator_sequence_length_observer(
std::shared_ptr<genesis::sequence::SequenceDict> sequence_dict
);

Expand Down
34 changes: 17 additions & 17 deletions lib/genesis/population/window/base_window_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
Genesis - A toolkit for working with phylogenetic data.
Copyright (C) 2014-2023 Lucas Czech
Copyright (C) 2014-2024 Lucas Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -203,9 +203,9 @@ class BaseWindowIterator
Iterator( std::unique_ptr<BaseIterator> base_iterator )
: pimpl_( std::move( base_iterator ))
{
// Visit the first element, if this is an active iterator.
// Observe the first element, if this is an active iterator.
assert( pimpl_ );
execute_visitors_();
execute_observers_();
}

public:
Expand Down Expand Up @@ -300,10 +300,10 @@ class BaseWindowIterator

self_type& operator ++()
{
// Advance to the next element, and visit it.
// Advance to the next element, and observe it.
assert( pimpl_ );
pimpl_->increment_();
execute_visitors_();
execute_observers_();
return *this;
}

Expand Down Expand Up @@ -344,15 +344,15 @@ class BaseWindowIterator

private:

void execute_visitors_()
void execute_observers_()
{
// If there is still a parent, we are active,
// and execute all visitors for the element.
// and execute all observers for the element.
assert( pimpl_ );
if( pimpl_->get_parent_() ) {
auto& element = pimpl_->get_current_window_();
for( auto const& visitor : pimpl_->get_parent_()->visitors_ ) {
visitor( element );
for( auto const& observer : pimpl_->get_parent_()->observers_ ) {
observer( element );
}
}
}
Expand Down Expand Up @@ -538,11 +538,11 @@ class BaseWindowIterator
friend Iterator;

// -------------------------------------------------------------------------
// Visitors
// Observers
// -------------------------------------------------------------------------

/**
* @brief Add a visitor function that is executed once for each window during the iteration.
* @brief Add a observer function that is executed once for each window during the iteration.
*
* These functions are executed when starting and incrementing the iterator, once for each
* window, in the order in which they are added here. They take the window (typically of type
Expand All @@ -553,18 +553,18 @@ class BaseWindowIterator
* in the beginning of the loop body of the user code. Still, offering this here can reduce
* redundant code, such as logging Windows during the iteration.
*/
self_type& add_visitor( std::function<void(WindowType const&)> const& visitor )
self_type& add_observer( std::function<void(WindowType const&)> const& observer )
{
visitors_.push_back( visitor );
observers_.push_back( observer );
return *this;
}

/**
* @brief Clear all functions that are executed on incrementing to the next element.
*/
self_type& clear_visitors()
self_type& clear_observers()
{
visitors_.clear();
observers_.clear();
return *this;
}

Expand Down Expand Up @@ -604,8 +604,8 @@ class BaseWindowIterator
InputIterator begin_;
InputIterator end_;

// Keep the visitors for each window view.
std::vector<std::function<void(WindowType const&)>> visitors_;
// Keep the observers for each window view.
std::vector<std::function<void(WindowType const&)>> observers_;

};

Expand Down
6 changes: 3 additions & 3 deletions lib/genesis/utils/containers/lambda_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
Genesis - A toolkit for working with phylogenetic data.
Copyright (C) 2014-2023 Lucas Czech
Copyright (C) 2014-2024 Lucas Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -918,7 +918,7 @@ class LambdaIterator
}

// -------------------------------------------------------------------------
// Visitors and Callbacks
// Observers and Callbacks
// -------------------------------------------------------------------------

/**
Expand Down Expand Up @@ -1081,7 +1081,7 @@ class LambdaIterator

// We have two different types of functions that we accept to operate on the data:
// the transforms and filters are executed when filling the buffers,
// while the visits are executed once the iteration reaches the respective element.
// while the observers are executed once the iteration reaches the respective element.
std::vector<std::function<bool(T&)>> transforms_and_filters_;
std::vector<std::function<void(T const&)>> observers_;

Expand Down
14 changes: 7 additions & 7 deletions test/src/population/region_window_iterator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Genesis - A toolkit for working with phylogenetic data.
Copyright (C) 2014-2023 Lucas Czech
Copyright (C) 2014-2024 Lucas Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -186,11 +186,11 @@ void run_region_window_test_(
variants.begin(), variants.end(), region_list
);

// Also test that the visitor functions get executed once per window.
size_t visit_cnt = 0;
win_it.add_visitor( [&visit_cnt]( Window<Variant> const& ){
// LOG_DBG << "at " << visit_cnt;
++visit_cnt;
// Also test that the observer functions get executed once per window.
size_t observe_cnt = 0;
win_it.add_observer( [&observe_cnt]( Window<Variant> const& ){
// LOG_DBG << "at " << observe_cnt;
++observe_cnt;
});

// ...and go through it.
Expand Down Expand Up @@ -382,7 +382,7 @@ void run_region_window_test_(
EXPECT_EQ( exp_var_total_cnt_2, var_total_cnt );
EXPECT_EQ( regions_with_vars, window_var_cnt );
EXPECT_EQ( window_cnt_target, window_tot_cnt );
EXPECT_EQ( window_cnt_target, visit_cnt );
EXPECT_EQ( window_cnt_target, observe_cnt );
if( ! skip_empty_regions ) {
EXPECT_EQ( region_list->total_region_count(), window_tot_cnt );
}
Expand Down
38 changes: 19 additions & 19 deletions test/src/population/sliding_entries_window_iterator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Genesis - A toolkit for working with phylogenetic data.
Copyright (C) 2014-2023 Lucas Czech
Copyright (C) 2014-2024 Lucas Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -50,12 +50,12 @@ void test_sliding_entries_iterator_( WindowIterator& win_it, size_t count )
bool found_first_win = false;
bool found_last_win = false;

// Also test that the visitor functions get executed once per window.
size_t visit_cnt = 0;
// Also test that the observer functions get executed once per window.
size_t observe_cnt = 0;
using ValueType = typename WindowIterator::InputIteratorType::value_type;
win_it.add_visitor( [&visit_cnt]( Window<ValueType> const& ){
// LOG_DBG << "at " << visit_cnt;
++visit_cnt;
win_it.add_observer( [&observe_cnt]( Window<ValueType> const& ){
// LOG_DBG << "at " << observe_cnt;
++observe_cnt;
});

// DBG 2R : 7790001 7790001-7800000 # 1
Expand Down Expand Up @@ -133,7 +133,7 @@ void test_sliding_entries_iterator_( WindowIterator& win_it, size_t count )
++window_cnt;
}
EXPECT_EQ( window_sizes.size(), window_cnt );
EXPECT_EQ( window_sizes.size(), visit_cnt );
EXPECT_EQ( window_sizes.size(), observe_cnt );

EXPECT_TRUE( found_first_win );
EXPECT_TRUE( found_last_win );
Expand Down Expand Up @@ -249,16 +249,16 @@ TEST( WindowIterator, SlidingEntriesWindowView )
pileup_begin, pileup_end, 9000
);

// Also test that the visitor functions get executed once per window.
size_t visit_cnt = 0;
win_it.add_visitor( [&visit_cnt]( WindowView<Variant> const& ){
// LOG_DBG << "at " << visit_cnt;
++visit_cnt;
// Also test that the observer functions get executed once per window.
size_t observe_cnt = 0;
win_it.add_observer( [&observe_cnt]( WindowView<Variant> const& ){
// LOG_DBG << "at " << observe_cnt;
++observe_cnt;
});

// We use a test function that takes our abstract type, to see if we set this up correctly.
run_sliding_entries_window_view_variant_test_( win_it );
EXPECT_EQ( 6, visit_cnt );
EXPECT_EQ( 6, observe_cnt );

// size_t window_cnt = 0;
// for( auto it = win_it.begin(); it != win_it.end(); ++it ) {
Expand All @@ -283,11 +283,11 @@ TEST( WindowIterator, SlidingEntriesEmpty )
pileup_begin, pileup_end, 10000
);

// Also test that the visitor functions get executed once per window.
size_t visit_cnt = 0;
win_it.add_visitor( [&visit_cnt]( Window<Variant> const& ){
// LOG_DBG << "at " << visit_cnt;
++visit_cnt;
// Also test that the observer functions get executed once per window.
size_t observe_cnt = 0;
win_it.add_observer( [&observe_cnt]( Window<Variant> const& ){
// LOG_DBG << "at " << observe_cnt;
++observe_cnt;
});

size_t window_cnt = 0;
Expand All @@ -304,5 +304,5 @@ TEST( WindowIterator, SlidingEntriesEmpty )
++window_cnt;
}
EXPECT_EQ( 0, window_cnt );
EXPECT_EQ( 0, visit_cnt );
EXPECT_EQ( 0, observe_cnt );
}
Loading

0 comments on commit e62934b

Please sign in to comment.