Functions

/home/sindre/dev_uclinux/suzaku_shared/hwos_sw/libhwos/hlist.h File Reference

[Hardware OS Double Linked List Interface] General functionality for double linked lists. More...

#include "hstructures.h"

Go to the source code of this file.

Functions

int hlist_size (struct hlist *list)
 Get size of elements in list.
void * hlelement_get_data (struct hlelement *element)
 Get data for the given element.
int hlelement_set_data (struct hlelement *element, void *data)
 Set data for the given element.
struct hlelementhlelement_get_prev (struct hlelement *element)
 Get previous element in list.
struct hlelementhlelement_get_next (struct hlelement *element)
 Get next element in list.
struct hlisthlist_create ()
 Create a new list.
struct hlelementhlist_enqueue (struct hlist *list, void *data_element)
 Enqueue a new element in a list.
struct hlelementhlist_insert_after (struct hlist *list, struct hlelement *element, void *data_element)
 Insert a new element after another in the given list.
struct hlelementhlist_insert_before (struct hlist *list, struct hlelement *element, void *data_element)
 Insert a new element before another in the given list.
void * hlelement_remove (struct hlelement *element, struct hlist *list)
 Remove a given element from the given list.
struct hlorphanhlorphan_create (void *data_element)
 Create an list element that does not belong to any list.
struct hlorphanhlorphan_get_next (struct hlorphan *orphan)
 Get next element for the given element.
int hlorphan_set_next (struct hlorphan *orphan, struct hlorphan *next)
 Set next field for the given element.
struct hlorphanhlorphan_get_prev (struct hlorphan *orphan)
 Get previous element for the given element.
int hlorphan_set_prev (struct hlorphan *orphan, struct hlorphan *prev)
 Set previous field for the given element.
void * hlorphan_get_data (struct hlorphan *orphan)
 Get pointer to data for the given element.
void * hlist_dequeue (struct hlist *list)
 Remove the first element (dequeue) from the list.
int hlist_remove (struct hlist *list)
 Remove the given list and it's elements.
struct hlelementhlist_get_first (struct hlist *list)
 Get first element of the given list.
struct hlelementhlist_get_last (struct hlist *list)
 Get last element of the given list.

Detailed Description

[Hardware OS Double Linked List Interface] General functionality for double linked lists.

General purpose double linked list structure. Each list (hlist) has elements (hlelement). Each element has data pointed to by void-pointers.

Author (2011): Sindre Hansen

Definition in file hlist.h.


Function Documentation

void* hlelement_get_data ( struct hlelement element  ) 

Get data for the given element.

Parameters:
element Pointer to element.
Returns:
Pointer to the element's data.

Definition at line 131 of file hlist.c.

{
        if (element == NULL)
                return NULL;

        return element->data_element;
}

struct hlelement* hlelement_get_next ( struct hlelement element  )  [read]

Get next element in list.

Parameters:
element Pointer to element that has the next field.
Returns:
Pointer to the next element on success. NULL on failure.

Definition at line 160 of file hlist.c.

{
        if (element == NULL)
                return NULL;

        return element->next;
}

struct hlelement* hlelement_get_prev ( struct hlelement element  )  [read]

Get previous element in list.

Parameters:
element Pointer to element that has the previous field.
Returns:
Pointer to the previous element on success. NULL on failure.

Definition at line 151 of file hlist.c.

{
        if (element == NULL)
                return NULL;

        return element->prev;
}

void* hlelement_remove ( struct hlelement element,
struct hlist list 
)

Remove a given element from the given list.

Parameters:
list Pointer to the given list.
element Pointer to the given element.
Returns:
Pointer to the elements data on success. NULL on failure.

Definition at line 252 of file hlist.c.

{
        if (element == NULL)
                return NULL;
        
        void* data_element = hlelement_get_data(element);
        hlelement_set_data(element, NULL);

        if (element == hlist_get_first(list))
                set_first_(list, hlelement_get_next(element));

        if (element == hlist_get_last(list))
                set_last_(list, hlelement_get_prev(element));

        // Update element structure and remove element from memory.
        element_set_next_(hlelement_get_prev(element), hlelement_get_next(element) );
        element_set_prev_(hlelement_get_next(element), hlelement_get_prev(element) );
        free(element);

        decrement_size_(list);

        return data_element;
}

int hlelement_set_data ( struct hlelement element,
void *  data 
)

Set data for the given element.

Parameters:
element Pointer to element.
data Pointer to the data.
Returns:
0 on success. Negative on failure.

Definition at line 140 of file hlist.c.

{
        if (element == NULL)
                return -1;

        element->data_element = data;

        return 0;
}

struct hlist* hlist_create (  )  [read]

Create a new list.

Returns:
Pointer to the new list structure on success. NULL on failure.

Definition at line 169 of file hlist.c.

{
        struct hlist* list = calloc(1, sizeof(*list));
        set_first_(list, NULL);
        set_last_(list, NULL);
        set_size_(list, 0);

        return list;
}

void* hlist_dequeue ( struct hlist list  ) 

Remove the first element (dequeue) from the list.

Parameters:
list Pointer to the given list.
Returns:
Pointer to the element's data on success. NULL on failure.

Definition at line 335 of file hlist.c.

{
        if (list == NULL)
                return NULL;

        // Delist is the same as removing the first element in the list.
        return hlelement_remove(hlist_get_first(list), list);
}

struct hlelement* hlist_enqueue ( struct hlist list,
void *  data_element 
) [read]

Enqueue a new element in a list.

Parameters:
list Pointer to the given list.
data_element Pointer to the data that should be connected to the element.
Returns:
Pointer to the new element on success. NULL on failure.

Definition at line 180 of file hlist.c.

{
        if (list == NULL)
                return NULL;

        struct hlelement* new_element = calloc(1, sizeof(*new_element));
        new_element->data_element = data_element;
        struct hlelement* last_element = hlist_get_last(list);

        // Update element structure.
        element_set_next_(last_element, new_element );
        element_set_prev_(new_element, last_element );
        element_set_next_(new_element, NULL );

        if (last_element == NULL) {
               set_first_(list, new_element);
        }

        set_last_(list, new_element);

        increment_size_(list);

        return new_element;
}

struct hlelement* hlist_get_first ( struct hlist list  )  [read]

Get first element of the given list.

Parameters:
list Pointer to the given list.
Returns:
Pointer to the first element on success. NULL on failure.

Definition at line 364 of file hlist.c.

{
        if (list == NULL)
                return NULL;

        return list->first; 
}

struct hlelement* hlist_get_last ( struct hlist list  )  [read]

Get last element of the given list.

Parameters:
list Pointer to the given list.
Returns:
Pointer to the last element on success. NULL on failure.

Definition at line 373 of file hlist.c.

{
        if (list == NULL)
                return NULL;

        return list->last;
}

struct hlelement* hlist_insert_after ( struct hlist list,
struct hlelement element,
void *  data_element 
) [read]

Insert a new element after another in the given list.

Parameters:
list Pointer to the given list.
element Element that the new element should be placed after.
data_element Pointer to the data that should be connected to the element.
Returns:
Pointer to the new element on success. NULL on failure.

Definition at line 206 of file hlist.c.

{
        if (list == NULL || element == NULL)
                return NULL;

        struct hlelement* new_element = calloc(1, sizeof(*new_element));
        new_element->data_element = data_element;

        // Update element structure.
        element_set_prev_(new_element, element);
        element_set_next_(new_element, hlelement_get_next(element));
        element_set_prev_(hlelement_get_next(element), new_element);
        element_set_next_(element, new_element);

        if (hlist_get_last(list) == element)
                set_last_(list, new_element);

        increment_size_(list);

        return new_element;
}

struct hlelement* hlist_insert_before ( struct hlist list,
struct hlelement element,
void *  data_element 
) [read]

Insert a new element before another in the given list.

Parameters:
list Pointer to the given list.
element Element that the new element should be placed before.
data_element Pointer to the data that should be connected to the element.
Returns:
Pointer to the new element on success. NULL on failure.

Definition at line 229 of file hlist.c.

{
        if (list == NULL || element == NULL)
                return NULL;

        struct hlelement* new_element = calloc(1, sizeof(*new_element));
        new_element->data_element = data_element;

        // Update element structure.
        element_set_prev_(new_element, hlelement_get_prev(element) );
        element_set_next_(hlelement_get_prev(element), new_element);
        element_set_next_(new_element, element);
        element_set_prev_(element, new_element);

        if (hlist_get_first(list) == element)
                set_first_(list, new_element);

        increment_size_(list);

        return new_element;
}

int hlist_remove ( struct hlist list  ) 

Remove the given list and it's elements.

Note that this will NOT remove the data elements connected to each list element.

Parameters:
list Pointer to the given list.
Returns:
0 on success. Negative on failure.

Definition at line 345 of file hlist.c.

{
        if (list == NULL)
                return -1;

        // Remove all the elements in the list and free the list from memory.
        struct hlelement* element = hlist_get_first(list);
        struct hlelement* next_element = NULL;
        while (element != NULL) {
                next_element = hlelement_get_next(element);
                hlelement_remove(element, list);
                element = next_element;
        }
        free(list);

        return 0;
}

int hlist_size ( struct hlist list  ) 

Get size of elements in list.

Parameters:
list Pointer to list.
Returns:
Number of elements in list.

Definition at line 122 of file hlist.c.

{
        if (list == NULL)
                return -1;

        return list->elements_num;
}

struct hlorphan* hlorphan_create ( void *  data_element  )  [read]

Create an list element that does not belong to any list.

Parameters:
data_element Pointer to the data that should be connected to the element.
Returns:
Pointer to the new element on success. NULL on failure.

Definition at line 277 of file hlist.c.

{
        struct hlorphan* new_element = calloc(1, sizeof(*new_element));
        new_element->data_element = data_element;

        return new_element;
}

void* hlorphan_get_data ( struct hlorphan orphan  ) 

Get pointer to data for the given element.

Parameters:
orphan Pointer to the element that has the data field.
Returns:
Pointer to the data on success. NULL on failure.

Definition at line 326 of file hlist.c.

{
        if (orphan == NULL)
                return NULL;

        return orphan->data_element;
}

struct hlorphan* hlorphan_get_next ( struct hlorphan orphan  )  [read]

Get next element for the given element.

Parameters:
orphan Pointer to the element that has the next field.
Returns:
Pointer to the element on success. NULL on failure.

Definition at line 286 of file hlist.c.

{
        if (orphan == NULL)
                return NULL;

        return orphan->next;
}

struct hlorphan* hlorphan_get_prev ( struct hlorphan orphan  )  [read]

Get previous element for the given element.

Parameters:
orphan Pointer to the element that has the previous field.
Returns:
Pointer to the element on success. NULL on failure.

Definition at line 306 of file hlist.c.

{
        if (orphan == NULL)
                return NULL;

        return orphan->prev;
}

int hlorphan_set_next ( struct hlorphan orphan,
struct hlorphan next 
)

Set next field for the given element.

Parameters:
orphan Pointer to the element that has the next field.
next Pointer to the next element.
Returns:
0 on success. Negative on failure.

Definition at line 295 of file hlist.c.

{
        if (orphan == NULL)
                return -1;

        orphan->next = next;

        return 0;
}

int hlorphan_set_prev ( struct hlorphan orphan,
struct hlorphan prev 
)

Set previous field for the given element.

Parameters:
orphan Pointer to the element that has the previous field.
prev Pointer to the previous element.
Returns:
0 on success. Negative on failure.

Definition at line 315 of file hlist.c.

{
        if (orphan == NULL)
                return -1;

        orphan->prev = prev;

        return 0;
}

 All Data Structures Files Functions Variables Enumerations Enumerator Defines