ManaPlus
textchunklist.cpp
Go to the documentation of this file.
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 
23 
24 #include "gui/fonts/textchunk.h"
25 
26 #include "debug.h"
27 
29  start(nullptr),
30  end(nullptr),
31  size(0),
32  search(),
33  searchWidth()
34 {
35 }
36 
38 {
39  TextChunk *restrict const oldFirst = start;
40  if (start != nullptr)
41  start->prev = item;
42  item->prev = nullptr;
43  if (oldFirst != nullptr)
44  item->next = oldFirst;
45  else
46  end = item;
47  start = item;
48  size ++;
49  search[TextChunkSmall(item->text, item->color, item->color2)] = item;
50  searchWidth[item->text] = item;
51 }
52 
54 {
55  if (item == start)
56  return;
57 
58  TextChunk *restrict const oldPrev = item->prev;
59  if (oldPrev != nullptr)
60  oldPrev->next = item->next;
61  TextChunk *restrict const oldNext = item->next;
62  if (oldNext != nullptr)
63  oldNext->prev = item->prev;
64  else
65  end = oldPrev;
66  TextChunk *restrict const oldFirst = start;
67  if (start != nullptr)
68  start->prev = item;
69  item->prev = nullptr;
70  item->next = oldFirst;
71  start = item;
72 }
73 
74 void TextChunkList::remove(const TextChunk *restrict const item)
75 {
76  if (item == nullptr)
77  return;
78 
79  TextChunk *restrict const oldPrev = item->prev;
80  TextChunk *restrict const oldNext = item->next;
81  if (oldPrev != nullptr)
82  oldPrev->next = item->next;
83  else
84  start = oldNext;
85  if (oldNext != nullptr)
86  oldNext->prev = item->prev;
87  else
88  end = oldPrev;
89 
90  search.erase(TextChunkSmall(item->text,
91  item->color, item->color2));
92  searchWidth.erase(item->text);
93  size --;
94 }
95 
97 {
98  TextChunk *restrict oldEnd = end;
99  if (oldEnd != nullptr)
100  {
101  end = oldEnd->prev;
102  if (end != nullptr)
103  end->next = nullptr;
104  else
105  start = nullptr;
106  search.erase(TextChunkSmall(oldEnd->text,
107  oldEnd->color, oldEnd->color2));
108  searchWidth.erase(oldEnd->text);
109  delete oldEnd;
110  size --;
111  }
112 }
113 
115 {
116  TextChunk *restrict item = end;
117  while ((n != 0) && (item != nullptr))
118  {
119  n --;
120  TextChunk *oldEnd = item;
121  item = item->prev;
122  search.erase(TextChunkSmall(oldEnd->text,
123  oldEnd->color, oldEnd->color2));
124  searchWidth.erase(oldEnd->text);
125  delete oldEnd;
126  size --;
127  }
128  if (item != nullptr)
129  {
130  item->next = nullptr;
131  end = item;
132  }
133  else
134  {
135  start = nullptr;
136  end = nullptr;
137  }
138 }
139 
141 {
142  search.clear();
143  searchWidth.clear();
144  TextChunk *restrict item = start;
145  while (item != nullptr)
146  {
147  TextChunk *restrict const item2 = item->next;
148  delete item;
149  item = item2;
150  }
151  start = nullptr;
152  end = nullptr;
153  size = 0;
154 }
void insertFirst(TextChunk *const item)
uint32_t size
Definition: textchunklist.h:56
TextChunk * start
Definition: textchunklist.h:54
void remove(const TextChunk *const item)
TextChunk * end
Definition: textchunklist.h:55
std::map< TextChunkSmall, TextChunk * > search
Definition: textchunklist.h:57
std::map< std::string, TextChunk * > searchWidth
Definition: textchunklist.h:58
void moveToFirst(TextChunk *const item)
TextChunk * next
Definition: textchunk.h:69
Color color
Definition: textchunk.h:66
Color color2
Definition: textchunk.h:67
TextChunk * prev
Definition: textchunk.h:68
std::string text
Definition: textchunk.h:65
#define restrict
Definition: localconsts.h:165
#define nullptr
Definition: localconsts.h:45
int size()
Definition: emotedb.cpp:306