diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-07-17 16:54:35 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-07-17 16:54:35 +1000 |
| commit | 7197da21f6bcc1aaa5d1905228e0e2ec1caf3fa8 (patch) | |
| tree | f24f81cba08160d45d7e994dc31f48506e823e49 /src/playlist/include/shuffler.hpp | |
| parent | b6bc6b9e47605ede9bffe50445d1afe3acf0ab49 (diff) | |
| download | tangara-fw-7197da21f6bcc1aaa5d1905228e0e2ec1caf3fa8.tar.gz | |
Basic playlists for upcoming
Beware under-testing and bugs. Just getting something barebones in so
that I can do rN+1 bringup
Diffstat (limited to 'src/playlist/include/shuffler.hpp')
| -rw-r--r-- | src/playlist/include/shuffler.hpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/playlist/include/shuffler.hpp b/src/playlist/include/shuffler.hpp new file mode 100644 index 00000000..affc6301 --- /dev/null +++ b/src/playlist/include/shuffler.hpp @@ -0,0 +1,68 @@ +/* + * Copyright 2023 jacqueline <me@jacqueline.id.au> + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include <deque> +#include <memory> +#include <mutex> +#include <variant> +#include <vector> + +#include "bloom_filter.hpp" +#include "database.hpp" +#include "future_fetcher.hpp" +#include "random.hpp" +#include "source.hpp" +#include "track.hpp" + +namespace playlist { + +/* + * A source composes of other sources and/or specific extra tracks. Supports + * iteration over its contents in a random order. + */ +class Shuffler : public ISource { + public: + static auto Create() -> Shuffler*; + + explicit Shuffler( + util::IRandom* random, + std::unique_ptr<util::BloomFilter<database::TrackId>> filter); + + auto Current() -> std::optional<database::TrackId> override; + auto Advance() -> std::optional<database::TrackId> override; + auto Peek(std::size_t, std::vector<database::TrackId>*) + -> std::size_t override; + + typedef std::variant<database::TrackId, std::shared_ptr<IResetableSource>> + Item; + auto Add(Item) -> void; + + /* + * Returns the enqueued items, starting from the current item, in their + * original insertion order. + */ + auto Unshuffle() -> std::vector<Item>; + + // Not copyable or movable. + + Shuffler(const Shuffler&) = delete; + Shuffler& operator=(const Shuffler&) = delete; + + private: + auto RefillBuffer() -> void; + + util::IRandom* random_; + + std::unique_ptr<util::BloomFilter<database::TrackId>> already_played_; + bool out_of_items_; + + std::deque<Item> ordered_items_; + std::deque<database::TrackId> shuffled_items_buffer_; +}; + +} // namespace playlist |
