Skip to content

Commit

Permalink
[wpiutil] Replace LLVM StringMap impl with std::map
Browse files Browse the repository at this point in the history
As string_view operations on std::map<std::string> won't be integrated
until C++26, placeholder implementations are used which are less efficient
in a couple of situations (e.g. insert with hint).
  • Loading branch information
PeterJohnson committed Nov 1, 2024
1 parent 5f3cf51 commit f620141
Show file tree
Hide file tree
Showing 34 changed files with 932 additions and 2,019 deletions.
4 changes: 2 additions & 2 deletions apriltag/src/main/native/cpp/AprilTagDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void AprilTagDetector::RemoveFamily(std::string_view fam) {
apriltag_detector_remove_family(
static_cast<apriltag_detector_t*>(m_impl),
static_cast<apriltag_family_t*>(it->second));
DestroyFamily(it->getKey(), it->second);
DestroyFamily(it->first, it->second);
m_families.erase(it);
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ void AprilTagDetector::Destroy() {

void AprilTagDetector::DestroyFamilies() {
for (auto&& entry : m_families) {
DestroyFamily(entry.getKey(), entry.second);
DestroyFamily(entry.first, entry.second);
}
}

Expand Down
13 changes: 6 additions & 7 deletions glass/src/lib/native/cpp/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void WorkspaceInit() {
}

for (auto&& root : gContext->storageRoots) {
root.getValue()->Apply();
root.second->Apply();
}
}

Expand Down Expand Up @@ -176,7 +176,7 @@ static bool LoadStorageImpl(Context* ctx, std::string_view dir,
bool rv = true;
for (auto&& root : ctx->storageRoots) {
std::string filename;
auto rootName = root.getKey();
auto& rootName = root.first;
if (rootName.empty()) {
filename = (fs::path{dir} / fmt::format("{}.json", name)).string();
} else {
Expand Down Expand Up @@ -291,7 +291,7 @@ static bool SaveStorageImpl(Context* ctx, std::string_view dir,
if (exiting && wpi::gui::gContext->resetOnExit) {
fs::remove(dirPath / fmt::format("{}-window.json", name), ec);
for (auto&& root : ctx->storageRoots) {
auto rootName = root.getKey();
auto& rootName = root.first;
if (rootName.empty()) {
fs::remove(dirPath / fmt::format("{}.json", name), ec);
} else {
Expand All @@ -304,14 +304,14 @@ static bool SaveStorageImpl(Context* ctx, std::string_view dir,
(dirPath / fmt::format("{}-window.json", name)).string());

for (auto&& root : ctx->storageRoots) {
auto rootName = root.getKey();
auto& rootName = root.first;
std::string filename;
if (rootName.empty()) {
filename = (dirPath / fmt::format("{}.json", name)).string();
} else {
filename = (dirPath / fmt::format("{}-{}.json", name, rootName)).string();
}
if (!SaveStorageRootImpl(ctx, filename, *root.getValue())) {
if (!SaveStorageRootImpl(ctx, filename, *root.second)) {
rv = false;
}
}
Expand All @@ -320,8 +320,7 @@ static bool SaveStorageImpl(Context* ctx, std::string_view dir,

Context::Context()
: sourceNameStorage{storageRoots.insert({"", std::make_unique<Storage>()})
.first->getValue()
->GetChild("sourceNames")} {
.first->second->GetChild("sourceNames")} {
storageStack.emplace_back(storageRoots[""].get());

// override ImGui ini saving
Expand Down
2 changes: 1 addition & 1 deletion glass/src/lib/native/cpp/DataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@ DataSource* DataSource::Find(std::string_view id) {
if (it == gContext->sources.end()) {
return nullptr;
}
return it->getValue();
return it->second;
}
18 changes: 8 additions & 10 deletions glass/src/lib/native/cpp/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,17 @@ std::vector<std::unique_ptr<Storage>>& Storage::GetChildArray(
std::unique_ptr<Storage::Value> Storage::Erase(std::string_view key) {
auto it = m_values.find(key);
if (it != m_values.end()) {
auto rv = std::move(it->getValue());
auto rv = std::move(it->second);
m_values.erase(it);
return rv;
}
return nullptr;
}

void Storage::EraseChildren() {
for (auto&& kv : m_values) {
if (kv.getValue()->type == Value::kChild) {
m_values.remove(&kv);
}
}
std::erase_if(m_values, [](const auto& kv) {
return kv.second->type == Value::kChild;
});
}

static bool JsonArrayToStorage(Storage::Value* valuePtr, const wpi::json& jarr,
Expand Down Expand Up @@ -559,7 +557,7 @@ wpi::json Storage::ToJson() const {
wpi::json j = wpi::json::object();
for (auto&& kv : m_values) {
wpi::json jelem;
auto& value = *kv.getValue();
auto& value = *kv.second;
switch (value.type) {
#define CASE(CapsName, LowerName) \
case Value::k##CapsName: \
Expand Down Expand Up @@ -602,7 +600,7 @@ wpi::json Storage::ToJson() const {
default:
continue;
}
j.emplace(kv.getKey(), std::move(jelem));
j.emplace(kv.first, std::move(jelem));
}
return j;
}
Expand All @@ -617,7 +615,7 @@ void Storage::Clear() {

void Storage::ClearValues() {
for (auto&& kv : m_values) {
auto& value = *kv.getValue();
auto& value = *kv.second;
switch (value.type) {
case Value::kInt:
value.intVal = value.intDefault;
Expand Down Expand Up @@ -703,7 +701,7 @@ void Storage::Apply() {

void Storage::ApplyChildren() {
for (auto&& kv : m_values) {
auto& value = *kv.getValue();
auto& value = *kv.second;
switch (value.type) {
case Value::kChild:
value.child->Apply();
Expand Down
10 changes: 4 additions & 6 deletions glass/src/lib/native/include/glass/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ class ChildIterator {
public:
ChildIterator(IteratorType it, IteratorType end) noexcept
: anchor(it), end(end) {
while (anchor != end &&
anchor->getValue()->type != Storage::Value::kChild) {
while (anchor != end && anchor->second->type != Storage::Value::kChild) {
++anchor;
}
}
Expand All @@ -261,8 +260,7 @@ class ChildIterator {
/// increment operator (needed for range-based for)
ChildIterator& operator++() {
++anchor;
while (anchor != end &&
anchor->getValue()->type != Storage::Value::kChild) {
while (anchor != end && anchor->second->type != Storage::Value::kChild) {
++anchor;
}
return *this;
Expand All @@ -274,10 +272,10 @@ class ChildIterator {
}

/// return key of the iterator
std::string_view key() const { return anchor->getKey(); }
std::string_view key() const { return anchor->first; }

/// return value of the iterator
Storage& value() const { return *anchor->getValue()->child; }
Storage& value() const { return *anchor->second->child; }
};

} // namespace detail
Expand Down
6 changes: 3 additions & 3 deletions hal/src/main/native/sim/mockdata/SimDeviceData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ HAL_SimDeviceHandle SimDeviceData::GetDeviceHandle(const char* name) {
if (it == m_deviceMap.end()) {
return 0;
}
if (auto deviceImpl = it->getValue().lock()) {
if (auto deviceImpl = it->second.lock()) {
return deviceImpl->handle;
} else {
return 0;
Expand Down Expand Up @@ -459,10 +459,10 @@ HAL_SimValueHandle SimDeviceData::GetValueHandle(HAL_SimDeviceHandle device,
if (it == deviceImpl->valueMap.end()) {
return 0;
}
if (!it->getValue()) {
if (!it->second) {
return 0;
}
return it->getValue()->handle;
return it->second->handle;
}

void SimDeviceData::EnumerateValues(HAL_SimDeviceHandle device, void* param,
Expand Down
4 changes: 2 additions & 2 deletions simulation/halsim_ws_client/src/main/native/cpp/HALSimWS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ void HALSimWS::Start() {
// Print any filters we are using
if (m_useMsgFiltering) {
wpi::print("WS Message Filters:");
for (auto filter : m_msgFilters.keys()) {
wpi::print("* \"{}\"\n", filter);
for (auto&& filter : m_msgFilters) {
wpi::print("* \"{}\"\n", filter.first);
}
} else {
wpi::print("No WS Message Filters specified");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void HALSimWSProviderSimDevice::CancelCallbacks() {
m_simValueCreatedCbKey = 0;

for (auto& kv : m_simValueChangedCbKeys) {
HALSIM_CancelSimValueChangedCallback(kv.getValue());
HALSIM_CancelSimValueChangedCallback(kv.second);
}

m_simValueChangedCbKeys.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ProviderContainer {
void ForEach(IterFn fn) {
std::shared_lock lock(m_mutex);
for (auto& kv : m_providers) {
fn(kv.getValue());
fn(kv.second);
}
}

Expand Down
4 changes: 2 additions & 2 deletions simulation/halsim_ws_server/src/main/native/cpp/HALSimWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ void HALSimWeb::Start() {
// Print any filters we are using
if (m_useMsgFiltering) {
wpi::print("WS Message Filters:");
for (auto filter : m_msgFilters.keys()) {
wpi::print("* \"{}\"\n", filter);
for (auto&& filter : m_msgFilters) {
wpi::print("* \"{}\"\n", filter.first);
}
} else {
wpi::print("No WS Message Filters specified");
Expand Down
6 changes: 3 additions & 3 deletions sysid/src/main/native/cpp/analysis/AnalysisManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ static void CopyRawData(wpi::StringMap<MotorData>* dataset) {
auto& data = *dataset;
// Loads the Raw Data
for (auto& it : data) {
auto key = it.first();
auto& motorData = it.getValue();
auto& key = it.first;
auto& motorData = it.second;

if (!wpi::contains(key, "raw")) {
data[fmt::format("raw-{}", key)] = motorData;
Expand Down Expand Up @@ -126,7 +126,7 @@ void AnalysisManager::PrepareGeneralData() {
WPI_INFO(m_logger, "{}", "Converting raw data to PreparedData struct.");
// Convert data to PreparedData structs
for (auto& it : m_data.motorData) {
auto key = it.first();
auto key = it.first;
preparedData[key] = ConvertToPrepared(m_data.motorData[key]);
WPI_INFO(m_logger, "SAMPLES {}", preparedData[key].size());
}
Expand Down
16 changes: 8 additions & 8 deletions sysid/src/main/native/cpp/analysis/FilteringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ static units::second_t GetMaxStepTime(
wpi::StringMap<std::vector<PreparedData>>& data) {
auto maxStepTime = 0_s;
for (auto& it : data) {
auto key = it.first();
auto& dataset = it.getValue();
auto& key = it.first;
auto& dataset = it.second;

if (IsRaw(key) && wpi::contains(key, "dynamic")) {
if (!dataset.empty()) {
Expand Down Expand Up @@ -351,8 +351,8 @@ void sysid::InitialTrimAndFilter(
// Calculate Velocity Threshold if it hasn't been set yet
if (settings->velocityThreshold == std::numeric_limits<double>::infinity()) {
for (auto& it : preparedData) {
auto key = it.first();
auto& dataset = it.getValue();
auto& key = it.first;
auto& dataset = it.second;
if (wpi::contains(key, "quasistatic")) {
settings->velocityThreshold =
std::min(settings->velocityThreshold,
Expand All @@ -363,8 +363,8 @@ void sysid::InitialTrimAndFilter(
}

for (auto& it : preparedData) {
auto key = it.first();
auto& dataset = it.getValue();
auto& key = it.first;
auto& dataset = it.second;

// Trim quasistatic test data to remove all points where voltage is zero or
// velocity < velocity threshold.
Expand Down Expand Up @@ -424,7 +424,7 @@ void sysid::AccelFilter(wpi::StringMap<std::vector<PreparedData>>* data) {

// Remove points with acceleration = 0
for (auto& it : preparedData) {
auto& dataset = it.getValue();
auto& dataset = it.second;

for (size_t i = 0; i < dataset.size(); i++) {
if (dataset.at(i).acceleration == 0.0) {
Expand All @@ -436,7 +436,7 @@ void sysid::AccelFilter(wpi::StringMap<std::vector<PreparedData>>* data) {

// Confirm there's still data
if (std::any_of(preparedData.begin(), preparedData.end(),
[](const auto& it) { return it.getValue().empty(); })) {
[](const auto& it) { return it.second.empty(); })) {
throw sysid::InvalidDataError(
"Acceleration filtering has removed all data.");
}
Expand Down
2 changes: 1 addition & 1 deletion sysid/src/main/native/cpp/view/DataSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void DataSelector::Display() {
TestData data = m_testdataFuture.get();
for (auto&& motordata : data.motorData) {
m_testdataStats.emplace_back(
fmt::format("Test State: {}", motordata.first()));
fmt::format("Test State: {}", motordata.first));
int i = 0;
for (auto&& run : motordata.second.runs) {
m_testdataStats.emplace_back(fmt::format(
Expand Down
1 change: 1 addition & 0 deletions sysid/src/main/native/cpp/view/LogLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <imgui.h>
#include <imgui_stdlib.h>
#include <portable-file-dialogs.h>
#include <wpi/SmallVector.h>
#include <wpi/SpanExtras.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
Expand Down
Loading

0 comments on commit f620141

Please sign in to comment.