Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Zwinkau
libfirm
Commits
f75cc683
Commit
f75cc683
authored
Aug 30, 2006
by
Christian Würdig
Browse files
made code more firm style
added iterator macro [r8144]
parent
7cfc603a
Changes
2
Show whitespace changes
Inline
Side-by-side
ir/adt/plist.c
View file @
f75cc683
...
...
@@ -6,6 +6,7 @@
* elements.
* @author Kimon Hoffmann
* @date 14.07.2005
* @cvs-id $Id$
* @note Until now the code is entirely untested so it probably contains
* plenty of errors.
*/
...
...
@@ -20,128 +21,155 @@
* @param list the list for which to allocate the element.
* @return the newly allocated, uninitialized element.
*/
static
PListElement
*
allocate_element
(
PList
*
list
)
{
PListElement
*
newElement
;
if
(
list
->
firstFreeElement
!=
NULL
)
{
newElement
=
list
->
firstFreeElement
;
list
->
firstFreeElement
=
newElement
->
next
;
static
plist_element_t
*
allocate_element
(
plist_t
*
list
)
{
plist_element_t
*
newElement
;
if
(
list
->
first_free_element
!=
NULL
)
{
newElement
=
list
->
first_free_element
;
list
->
first_free_element
=
newElement
->
next
;
newElement
->
next
=
NULL
;
}
else
{
}
else
{
newElement
=
obstack_alloc
(
&
list
->
obst
,
sizeof
(
*
newElement
));
}
return
newElement
;
}
PList
*
plist_new
(
void
)
{
PList
*
list
=
xmalloc
(
sizeof
(
*
list
));
plist_t
*
plist_new
(
void
)
{
plist_t
*
list
=
xmalloc
(
sizeof
(
*
list
));
obstack_init
(
&
list
->
obst
);
list
->
firstElement
=
NULL
;
list
->
lastElement
=
NULL
;
list
->
firstFreeElement
=
NULL
;
list
->
elementCount
=
0
;
list
->
first_element
=
NULL
;
list
->
last_element
=
NULL
;
list
->
first_free_element
=
NULL
;
list
->
element_count
=
0
;
return
list
;
}
void
plist_free
(
PLis
t
*
list
)
{
void
plist_free
(
plist_
t
*
list
)
{
obstack_free
(
&
list
->
obst
,
NULL
);
list
->
firstElement
=
NULL
;
list
->
lastElement
=
NULL
;
list
->
firstFreeElement
=
NULL
;
list
->
elementCount
=
0
;
list
->
first_element
=
NULL
;
list
->
last_element
=
NULL
;
list
->
first_free_element
=
NULL
;
list
->
element_count
=
0
;
xfree
(
list
);
}
void
plist_insert_back
(
PList
*
list
,
void
*
value
)
{
if
(
list
->
lastElement
!=
NULL
)
{
plist_insert_after
(
list
,
list
->
lastElement
,
value
);
}
else
{
PListElement
*
newElement
=
allocate_element
(
list
);
void
plist_insert_back
(
plist_t
*
list
,
void
*
value
)
{
if
(
list
->
last_element
!=
NULL
)
{
plist_insert_after
(
list
,
list
->
last_element
,
value
);
}
else
{
plist_element_t
*
newElement
=
allocate_element
(
list
);
newElement
->
data
=
value
;
newElement
->
prev
=
NULL
;
newElement
->
next
=
NULL
;
list
->
first
E
lement
=
list
->
last
E
lement
=
newElement
;
list
->
element
C
ount
=
1
;
list
->
first
_e
lement
=
list
->
last
_e
lement
=
newElement
;
list
->
element
_c
ount
=
1
;
}
}
void
plist_insert_front
(
PList
*
list
,
void
*
value
)
{
if
(
list
->
firstElement
!=
NULL
)
{
plist_insert_before
(
list
,
list
->
firstElement
,
value
);
}
else
{
PListElement
*
newElement
=
allocate_element
(
list
);
void
plist_insert_front
(
plist_t
*
list
,
void
*
value
)
{
if
(
list
->
first_element
!=
NULL
)
{
plist_insert_before
(
list
,
list
->
first_element
,
value
);
}
else
{
plist_element_t
*
newElement
=
allocate_element
(
list
);
newElement
->
data
=
value
;
newElement
->
prev
=
NULL
;
newElement
->
next
=
NULL
;
list
->
first
E
lement
=
list
->
last
E
lement
=
newElement
;
list
->
element
C
ount
=
1
;
list
->
first
_e
lement
=
list
->
last
_e
lement
=
newElement
;
list
->
element
_c
ount
=
1
;
}
}
void
plist_insert_before
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
)
{
PL
ist
E
lement
*
prevElement
;
PL
ist
E
lement
*
newElement
=
allocate_element
(
list
);
void
plist_insert_before
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
)
{
pl
ist
_e
lement
_t
*
prevElement
;
pl
ist
_e
lement
_t
*
newElement
=
allocate_element
(
list
);
newElement
->
data
=
value
;
newElement
->
next
=
element
;
prevElement
=
element
->
prev
;
newElement
->
prev
=
prevElement
;
if
(
prevElement
!=
NULL
)
{
prevElement
->
next
=
newElement
;
}
else
{
list
->
firstElement
=
newElement
;
}
else
{
list
->
first_element
=
newElement
;
}
element
->
prev
=
newElement
;
++
list
->
element
C
ount
;
++
list
->
element
_c
ount
;
}
void
plist_insert_after
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
)
{
PL
ist
E
lement
*
nextElement
;
PL
ist
E
lement
*
newElement
=
allocate_element
(
list
);
void
plist_insert_after
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
)
{
pl
ist
_e
lement
_t
*
nextElement
;
pl
ist
_e
lement
_t
*
newElement
=
allocate_element
(
list
);
newElement
->
data
=
value
;
newElement
->
prev
=
element
;
nextElement
=
element
->
next
;
newElement
->
next
=
nextElement
;
if
(
nextElement
!=
NULL
)
{
nextElement
->
prev
=
newElement
;
}
else
{
list
->
lastElement
=
newElement
;
}
else
{
list
->
last_element
=
newElement
;
}
element
->
next
=
newElement
;
++
list
->
element
C
ount
;
++
list
->
element
_c
ount
;
}
void
plist_erase
(
PList
*
list
,
PListElement
*
element
)
{
PListElement
*
nextElement
=
element
->
next
;
PListElement
*
prevElement
=
element
->
prev
;
void
plist_erase
(
plist_t
*
list
,
plist_element_t
*
element
)
{
plist_element_t
*
nextElement
=
element
->
next
;
plist_element_t
*
prevElement
=
element
->
prev
;
if
(
nextElement
!=
NULL
)
{
nextElement
->
prev
=
prevElement
;
}
else
{
list
->
lastElement
=
prevElement
;
}
else
{
list
->
last_element
=
prevElement
;
}
if
(
prevElement
!=
NULL
)
{
prevElement
->
next
=
nextElement
;
}
else
{
list
->
firstElement
=
nextElement
;
}
--
list
->
elementCount
;
else
{
list
->
first_element
=
nextElement
;
}
--
list
->
element_count
;
/* Clean the element and prepend it to the free list */
element
->
prev
=
NULL
;
/* The allocation code expects prev to be NULL */
element
->
next
=
list
->
first
F
ree
E
lement
;
list
->
first
F
ree
E
lement
=
element
;
element
->
next
=
list
->
first
_f
ree
_e
lement
;
list
->
first
_f
ree
_e
lement
=
element
;
}
void
plist_clear
(
PList
*
list
)
{
PListElement
*
currentElement
=
list
->
firstElement
;
void
plist_clear
(
plist_t
*
list
)
{
plist_element_t
*
currentElement
=
list
->
first_element
;
while
(
currentElement
!=
NULL
)
{
currentElement
->
prev
=
NULL
;
currentElement
=
currentElement
->
next
;
}
currentElement
=
list
->
lastElement
;
currentElement
=
list
->
last_element
;
if
(
currentElement
!=
NULL
)
{
currentElement
->
next
=
list
->
first
F
ree
E
lement
;
currentElement
->
next
=
list
->
first
_f
ree
_e
lement
;
}
list
->
firstFreeElement
=
list
->
firstElement
;
list
->
firstElement
=
list
->
lastElement
=
0
;
list
->
elementCount
=
0
;
list
->
first_free_element
=
list
->
first_element
;
list
->
first_element
=
0
;
list
->
last_element
=
0
;
list
->
element_count
=
0
;
}
ir/adt/plist.h
View file @
f75cc683
...
...
@@ -6,6 +6,7 @@
* elements.
* @author Kimon Hoffmann
* @date 14.07.2005
* @cvs-id $Id$
* @note Until now the code is entirely untested so it probably contains
* plenty of errors.
*/
...
...
@@ -15,13 +16,13 @@
#include
<stddef.h>
#include
"obst.h"
typedef
struct
PL
ist
E
lement
PL
ist
E
lement
;
typedef
struct
PL
ist
PLis
t
;
typedef
struct
_pl
ist
_e
lement
pl
ist
_e
lement
_t
;
typedef
struct
_pl
ist
plist_
t
;
/**
* The
P
list data type.
* The
p
list data type.
*/
struct
PL
ist
{
struct
_pl
ist
{
/**
* The obstack used for all allocations.
*/
...
...
@@ -29,29 +30,29 @@ struct PList {
/**
* First element in the list.
*/
PL
ist
E
lement
*
first
E
lement
;
pl
ist
_e
lement
_t
*
first
_e
lement
;
/**
* Last element in the list.
*/
PL
ist
E
lement
*
last
E
lement
;
pl
ist
_e
lement
_t
*
last
_e
lement
;
/**
* Current number of elements in the list.
*/
int
element
C
ount
;
int
element
_c
ount
;
/**
* First element in the free list.
* Please note that the free list is a single linked list and all back
* references are invalid.
*/
PL
ist
E
lement
*
first
F
ree
E
lement
;
pl
ist
_e
lement
_t
*
first
_f
ree
_e
lement
;
};
/**
* An element in the pointer list.
*/
struct
PL
ist
E
lement
{
PL
ist
E
lement
*
next
;
PL
ist
E
lement
*
prev
;
struct
_pl
ist
_e
lement
{
pl
ist
_e
lement
_t
*
next
;
pl
ist
_e
lement
_t
*
prev
;
void
*
data
;
};
...
...
@@ -59,14 +60,14 @@ struct PListElement {
* Creates a new pointer list and initializes it.
* @return The newly created pointer list.
*/
PLis
t
*
plist_new
(
void
);
plist_
t
*
plist_new
(
void
);
/**
* Frees the passed pointer list.
* After a call to this function all references to the list and any of
* its elements are invalid.
*/
void
plist_free
(
PLis
t
*
list
);
void
plist_free
(
plist_
t
*
list
);
/**
* Returns the number of elements in a pointer list.
...
...
@@ -74,21 +75,21 @@ void plist_free(PList* list);
* @return The number of elements in a pointer list.
*/
#define plist_count(list) \
((list)->element
C
ount)
((list)->element
_c
ount)
/**
* Inserts an element at the back of a pointer list.
* @param list the pointer list to append the new element to.
* @param value the element value to append.
*/
void
plist_insert_back
(
PLis
t
*
list
,
void
*
value
);
void
plist_insert_back
(
plist_
t
*
list
,
void
*
value
);
/**
* Inserts an element at the front of a pointer list.
* @param list the pointer list to prepend the new element to.
* @param value the element value to prepend.
*/
void
plist_insert_front
(
PLis
t
*
list
,
void
*
value
);
void
plist_insert_front
(
plist_
t
*
list
,
void
*
value
);
/**
* Inserts an element into a pointer list before the specified element,
...
...
@@ -98,7 +99,7 @@ void plist_insert_front(PList* list, void* value);
* be inserted. This element must be a part of @p list.
* @param value the element value to insert.
*/
void
plist_insert_before
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
);
void
plist_insert_before
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
);
/**
* Inserts an element into a pointer list after the specified element,
...
...
@@ -108,7 +109,7 @@ void plist_insert_before(PList* list, PListElement* element, void* value);
* be inserted. This element must be a part of @p list.
* @param value the element value to insert.
*/
void
plist_insert_after
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
);
void
plist_insert_after
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
);
/**
* Erases the specified element from the pointer list.
...
...
@@ -116,13 +117,13 @@ void plist_insert_after(PList* list, PListElement* element, void* value);
* @param element the list element to erase. This element must be a part
* of @p list.
*/
void
plist_erase
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
);
void
plist_erase
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
);
/**
* Erases all elements from the specified pointer list.
* @param list the pointer list that should be cleard.
*/
void
plist_clear
(
PLis
t
*
list
);
void
plist_clear
(
plist_
t
*
list
);
/**
* Returns the first element of a pointer list.
...
...
@@ -130,7 +131,7 @@ void plist_clear(PList* list);
* @return a pointer to the element or NULL if the list is empty
*/
#define plist_first(list) \
((list)->first
E
lement)
((list)->first
_e
lement)
/**
* Returns the last element of a pointer list.
...
...
@@ -138,7 +139,7 @@ void plist_clear(PList* list);
* @return a pointer to the element or NULL if the list is empty
*/
#define plist_last(list) \
((list)->last
E
lement)
((list)->last
_e
lement)
/**
* Checks whether a pointer list element has a successor or not.
...
...
@@ -184,4 +185,10 @@ void plist_clear(PList* list);
#define plist_element_get_value(element) \
((element)->data)
/**
* Convenience macro to iterate over a plist.
*/
#define foreach_plist(list, el) \
for (el = plist_first(list); el; el = plist_element_get_next(el))
#endif
/*_PLIST_H_*/
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment