From: Carl Hetherington Date: Mon, 16 May 2022 19:37:53 +0000 (+0200) Subject: Add overlap() for lists of time periods. X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=eaee1cd5601a93037bc2fb0956b99b12fe1f90e6 Add overlap() for lists of time periods. --- diff --git a/src/lib/dcpomatic_time_overlap.h b/src/lib/dcpomatic_time_overlap.h new file mode 100644 index 000000000..dabeb98f3 --- /dev/null +++ b/src/lib/dcpomatic_time_overlap.h @@ -0,0 +1,47 @@ +/* + Copyright (C) 2022 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + + +#include "dcpomatic_time.h" +#include "dcpomatic_time_coalesce.h" + + +namespace dcpomatic { + + +template +std::list> overlap (std::list> const & A, std::list> const& B) +{ + std::list> result; + + for (auto i: A) { + for (auto j: B) { + if (auto ov = i.overlap(j)) { + result.push_back(*ov); + } + } + } + + return coalesce (result); +} + + +} + diff --git a/test/dcpomatic_time_test.cc b/test/dcpomatic_time_test.cc index 21739d185..80e0a9893 100644 --- a/test/dcpomatic_time_test.cc +++ b/test/dcpomatic_time_test.cc @@ -27,6 +27,7 @@ #include "lib/dcpomatic_time.h" #include "lib/dcpomatic_time_coalesce.h" +#include "lib/dcpomatic_time_overlap.h" #include #include #include @@ -427,3 +428,109 @@ BOOST_AUTO_TEST_CASE (dcpomatic_time_floor_test) /* Check that rounding down to non-integer frame rates works */ BOOST_CHECK_EQUAL (DCPTime(45312).floor(29.976).get(), 44836); } + + +BOOST_AUTO_TEST_CASE (dcpomatic_time_overlap_test1) +{ + list A = { + { DCPTime(0), DCPTime(5) } + }; + + BOOST_CHECK (overlap(A, std::list()).empty()); + BOOST_CHECK (overlap(std::list(), A).empty()); +} + + +BOOST_AUTO_TEST_CASE (dcpomatic_time_overlap_test2) +{ + list A = { + { DCPTime(0), DCPTime(5) } + }; + + list B = { + { DCPTime(6), DCPTime(10) } + }; + + BOOST_CHECK (overlap(A, B).empty()); + BOOST_CHECK (overlap(B, A).empty()); +} + + +BOOST_AUTO_TEST_CASE (dcpomatic_time_overlap_test3) +{ + list A = { + { DCPTime(0), DCPTime(5) } + }; + + list B = { + { DCPTime(2), DCPTime(3) } + }; + + std::list correct = { + { DCPTime(2), DCPTime(3) } + }; + + BOOST_CHECK (overlap(A, B) == correct); + BOOST_CHECK (overlap(B, A) == correct); +} + + +BOOST_AUTO_TEST_CASE (dcpomatic_time_overlap_test4) +{ + list A = { + { DCPTime(0), DCPTime(5) } + }; + + list B = { + { DCPTime(0), DCPTime(5) } + }; + + std::list correct = { + { DCPTime(0), DCPTime(5) } + }; + + BOOST_CHECK (overlap(A, B) == correct); + BOOST_CHECK (overlap(B, A) == correct); +} + + +BOOST_AUTO_TEST_CASE (dcpomatic_time_overlap_test5) +{ + list A = { + { DCPTime(0), DCPTime(5) }, + { DCPTime(7), DCPTime(12) } + }; + + list B = { + { DCPTime(0), DCPTime(15) } + }; + + std::list correct = { + { DCPTime(0), DCPTime(5) }, + { DCPTime(7), DCPTime(12) } + }; + + BOOST_CHECK (overlap(A, B) == correct); + BOOST_CHECK (overlap(B, A) == correct); +} + + +BOOST_AUTO_TEST_CASE (dcpomatic_time_overlap_test6) +{ + list A = { + { DCPTime(0), DCPTime(5) }, + { DCPTime(7), DCPTime(12) } + }; + + list B = { + { DCPTime(3), DCPTime(10) } + }; + + std::list correct = { + { DCPTime(3), DCPTime(5) }, + { DCPTime(7), DCPTime(10) } + }; + + BOOST_CHECK (overlap(A, B) == correct); + BOOST_CHECK (overlap(B, A) == correct); +}