summaryrefslogtreecommitdiff
path: root/AK/SinglyLinkedListSizePolicy.h
blob: b0123f01b6c9e40e175b4c83590a064ea911f140 (plain)
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
/*
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Types.h>

namespace AK {

struct DefaultSizeCalculationPolicy {
    constexpr void increase_size(auto const&) { }

    constexpr void decrease_size(auto const&) { }

    constexpr void reset() { }

    constexpr size_t size(auto const* head) const
    {
        size_t size = 0;
        for (auto* node = head; node; node = node->next)
            ++size;
        return size;
    }
};

struct CountingSizeCalculationPolicy {
    constexpr void increase_size(auto const&) { ++m_size; }

    constexpr void decrease_size(auto const&) { --m_size; }

    constexpr void reset() { m_size = 0; }

    constexpr size_t size(auto const*) const { return m_size; }

private:
    size_t m_size { 0 };
};

}

#ifdef USING_AK_GLOBALLY
using AK::CountingSizeCalculationPolicy;
using AK::DefaultSizeCalculationPolicy;
#endif