2024-10-22 00:24:13 +00:00
|
|
|
#ifndef TAPE_UTIL_H
|
|
|
|
#define TAPE_UTIL_H
|
|
|
|
|
2024-10-27 03:18:23 +00:00
|
|
|
#include "filetape.h"
|
2024-10-22 00:24:13 +00:00
|
|
|
#include "tape.h"
|
2024-10-27 03:18:23 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using std::unique_ptr;
|
2024-10-22 00:24:13 +00:00
|
|
|
|
|
|
|
namespace tape {
|
|
|
|
|
2024-10-27 03:18:23 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
2024-10-22 00:24:13 +00:00
|
|
|
|
2024-10-26 01:23:11 +00:00
|
|
|
} // namespace tape
|
2024-10-22 00:24:13 +00:00
|
|
|
|
2024-10-27 03:18:23 +00:00
|
|
|
#endif // !TAPE_UTIL_H
|