erius
33fee958a5
Added 2 new constructors for FileTape which copy the size of existing tape into a new one Implemented external sort of a Tape using heap sort Rewritten FileTape constructors implementation to better take advantage of existing constructor calls Added const to some parameters to indicate their immutability Added unit tests for external_sort
47 lines
1.5 KiB
C++
47 lines
1.5 KiB
C++
#ifndef TAPE_UTIL_H
|
|
#define TAPE_UTIL_H
|
|
|
|
#include "filetape.h"
|
|
#include "tape.h"
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <memory>
|
|
|
|
using std::unique_ptr;
|
|
|
|
namespace tape {
|
|
|
|
/**
|
|
* Lambda, which takes the amount of cells a tape should have and returns
|
|
* a unique pointer to a temporary Tape with the specified amount of cells
|
|
*/
|
|
using TempTapeFactory = std::function<unique_ptr<Tape>(size_t)>;
|
|
|
|
/**
|
|
* TempTapeFactory that creates a new temporary FileTape
|
|
*/
|
|
const static inline TempTapeFactory FILETAPE_FACTORY =
|
|
[](size_t cells) -> unique_ptr<Tape> {
|
|
return std::make_unique<FileTape>(FileTape(cells));
|
|
};
|
|
|
|
/**
|
|
* External sort for Tape. Reads *ram_limit* cells into memory, sorts them,
|
|
* writes them to a temporary Tape, does this until all data from input Tape is
|
|
* sorted and put into temporary Tapes, then merges temporary Tapes sorted data
|
|
* into output Tape. Heap sort is preffered due to reduction in seek operations.
|
|
*
|
|
* @param input Tape, data of which should be sorted
|
|
* @param output Tape, where the sorted data is going to be written
|
|
* @param tmp_tape_factory Lambda expression that defines how new temporary
|
|
* Tapes are created
|
|
* @param sort_limit Max amount of cells that can be loaded into memory to use
|
|
* while sorting. Set to SIZE_MAX by default
|
|
*/
|
|
void external_sort(Tape &input, Tape &output, TempTapeFactory tmp_tape_factory,
|
|
size_t sort_limit = SIZE_MAX);
|
|
|
|
} // namespace tape
|
|
|
|
#endif // !TAPE_UTIL_H
|