Skip to content

Commit

Permalink
[ABSL] Use flat_hash_set instead of unordered_set. (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
pifon2a committed Oct 8, 2018
1 parent bdb6f2d commit 5e11365
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 37 deletions.
1 change: 1 addition & 0 deletions cartographer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ cc_library(
"@com_google_absl//absl/base",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:optional",
"@com_google_glog//:glog",
Expand Down
2 changes: 1 addition & 1 deletion cartographer/io/fixed_ratio_sampling_points_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ FixedRatioSamplingPointsProcessor::FixedRatioSamplingPointsProcessor(

void FixedRatioSamplingPointsProcessor::Process(
std::unique_ptr<PointsBatch> batch) {
std::unordered_set<int> to_remove;
absl::flat_hash_set<int> to_remove;
for (size_t i = 0; i < batch->points.size(); ++i) {
if (!sampler_->Pulse()) {
to_remove.insert(i);
Expand Down
8 changes: 4 additions & 4 deletions cartographer/io/frame_id_filtering_points_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ FrameIdFilteringPointsProcessor::FromDictionary(
dictionary->GetDictionary("drop_frames")->GetArrayValuesAsStrings();
}
return absl::make_unique<FrameIdFilteringPointsProcessor>(
std::unordered_set<std::string>(keep_frames.begin(), keep_frames.end()),
std::unordered_set<std::string>(drop_frames.begin(), drop_frames.end()),
absl::flat_hash_set<std::string>(keep_frames.begin(), keep_frames.end()),
absl::flat_hash_set<std::string>(drop_frames.begin(), drop_frames.end()),
next);
}

FrameIdFilteringPointsProcessor::FrameIdFilteringPointsProcessor(
const std::unordered_set<std::string>& keep_frame_ids,
const std::unordered_set<std::string>& drop_frame_ids,
const absl::flat_hash_set<std::string>& keep_frame_ids,
const absl::flat_hash_set<std::string>& drop_frame_ids,
PointsProcessor* next)
: keep_frame_ids_(keep_frame_ids),
drop_frame_ids_(drop_frame_ids),
Expand Down
11 changes: 5 additions & 6 deletions cartographer/io/frame_id_filtering_points_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#ifndef CARTOGRAPHER_IO_FRAME_ID_FILTERING_POINTS_PROCESSOR_H_
#define CARTOGRAPHER_IO_FRAME_ID_FILTERING_POINTS_PROCESSOR_H_

#include <unordered_set>

#include "absl/container/flat_hash_set.h"
#include "cartographer/common/lua_parameter_dictionary.h"
#include "cartographer/io/points_processor.h"

Expand All @@ -32,8 +31,8 @@ class FrameIdFilteringPointsProcessor : public PointsProcessor {
public:
constexpr static const char* kConfigurationFileActionName = "frame_id_filter";
FrameIdFilteringPointsProcessor(
const std::unordered_set<std::string>& keep_frame_ids,
const std::unordered_set<std::string>& drop_frame_ids,
const absl::flat_hash_set<std::string>& keep_frame_ids,
const absl::flat_hash_set<std::string>& drop_frame_ids,
PointsProcessor* next);
static std::unique_ptr<FrameIdFilteringPointsProcessor> FromDictionary(
common::LuaParameterDictionary* dictionary, PointsProcessor* next);
Expand All @@ -48,8 +47,8 @@ class FrameIdFilteringPointsProcessor : public PointsProcessor {
FlushResult Flush() override;

private:
const std::unordered_set<std::string> keep_frame_ids_;
const std::unordered_set<std::string> drop_frame_ids_;
const absl::flat_hash_set<std::string> keep_frame_ids_;
const absl::flat_hash_set<std::string> drop_frame_ids_;
PointsProcessor* const next_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ MinMaxRangeFiteringPointsProcessor::MinMaxRangeFiteringPointsProcessor(

void MinMaxRangeFiteringPointsProcessor::Process(
std::unique_ptr<PointsBatch> batch) {
std::unordered_set<int> to_remove;
absl::flat_hash_set<int> to_remove;
for (size_t i = 0; i < batch->points.size(); ++i) {
const float range = (batch->points[i].position - batch->origin).norm();
if (!(min_range_ <= range && range <= max_range_)) {
Expand Down
2 changes: 1 addition & 1 deletion cartographer/io/outlier_removing_points_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void OutlierRemovingPointsProcessor::ProcessInPhaseTwo(
void OutlierRemovingPointsProcessor::ProcessInPhaseThree(
std::unique_ptr<PointsBatch> batch) {
constexpr double kMissPerHitLimit = 3;
std::unordered_set<int> to_remove;
absl::flat_hash_set<int> to_remove;
for (size_t i = 0; i < batch->points.size(); ++i) {
const VoxelData voxel =
voxels_.value(voxels_.GetCellIndex(batch->points[i].position));
Expand Down
2 changes: 1 addition & 1 deletion cartographer/io/pbstream_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

#include <functional>
#include <unordered_set>

#include "absl/container/flat_hash_set.h"
#include "cartographer/io/internal/pbstream_info.h"
#include "cartographer/io/internal/pbstream_migrate.h"
#include "gflags/gflags.h"
Expand Down
2 changes: 1 addition & 1 deletion cartographer/io/points_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace cartographer {
namespace io {

void RemovePoints(std::unordered_set<int> to_remove, PointsBatch* batch) {
void RemovePoints(absl::flat_hash_set<int> to_remove, PointsBatch* batch) {
const int new_num_points = batch->points.size() - to_remove.size();
sensor::PointCloud points;
points.reserve(new_num_points);
Expand Down
4 changes: 2 additions & 2 deletions cartographer/io/points_batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include <array>
#include <cstdint>
#include <unordered_set>
#include <vector>

#include "Eigen/Core"
#include "absl/container/flat_hash_set.h"
#include "cartographer/common/time.h"
#include "cartographer/io/color.h"
#include "cartographer/sensor/point_cloud.h"
Expand Down Expand Up @@ -67,7 +67,7 @@ struct PointsBatch {
};

// Removes the indices in 'to_remove' from 'batch'.
void RemovePoints(std::unordered_set<int> to_remove, PointsBatch* batch);
void RemovePoints(absl::flat_hash_set<int> to_remove, PointsBatch* batch);

} // namespace io
} // namespace cartographer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CollatedTrajectoryBuilder::CollatedTrajectoryBuilder(
trajectory_id_(trajectory_id),
wrapped_trajectory_builder_(std::move(wrapped_trajectory_builder)),
last_logging_time_(std::chrono::steady_clock::now()) {
std::unordered_set<std::string> expected_sensor_id_strings;
absl::flat_hash_set<std::string> expected_sensor_id_strings;
for (const auto& sensor_id : expected_sensor_ids) {
if (sensor_id.type == SensorId::SensorType::LANDMARK &&
!collate_landmarks_) {
Expand Down
2 changes: 1 addition & 1 deletion cartographer/mapping/internal/connected_components.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include "cartographer/mapping/internal/connected_components.h"

#include <algorithm>
#include <unordered_set>

#include "absl/container/flat_hash_set.h"
#include "cartographer/mapping/proto/connected_components.pb.h"
#include "glog/logging.h"

Expand Down
4 changes: 2 additions & 2 deletions cartographer/sensor/collator_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

#include <functional>
#include <memory>
#include <unordered_set>
#include <vector>

#include "absl/container/flat_hash_set.h"
#include "absl/types/optional.h"
#include "cartographer/sensor/data.h"

Expand All @@ -42,7 +42,7 @@ class CollatorInterface {
// for each collated sensor data.
virtual void AddTrajectory(
int trajectory_id,
const std::unordered_set<std::string>& expected_sensor_ids,
const absl::flat_hash_set<std::string>& expected_sensor_ids,
const Callback& callback) = 0;

// Marks 'trajectory_id' as finished.
Expand Down
2 changes: 1 addition & 1 deletion cartographer/sensor/internal/collator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace sensor {

void Collator::AddTrajectory(
const int trajectory_id,
const std::unordered_set<std::string>& expected_sensor_ids,
const absl::flat_hash_set<std::string>& expected_sensor_ids,
const Callback& callback) {
for (const auto& sensor_id : expected_sensor_ids) {
const auto queue_key = QueueKey{trajectory_id, sensor_id};
Expand Down
9 changes: 5 additions & 4 deletions cartographer/sensor/internal/collator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include <functional>
#include <memory>
#include <unordered_set>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "cartographer/sensor/collator_interface.h"
#include "cartographer/sensor/data.h"
#include "cartographer/sensor/internal/ordered_multi_queue.h"
Expand All @@ -37,9 +37,10 @@ class Collator : public CollatorInterface {
Collator(const Collator&) = delete;
Collator& operator=(const Collator&) = delete;

void AddTrajectory(int trajectory_id,
const std::unordered_set<std::string>& expected_sensor_ids,
const Callback& callback) override;
void AddTrajectory(
int trajectory_id,
const absl::flat_hash_set<std::string>& expected_sensor_ids,
const Callback& callback) override;

void FinishTrajectory(int trajectory_id) override;

Expand Down
6 changes: 3 additions & 3 deletions cartographer/sensor/internal/collator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TEST(Collator, Ordering) {
Collator collator;
collator.AddTrajectory(
kTrajectoryId,
std::unordered_set<std::string>(kSensorId.begin(), kSensorId.end()),
absl::flat_hash_set<std::string>(kSensorId.begin(), kSensorId.end()),
[&received, kTrajectoryId](const std::string& sensor_id,
std::unique_ptr<Data> data) {
received.push_back(CollatorOutput(kTrajectoryId, data->GetSensorId(),
Expand Down Expand Up @@ -134,15 +134,15 @@ TEST(Collator, OrderingMultipleTrajectories) {
Collator collator;
collator.AddTrajectory(
kTrajectoryId[0],
std::unordered_set<std::string>(kSensorId.begin(), kSensorId.end()),
absl::flat_hash_set<std::string>(kSensorId.begin(), kSensorId.end()),
[&received, kTrajectoryId](const std::string& sensor_id,
std::unique_ptr<Data> data) {
received.push_back(CollatorOutput(kTrajectoryId[0], data->GetSensorId(),
data->GetTime()));
});
collator.AddTrajectory(
kTrajectoryId[1],
std::unordered_set<std::string>(kSensorId.begin(), kSensorId.end()),
absl::flat_hash_set<std::string>(kSensorId.begin(), kSensorId.end()),
[&received, kTrajectoryId](const std::string& sensor_id,
std::unique_ptr<Data> data) {
received.push_back(CollatorOutput(kTrajectoryId[1], data->GetSensorId(),
Expand Down
2 changes: 1 addition & 1 deletion cartographer/sensor/internal/trajectory_collator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ metrics::Family<metrics::Counter>*

void TrajectoryCollator::AddTrajectory(
const int trajectory_id,
const std::unordered_set<std::string>& expected_sensor_ids,
const absl::flat_hash_set<std::string>& expected_sensor_ids,
const Callback& callback) {
CHECK_EQ(trajectory_to_queue_.count(trajectory_id), 0);
for (const auto& sensor_id : expected_sensor_ids) {
Expand Down
7 changes: 4 additions & 3 deletions cartographer/sensor/internal/trajectory_collator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ class TrajectoryCollator : public CollatorInterface {
TrajectoryCollator(const TrajectoryCollator&) = delete;
TrajectoryCollator& operator=(const TrajectoryCollator&) = delete;

void AddTrajectory(int trajectory_id,
const std::unordered_set<std::string>& expected_sensor_ids,
const Callback& callback) override;
void AddTrajectory(
int trajectory_id,
const absl::flat_hash_set<std::string>& expected_sensor_ids,
const Callback& callback) override;

void FinishTrajectory(int trajectory_id) override;

Expand Down
4 changes: 2 additions & 2 deletions cartographer/sensor/internal/trajectory_collator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ TEST(TrajectoryCollator, OrderingMultipleTrajectories) {
TrajectoryCollator collator;
collator.AddTrajectory(
kTrajectoryId[0],
std::unordered_set<std::string>(kSensorId.begin(), kSensorId.end()),
absl::flat_hash_set<std::string>(kSensorId.begin(), kSensorId.end()),
[&received, kTrajectoryId](const std::string& sensor_id,
std::unique_ptr<Data> data) {
received.push_back(CollatorOutput(kTrajectoryId[0], data->GetSensorId(),
data->GetTime()));
});
collator.AddTrajectory(
kTrajectoryId[1],
std::unordered_set<std::string>(kSensorId.begin(), kSensorId.end()),
absl::flat_hash_set<std::string>(kSensorId.begin(), kSensorId.end()),
[&received, kTrajectoryId](const std::string& sensor_id,
std::unique_ptr<Data> data) {
received.push_back(CollatorOutput(kTrajectoryId[1], data->GetSensorId(),
Expand Down
4 changes: 2 additions & 2 deletions cartographer/sensor/internal/voxel_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#define CARTOGRAPHER_SENSOR_INTERNAL_VOXEL_FILTER_H_

#include <bitset>
#include <unordered_set>

#include "absl/container/flat_hash_set.h"
#include "cartographer/common/lua_parameter_dictionary.h"
#include "cartographer/sensor/point_cloud.h"
#include "cartographer/sensor/proto/adaptive_voxel_filter_options.pb.h"
Expand Down Expand Up @@ -58,7 +58,7 @@ class VoxelFilter {
Eigen::Array3i GetCellIndex(const Eigen::Vector3f& point) const;

float resolution_;
std::unordered_set<KeyType> voxel_set_;
absl::flat_hash_set<KeyType> voxel_set_;
};

proto::AdaptiveVoxelFilterOptions CreateAdaptiveVoxelFilterOptions(
Expand Down

0 comments on commit 5e11365

Please sign in to comment.