summaryrefslogtreecommitdiff
path: root/include/c/c_list.h
blob: 97a481bd887977fe1dd12556bd576c61f3e17e51 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef C_LIST_H
#define C_LIST_H

// This file was ported from https://github.com/NSMBW-Community/NSMBW-Decomp/blob/master/include/dol/cLib/c_list.hpp

#include "common.h"

/// @brief A doubly-linked list node. See cListMg_c.
/// @note Unofficial name.
class cListNd_c {
public:
    /// @brief Constructs a new list node.
    cListNd_c() : mpPrev(nullptr), mpNext(nullptr) {}

    cListNd_c *getPrev() const {
        return mpPrev;
    }
    cListNd_c *getNext() const {
        return mpNext;
    }

protected:
    cListNd_c *mpPrev; ///< The previous node.
    cListNd_c *mpNext; ///< The next node.

    friend class cListMg_c;
};

class cListMg_c {
public:
    cListMg_c() : mpFirst(nullptr), mpLast(nullptr) {}

    void insertAfter(cListNd_c *node, cListNd_c *prevNode);

    /**
     * @brief Removes a node from the list.
     *
     * @param node The node to remove.
     * @return If the operation was successful.
     */
    void remove(cListNd_c *node);

    /**
     * @brief Adds a node to the end of the list.
     *
     * @param node The node to append.
     * @return If the operation was successful.
     */
    void append(cListNd_c *node);

    /**
     * @brief Adds a node to the beginning of the list.
     *
     * @param node The node to prepend.
     * @return If the operation was successful.
     */
    void prepend(cListNd_c *node);

    void clear();

    cListNd_c *getFirst() const {
        return mpFirst;
    }
    cListNd_c *getLast() const {
        return mpLast;
    }

protected:
    cListNd_c *mpFirst; ///< The first node in the list.
    cListNd_c *mpLast;  ///< The last node in the list.
};

#endif