GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/fonts/textchunklist.cpp Lines: 87 87 100.0 %
Date: 2021-03-17 Branches: 26 32 81.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/fonts/textchunklist.h"
23
24
#include "gui/fonts/textchunk.h"
25
26
#include "debug.h"
27
28
195602
TextChunkList::TextChunkList() :
29
    start(nullptr),
30
    end(nullptr),
31
    size(0),
32
    search(),
33
586806
    searchWidth()
34
{
35
195602
}
36
37
242
void TextChunkList::insertFirst(TextChunk *restrict const item)
38
{
39
242
    TextChunk *restrict const oldFirst = start;
40
242
    if (start != nullptr)
41
55
        start->prev = item;
42
242
    item->prev = nullptr;
43
242
    if (oldFirst != nullptr)
44
55
        item->next = oldFirst;
45
    else
46
187
        end = item;
47
242
    start = item;
48
242
    size ++;
49
484
    search[TextChunkSmall(item->text, item->color, item->color2)] = item;
50
242
    searchWidth[item->text] = item;
51
242
}
52
53
6
void TextChunkList::moveToFirst(TextChunk *restrict const item)
54
{
55
6
    if (item == start)
56
        return;
57
58
5
    TextChunk *restrict const oldPrev = item->prev;
59
5
    if (oldPrev != nullptr)
60
5
        oldPrev->next = item->next;
61
5
    TextChunk *restrict const oldNext = item->next;
62
5
    if (oldNext != nullptr)
63
2
        oldNext->prev = item->prev;
64
    else
65
3
        end = oldPrev;
66
5
    TextChunk *restrict const oldFirst = start;
67
5
    if (start != nullptr)
68
5
        start->prev = item;
69
5
    item->prev = nullptr;
70
5
    item->next = oldFirst;
71
5
    start = item;
72
}
73
74
3
void TextChunkList::remove(const TextChunk *restrict const item)
75
{
76
3
    if (item == nullptr)
77
        return;
78
79
3
    TextChunk *restrict const oldPrev = item->prev;
80
3
    TextChunk *restrict const oldNext = item->next;
81
3
    if (oldPrev != nullptr)
82
2
        oldPrev->next = item->next;
83
    else
84
1
        start = oldNext;
85
3
    if (oldNext != nullptr)
86
1
        oldNext->prev = item->prev;
87
    else
88
2
        end = oldPrev;
89
90
6
    search.erase(TextChunkSmall(item->text,
91
3
        item->color, item->color2));
92
6
    searchWidth.erase(item->text);
93
3
    size --;
94
}
95
96
5
void TextChunkList::removeBack()
97
{
98
5
    TextChunk *restrict oldEnd = end;
99
5
    if (oldEnd != nullptr)
100
    {
101
5
        end = oldEnd->prev;
102
5
        if (end != nullptr)
103
4
            end->next = nullptr;
104
        else
105
1
            start = nullptr;
106
10
        search.erase(TextChunkSmall(oldEnd->text,
107
5
            oldEnd->color, oldEnd->color2));
108
10
        searchWidth.erase(oldEnd->text);
109
5
        delete oldEnd;
110
5
        size --;
111
    }
112
5
}
113
114
2
void TextChunkList::removeBack(int n)
115
{
116
2
    TextChunk *restrict item = end;
117
10
    while ((n != 0) && (item != nullptr))
118
    {
119
4
        n --;
120
4
        TextChunk *oldEnd = item;
121
4
        item = item->prev;
122
8
        search.erase(TextChunkSmall(oldEnd->text,
123
4
            oldEnd->color, oldEnd->color2));
124
8
        searchWidth.erase(oldEnd->text);
125
4
        delete oldEnd;
126
4
        size --;
127
    }
128
2
    if (item != nullptr)
129
    {
130
1
        item->next = nullptr;
131
1
        end = item;
132
    }
133
    else
134
    {
135
1
        start = nullptr;
136
1
        end = nullptr;
137
    }
138
2
}
139
140
195588
void TextChunkList::clear()
141
{
142
391176
    search.clear();
143
391176
    searchWidth.clear();
144
195588
    TextChunk *restrict item = start;
145
196012
    while (item != nullptr)
146
    {
147
212
        TextChunk *restrict const item2 = item->next;
148
212
        delete item;
149
212
        item = item2;
150
    }
151
195588
    start = nullptr;
152
195588
    end = nullptr;
153
195588
    size = 0;
154
195588
}