summaryrefslogtreecommitdiff
path: root/src/SSystem/SComponent/c_list.cpp
blob: 0f590a969a205b8290732408a81d573a62864965 (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
74
75
/**
 * c_list.cpp
 *
 */

#include "SSystem/SComponent/c_list.h"
#include "SSystem/SComponent/c_node.h"
#include <types.h>

void cLs_Init(node_list_class* list) {
    list->mpHead = NULL;
    list->mpTail = NULL;
    list->mSize = 0;
}

int cLs_SingleCut(node_class* node) {
    node_list_class* list = (node_list_class*)node->mpData;

    if (node == list->mpHead)
        list->mpHead = node->mpNextNode;
    if (node == list->mpTail)
        list->mpTail = node->mpPrevNode;

    cNd_SingleCut(node);
    cNd_ClearObject(node);

    int ret;
    if (--list->mSize > 0) {
        ret = 1;
    } else {
        ret = 0;
    }

    return ret;
}

int cLs_Addition(node_list_class* list, node_class* node) {
    if (list->mpTail == NULL) {
        list->mpHead = node;
    } else {
        cNd_Addition(list->mpTail, node);
    }

    list->mpTail = cNd_Last(node);
    cNd_SetObject(node, list);
    list->mSize = cNd_LengthOf(list->mpHead);
    return list->mSize;
}

int cLs_Insert(node_list_class* list, int idx, node_class* node) {
    node_class* pExisting = cNd_Order(list->mpHead, idx);
    if (pExisting == NULL) {
        return cLs_Addition(list, node);
    }

    cNd_SetObject(node, list);
    cNd_Insert(pExisting, node);
    list->mpHead = cNd_First(node);
    list->mSize = cNd_LengthOf(list->mpHead);
    return list->mSize;
}

node_class* cLs_GetFirst(node_list_class* list) {
    if (list->mSize != 0) {
        node_class* pHead = list->mpHead;
        cLs_SingleCut(pHead);
        return pHead;
    }

    return NULL;
}

void cLs_Create(node_list_class* list) {
    cLs_Init(list);
}