Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 17971ba7bf48
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d9c48a201687
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on May 2, 2021

  1. Copy the full SHA
    11675d6 View commit details
  2. Merge pull request #2969 from pleroy/Checkpointer

    More functions in the Checkpointer
    pleroy authored May 2, 2021
    Copy the full SHA
    d9c48a2 View commit details
Showing with 48 additions and 0 deletions.
  1. +8 −0 physics/checkpointer.hpp
  2. +25 −0 physics/checkpointer_body.hpp
  3. +15 −0 physics/checkpointer_test.cpp
8 changes: 8 additions & 0 deletions physics/checkpointer.hpp
Original file line number Diff line number Diff line change
@@ -69,6 +69,11 @@ class Checkpointer {
std::set<Instant> all_checkpoints_at_or_before(Instant const& t) const
EXCLUDES(lock_);

// Returns all the checkpoints in interval [t1, t2].
std::set<Instant> all_checkpoints_between(Instant const& t1,
Instant const& t2) const
EXCLUDES(lock_);

// Creates a checkpoint at time |t|, which will be used to recreate the
// timeline after |t|. The checkpoint is constructed by calling the |Writer|
// passed at construction.
@@ -101,6 +106,9 @@ class Checkpointer {
Status ReadFromCheckpointAt(Instant const& t,
Reader const& reader) const EXCLUDES(lock_);

// Same as above, but uses the reader passed at construction.
Status ReadFromCheckpointAt(Instant const& t) const EXCLUDES(lock_);

void WriteToMessage(not_null<google::protobuf::RepeatedPtrField<
typename Message::Checkpoint>*> message) const
EXCLUDES(lock_);
25 changes: 25 additions & 0 deletions physics/checkpointer_body.hpp
Original file line number Diff line number Diff line change
@@ -83,6 +83,26 @@ std::set<Instant> Checkpointer<Message>::all_checkpoints_at_or_before(
return result;
}

template<typename Message>
std::set<Instant> Checkpointer<Message>::all_checkpoints_between(
Instant const& t1,
Instant const& t2) const {
absl::ReaderMutexLock l(&lock_);
// |it1| denotes an entry greater or equal to |t1| (or end).
auto const it1 = checkpoints_.lower_bound(t1);
// |it2| denotes an entry strictly greater than |t2| (or end).
auto const it2 = checkpoints_.upper_bound(t2);
std::set<Instant> result;
std::transform(
it1,
it2,
std::inserter(result, result.end()),
[](std::pair<Instant, typename Message::Checkpoint> const& pair) {
return pair.first;
});
return result;
}

template<typename Message>
void Checkpointer<Message>::WriteToCheckpoint(Instant const& t) {
absl::MutexLock l(&lock_);
@@ -158,6 +178,11 @@ Status Checkpointer<Message>::ReadFromCheckpointAt(Instant const& t,
return reader(it->second);
}

template<typename Message>
Status Checkpointer<Message>::ReadFromCheckpointAt(Instant const& t) const {
return ReadFromCheckpointAt(t, reader_);
}

template<typename Message>
void Checkpointer<Message>::WriteToMessage(
not_null<google::protobuf::RepeatedPtrField<typename Message::Checkpoint>*>
15 changes: 15 additions & 0 deletions physics/checkpointer_test.cpp
Original file line number Diff line number Diff line change
@@ -148,6 +148,21 @@ TEST_F(CheckpointerTest, ReadFromCheckpointAtOrBefore) {
EXPECT_THAT(checkpointer_.all_checkpoints_at_or_before(t2 + 1 * Second),
ElementsAre(t1, t2));

EXPECT_THAT(checkpointer_.all_checkpoints_between(Instant() + 1 * Second,
Instant() + 3 * Second),
IsEmpty());
EXPECT_THAT(checkpointer_.all_checkpoints_between(Instant() + 1 * Second,
t1),
ElementsAre(t1));
EXPECT_THAT(checkpointer_.all_checkpoints_between(Instant() + 1 * Second,
t2 + 1 * Second),
ElementsAre(t1, t2));
EXPECT_THAT(checkpointer_.all_checkpoints_between(t1, t2),
ElementsAre(t1, t2));
EXPECT_THAT(checkpointer_.all_checkpoints_between(t1 - 1 * Second,
t2 + 1 * Second),
ElementsAre(t1, t2));

EXPECT_THAT(
checkpointer_.ReadFromCheckpointAtOrBefore(Instant() + 1 * Second),
StatusIs(Error::NOT_FOUND));