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

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