~aleteoryx/muditaos

ref: 2de71a003692ec1207cd4f0258379f82500f378f muditaos/module-vfs/include/user/purefs/fs/handle_mapper.hpp -rw-r--r-- 1.5 KiB
2de71a00 — Maciej-Mudita [MOS-74] Fix wrong tethering popup order 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <vector>
namespace purefs::fs::internal
{

    template <typename T> class handle_mapper
    {
      public:
        T const &operator[](std::size_t index) const
        {
            return data[index];
        }
        T &operator[](std::size_t index)
        {
            return data[index];
        }
        void remove(std::size_t index)
        {
            unused.push_back(index);
            data[index] = {};
        }
        bool exists(std::size_t index) const
        {
            return index < data.size();
        }
        std::size_t insert(T const &value);
        auto begin() const
        {
            return data.begin();
        }
        auto end() const
        {
            return data.end();
        }
        auto size() const
        {
            return data.size();
        }
        auto clear() -> void
        {
            data.clear();
            unused.clear();
        }
        using value_type = T;

      private:
        std::vector<T> data;
        std::vector<std::size_t> unused;
    };

    template <typename T> std::size_t handle_mapper<T>::insert(T const &value)
    {
        if (unused.empty()) {
            data.push_back(value);
            return data.size() - 1;
        }
        const std::size_t result = unused.back();
        unused.pop_back();
        data[result] = value;
        return result;
    }
} // namespace purefs::fs::internal