14 #ifndef LIST_MERGE_SORT_H
15 #define LIST_MERGE_SORT_H
21 #ifndef DOUBLE_LINKED_LIST_H
59 template<
class T,
class Iterator = T*,
class Lessor = std::less<T>,
class Metrics = NoSortMetrics<T> >
98 for(
Iterator iter = begin; iter != end; ++iter) {
101 added->push_back(*iter);
106 bool dataInListA =
true;
107 DataList& sorted = mergeSort(listA, listB, dataInListA);
114 for( ; originalIter != end && sorted.
end() != sortedIter; ++originalIter, ++sortedIter) {
115 *originalIter = *sortedIter;
142 bool dataInListA =
true;
143 DataList& sorted = mergeSort(listA, listB, dataInListA);
147 moveAll(sorted, data);
175 DataList& mergeSort(ListList& listA, ListList& listB,
bool& dataInListA) {
178 ListList& input = dataInListA ? listA : listB;
179 ListList& output = dataInListA ? listB : listA;
181 if(input.end() == input.begin() + 1) {
185 dataInListA = ! dataInListA;
186 mergeLists(input, output);
189 return *(dataInListA ? listA : listB).begin();
202 void mergeLists(ListList& input, ListList& output) {
205 ListIter listIter = input.begin();
206 while(input.end() != listIter) {
209 ListIter current = output.end() - 1;
212 ListIter first = listIter;
215 ListIter second = listIter;
219 if(input.end() == second) {
221 moveAll(*first, *current);
224 mergeTwo(*first, *second, *current);
238 while(!first.isEmpty() && !second.isEmpty()) {
240 DataIter secondIter = second.begin();
241 metrics.compare(*firstIter, *secondIter);
242 if(lessor(*firstIter, *secondIter)) {
253 moveAll(second, out);
262 while(!from.isEmpty()) {
Double Linked List defintion.
DoubleLinkedList which can be iterated via DoubleLinkedList::iterator.
void push_back(const value_type &value)
Insert value as the last item in this list.
Iterator class that works genericly on a List containing Node elements with data of type T.
Merge Sort algorithm using lists to hold the data.
DoubleLinkedList< T > DataList
Convenience typedef of the type of list used in this sort.
void sort(DataList &data)
Sort the values in data in constant memory by removing the disassembling data, sorting the values and...
T value_type
Convenience typedef of the type of data in the list.
Metrics metrics
Collection of metrics for the caller to inspect details of the sort.
DataList::iterator DataIter
Convenience typedef of the iterator of DataList.
void sort(const Iterator begin, const Iterator end)
Sort from begin to end by copying.
Do-nothing Metric-collector for major actions done by ListMergeSort.
void done()
The sort completed.
void compare(const T &a, const T &b)
a and b are compared
void reset()
Metrics should be reset due to starting a new sort.
void swap()
Two items were swapped.
This file is to be included at the end of Iterator.h.
void moveBefore(Iterator< T, List, Node > &a, Iterator< T, List, Node > &b)
Move the value at a before b.