Skip to content

Commit 68427ff

Browse files
committed
Reduce memory used by random-access indirect_adapter
1 parent 6c13d0e commit 68427ff

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

include/cpp-sort/adapters/indirect_adapter.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2022 Morwenn
2+
* Copyright (c) 2015-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_ADAPTERS_INDIRECT_ADAPTER_H_
@@ -12,7 +12,6 @@
1212
#include <iterator>
1313
#include <type_traits>
1414
#include <utility>
15-
#include <vector>
1615
#include <cpp-sort/sorter_facade.h>
1716
#include <cpp-sort/sorter_traits.h>
1817
#include <cpp-sort/utility/adapter_storage.h>
@@ -92,8 +91,6 @@ namespace cppsort
9291
////////////////////////////////////////////////////////////
9392
// Move the values according the iterator's positions
9493

95-
std::vector<bool> sorted(last - first, false);
96-
9794
// Element where the current cycle starts
9895
auto start = first;
9996

@@ -102,7 +99,9 @@ namespace cppsort
10299
auto current = start;
103100
auto next_pos = current - first;
104101
auto next = iterators[next_pos];
105-
sorted[next_pos] = true;
102+
// We replace all "sorted" iterators with last to make it
103+
// possible to find unsorted iterators between cycles
104+
iterators[next_pos] = last;
106105

107106
// Process the current cycle
108107
if (next != current) {
@@ -112,15 +111,15 @@ namespace cppsort
112111
current = next;
113112
auto next_pos = next - first;
114113
next = iterators[next_pos];
115-
sorted[next_pos] = true;
114+
iterators[next_pos] = last;
116115
}
117116
*current = std::move(tmp);
118117
}
119118

120119
// Find the next cycle
121120
do {
122121
++start;
123-
} while (start != last && sorted[start - first]);
122+
} while (start != last && iterators[start - first] == last);
124123

125124
}
126125
#ifdef __cpp_lib_uncaught_exceptions

0 commit comments

Comments
 (0)