Skip to content

Commit f919b86

Browse files
committed
- Include guards for memory system
- Began Cache.cc/h - Class misc::List implementing an in-place linked list where the nodes contain the list pointers
1 parent eb8fa3b commit f919b86

27 files changed

+607
-41
lines changed

Diff for: Doxyfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ WARN_LOGFILE =
657657
# directories like "/usr/src/myproject". Separate the files or directories
658658
# with spaces.
659659

660-
INPUT = src/
660+
INPUT = src/lib/cpp
661661

662662
# This tag can be used to specify the character encoding of the source files
663663
# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is

Diff for: src/lib/cpp/List.cc

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* Multi2Sim
3+
* Copyright (C) 2013 Rafael Ubal ([email protected])
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
*/
19+
20+
#include <cassert>
21+
22+
#include "List.h"
23+
24+
25+
namespace misc
26+
{
27+
28+
29+
List::Node *List::getCurrent()
30+
{
31+
// Past the end
32+
if (current_index == size)
33+
{
34+
error = ErrorBounds;
35+
return nullptr;
36+
}
37+
38+
// Found
39+
error = ErrorOK;
40+
return current;
41+
}
42+
43+
44+
List::Node *List::Next()
45+
{
46+
// List empty
47+
if (size == 0)
48+
{
49+
error = ErrorEmpty;
50+
return nullptr;
51+
}
52+
53+
// Past the end
54+
if (current_index == size)
55+
{
56+
error = ErrorBounds;
57+
return nullptr;
58+
}
59+
60+
// Next element
61+
current_index++;
62+
current = current->next;
63+
64+
// Past the end reached
65+
if (current_index == size)
66+
{
67+
error = ErrorBounds;
68+
return nullptr;
69+
}
70+
71+
// Valid element
72+
error = ErrorOK;
73+
return current;
74+
}
75+
76+
77+
List::Node *List::Prev()
78+
{
79+
// List is empty
80+
if (size == 0)
81+
{
82+
error = ErrorEmpty;
83+
return nullptr;
84+
}
85+
86+
// Already in the beginning
87+
if (current_index == 0)
88+
{
89+
error = ErrorBounds;
90+
return nullptr;
91+
}
92+
93+
// Previous element
94+
current_index--;
95+
current = current ? current->prev : tail;
96+
97+
// Valid element
98+
error = ErrorOK;
99+
return current;
100+
}
101+
102+
103+
List::Node *List::Head()
104+
{
105+
// List is empty
106+
if (size == 0)
107+
{
108+
error = ErrorEmpty;
109+
return nullptr;
110+
}
111+
112+
// Go to head
113+
current_index = 0;
114+
current = head;
115+
116+
// Valid element
117+
error = ErrorOK;
118+
return current;
119+
}
120+
121+
122+
List::Node *List::Tail()
123+
{
124+
// List is empty
125+
if (size == 0)
126+
{
127+
error = ErrorEmpty;
128+
return nullptr;
129+
}
130+
131+
// Go to tail
132+
current_index = size - 1;
133+
current = tail;
134+
135+
// Valid element
136+
error = ErrorOK;
137+
return current;
138+
}
139+
140+
141+
void List::End()
142+
{
143+
current_index = size;
144+
current = nullptr;
145+
error = ErrorOK;
146+
}
147+
148+
149+
List::Node *List::Insert(Node *node)
150+
{
151+
// Invalid node
152+
if (node == nullptr)
153+
{
154+
error = ErrorNullNode;
155+
return nullptr;
156+
}
157+
158+
// Insert it
159+
if (size == 0)
160+
{
161+
// List is empty
162+
current = node;
163+
head = node;
164+
tail = node;
165+
}
166+
else if (current == head)
167+
{
168+
// Insert at the head
169+
node->next = head;
170+
head->prev = node;
171+
head = node;
172+
}
173+
else if (current == nullptr)
174+
{
175+
// Insert at the tail
176+
node->prev = tail;
177+
tail->next = node;
178+
tail = node;
179+
}
180+
else
181+
{
182+
// Insert in the middle
183+
node->prev = current->prev;
184+
node->next = current;
185+
current->prev = node;
186+
node->prev->next = node;
187+
}
188+
189+
// Update state
190+
error = ErrorOK;
191+
size++;
192+
current = node;
193+
return node;
194+
}
195+
196+
197+
List::Node *List::Remove()
198+
{
199+
// Check bounds
200+
if (current_index == size)
201+
{
202+
error = ErrorBounds;
203+
return nullptr;
204+
}
205+
206+
// Remove current element
207+
Node *node = current;
208+
if (size == 1)
209+
{
210+
head = nullptr;
211+
tail = nullptr;
212+
}
213+
else if (node == head)
214+
{
215+
node->next->prev = nullptr;
216+
head = node->next;
217+
}
218+
else if (node == tail)
219+
{
220+
node->prev->next = nullptr;
221+
tail = node->prev;
222+
}
223+
else
224+
{
225+
node->prev->next = node->next;
226+
node->next->prev = node->prev;
227+
}
228+
229+
// Update state
230+
assert(size > 0);
231+
error = ErrorOK;
232+
size--;
233+
current = node->next;
234+
return node;
235+
}
236+
237+
238+
} // namespace misc
239+

0 commit comments

Comments
 (0)