Skip to content

Commit 7f29d06

Browse files
committed
Use ordered map in DataTree to avoid extra sorting
1 parent a9477a2 commit 7f29d06

4 files changed

Lines changed: 9 additions & 38 deletions

File tree

include/gul17/DataTree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#ifndef GUL17_DATA_TREE_H_
2424
#define GUL17_DATA_TREE_H_
2525

26+
#include <map>
2627
#include <stdexcept>
2728
#include <string>
28-
#include <unordered_map>
2929
#include <variant>
3030
#include <vector>
3131

@@ -68,7 +68,7 @@ class DataTree
6868
// Type definitions
6969

7070
/// Type of an object (key-value pairs)
71-
using Object = std::unordered_map<std::string, DataTree>;
71+
using Object = std::map<std::string, DataTree>;
7272
/// Type of an array (list of values)
7373
using Array = std::vector<DataTree>;
7474
/// Underlying variant type to hold different data types

src/data_processors/json_processor.cc

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,23 +439,15 @@ struct JsonDataSerializer
439439
output_ << "{";
440440
if (!obj.empty())
441441
{
442-
// Sort keys for consistent output
443-
std::vector<DataTree::Object::key_type> keys;
444-
std::transform(obj.begin(), obj.end(), std::back_inserter(keys),
445-
[](const auto& pair) { return pair.first; });
446-
std::sort(keys.begin(), keys.end());
447-
448442
output_ << newline;
449-
for (size_t i = 0; i < keys.size(); ++i)
443+
size_t i = 0;
444+
for (const auto & [key, val] : obj)
450445
{
451-
const auto& key = keys[i];
452-
const auto& val = obj.at(key);
453-
454446
output_ << std::string(current_indent + indent, ' ');
455447
output_ << "\"" << escape_string(key) << "\": ";
456448
serialize_value(val, indent, current_indent + indent);
457449

458-
if (i < keys.size() - 1)
450+
if (i++ < obj.size() - 1)
459451
output_ << ",";
460452
output_ << newline;
461453
}

src/data_processors/xml_processor.cc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ struct XmlDataParser
139139
if (!attributes.empty() || !children.empty())
140140
{
141141
// Handle arrays for multiple same-tag children / attributes
142-
std::unordered_map<std::string, DataTree> obj;
142+
std::map<std::string, DataTree> obj;
143143
std::unordered_map<std::string, std::vector<DataTree>> child_groups;
144144

145145
for (const auto& [child_tag, child_value] : children)
@@ -480,19 +480,10 @@ struct XmlDataSerializer
480480
{
481481
const auto& obj = value.as<DataTree::Object>();
482482

483-
// Sort keys for consistent output
484-
std::vector<DataTree::Object::key_type> keys;
485-
std::transform(obj.begin(), obj.end(), std::back_inserter(keys),
486-
[](const auto& pair) { return pair.first; });
487-
std::sort(keys.begin(), keys.end());
488-
489483
// Opening tag with attributes
490484
output_ << indent_str << opening_tag;
491-
for (size_t i = 0; i < keys.size(); ++i)
485+
for (const auto & [key, val] : obj)
492486
{
493-
const auto& key = keys[i];
494-
const auto& val = obj.at(key);
495-
496487
if (key.rfind("@", 0) == 0)
497488
{
498489
// Attribute
@@ -512,11 +503,8 @@ struct XmlDataSerializer
512503
output_ << newline;
513504

514505
// Child elements and text content
515-
for (size_t i = 0; i < keys.size(); ++i)
506+
for (const auto & [key, val] : obj)
516507
{
517-
const auto& key = keys[i];
518-
const auto& val = obj.at(key);
519-
520508
// Skip already handled attributes and text content handled later
521509
if (key.rfind("@", 0) == 0 || key == "#text")
522510
continue;

src/data_processors/yaml_processor.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,8 @@ struct YamlDataSerializer
553553

554554
void serialize_mapping(const DataTree::Object& obj, size_t indent, size_t current_indent)
555555
{
556-
// Sort keys for consistent output
557-
std::vector<DataTree::Object::key_type> keys;
558-
std::transform(obj.begin(), obj.end(), std::back_inserter(keys),
559-
[](const auto& pair) { return pair.first; });
560-
std::sort(keys.begin(), keys.end());
561-
562-
for (size_t i = 0; i < keys.size(); ++i)
556+
for (const auto & [key, val] : obj)
563557
{
564-
const auto& key = keys[i];
565-
const auto& val = obj.at(key);
566-
567558
output_ << std::string(current_indent, ' ') << key << ":";
568559

569560
if (val.is_object() || val.is_array())

0 commit comments

Comments
 (0)