GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/fonts/textchunklist.cpp Lines: 84 84 100.0 %
Date: 2017-11-29 Branches: 25 30 83.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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
381476
TextChunkList::TextChunkList() :
28
    start(nullptr),
29
    end(nullptr),
30
    size(0),
31
    search(),
32
1144428
    searchWidth()
33
{
34
381476
}
35
36
482
void TextChunkList::insertFirst(TextChunk *restrict const item)
37
{
38
482
    TextChunk *restrict const oldFirst = start;
39
482
    if (start != nullptr)
40
108
        start->prev = item;
41
482
    item->prev = nullptr;
42
482
    if (oldFirst != nullptr)
43
108
        item->next = oldFirst;
44
    else
45
374
        end = item;
46
482
    start = item;
47
482
    size ++;
48
964
    search[TextChunkSmall(item->text, item->color, item->color2)] = item;
49
482
    searchWidth[item->text] = item;
50
482
}
51
52
12
void TextChunkList::moveToFirst(TextChunk *restrict const item)
53
{
54
12
    if (item == start)
55
        return;
56
57
10
    TextChunk *restrict const oldPrev = item->prev;
58
10
    if (oldPrev != nullptr)
59
10
        oldPrev->next = item->next;
60
10
    TextChunk *restrict const oldNext = item->next;
61
10
    if (oldNext != nullptr)
62
4
        oldNext->prev = item->prev;
63
    else
64
6
        end = oldPrev;
65
10
    TextChunk *restrict const oldFirst = start;
66
10
    if (start != nullptr)
67
10
        start->prev = item;
68
10
    item->prev = nullptr;
69
10
    item->next = oldFirst;
70
10
    start = item;
71
}
72
73
6
void TextChunkList::remove(const TextChunk *restrict const item)
74
{
75
6
    if (item == nullptr)
76
        return;
77
78
6
    TextChunk *restrict const oldPrev = item->prev;
79
6
    TextChunk *restrict const oldNext = item->next;
80
6
    if (oldPrev != nullptr)
81
4
        oldPrev->next = item->next;
82
    else
83
2
        start = oldNext;
84
6
    if (oldNext != nullptr)
85
2
        oldNext->prev = item->prev;
86
    else
87
4
        end = oldPrev;
88
89
18
    search.erase(TextChunkSmall(item->text,
90
        item->color, item->color2));
91
12
    searchWidth.erase(item->text);
92
6
    size --;
93
}
94
95
10
void TextChunkList::removeBack()
96
{
97
10
    TextChunk *restrict oldEnd = end;
98
10
    if (oldEnd != nullptr)
99
    {
100
10
        end = oldEnd->prev;
101
10
        if (end != nullptr)
102
8
            end->next = nullptr;
103
        else
104
2
            start = nullptr;
105
30
        search.erase(TextChunkSmall(oldEnd->text,
106
            oldEnd->color, oldEnd->color2));
107
20
        searchWidth.erase(oldEnd->text);
108
10
        delete oldEnd;
109
10
        size --;
110
    }
111
10
}
112
113
4
void TextChunkList::removeBack(int n)
114
{
115
4
    TextChunk *restrict item = end;
116
20
    while ((n != 0) && (item != nullptr))
117
    {
118
8
        n --;
119
8
        TextChunk *oldEnd = item;
120
8
        item = item->prev;
121
24
        search.erase(TextChunkSmall(oldEnd->text,
122
            oldEnd->color, oldEnd->color2));
123
16
        searchWidth.erase(oldEnd->text);
124
8
        delete oldEnd;
125
8
        size --;
126
    }
127
4
    if (item != nullptr)
128
    {
129
2
        item->next = nullptr;
130
2
        end = item;
131
    }
132
    else
133
    {
134
2
        start = nullptr;
135
2
        end = nullptr;
136
    }
137
4
}
138
139
381448
void TextChunkList::clear()
140
{
141
762896
    search.clear();
142
762896
    searchWidth.clear();
143
381448
    TextChunk *restrict item = start;
144
382292
    while (item != nullptr)
145
    {
146
422
        TextChunk *restrict const item2 = item->next;
147
422
        delete item;
148
422
        item = item2;
149
    }
150
381448
    start = nullptr;
151
381448
    end = nullptr;
152
381448
    size = 0;
153
381448
}