Commit 778acc48 by Celine Mercier

Added linked lists to handle lists of column pointers in views (not

tested)
parent 3319ede8
......@@ -4,6 +4,7 @@
../../../src/dna_seq_indexer.c
../../../src/encode.c
../../../src/hashtable.c
../../../src/linked_list.c
../../../src/murmurhash2.c
../../../src/obi_align.c
../../../src/obiavl.c
......
/****************************************************************************
* Linked list source file *
****************************************************************************/
/**
* @file linked_list.c
* @author Celine Mercier
* @date February 22th 2017
* @brief Source file for linked list functions.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "linked_list.h"
/**
* @brief Creates a new node.
*
* @warning The returned pointer has to be freed by the caller.
*
* @returns A pointer on the new node.
* @retval NULL if an error occurred.
*
* @since February 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
static Linked_list_node_p create_node();
static Linked_list_node_p create_node()
{
Linked_list_node_p node;
node = malloc(sizeof(Linked_list_node_t));
if (node == NULL)
return NULL;
node->value = NULL;
node->previous = NULL;
node->next = NULL;
return node;
}
// Add a value at the end of a linked list
Linked_list_node_p ll_add(Linked_list_node_p head, void* value)
{
Linked_list_node_p node = head;
Linked_list_node_p new_node = NULL;
// First node
if (head == NULL)
{
head = create_node();
if (head == NULL)
return NULL;
head->value = value;
}
else
{
while (node->next != NULL)
node = node->next;
new_node = create_node();
if (new_node == NULL)
return NULL;
node->next = new_node;
new_node->previous = node;
new_node->value = value;
}
return head;
}
// Set a value at a given index of the list
int ll_set(Linked_list_node_p head, int idx, void* value)
{
int i = 0;
Linked_list_node_p node = head;
while ((node != NULL) && (i < idx))
{
node = node->next;
i++;
}
if (node == NULL) // End of list reached before index
return -1;
node->value = value;
return 0;
}
// Get a node with its index
Linked_list_node_p ll_get(Linked_list_node_p head, int idx)
{
int i = 0;
Linked_list_node_p node = head;
while ((node != NULL) && (i < idx))
{
node = node->next;
i++;
}
return node;
}
// Delete a node
Linked_list_node_p ll_delete(Linked_list_node_p head, int idx) // TODO or with value?
{
int i = 0;
Linked_list_node_p node = head;
while ((node != NULL) && (i < idx))
{
node = node->next;
i++;
}
if (node == NULL) // Node didn't exist
return NULL;
if (node->previous != NULL)
(node->previous)->next = node->next;
else // deleting head node: head changes
head = node->next;
if (node->next != NULL)
(node->next)->previous = node->previous;
free(node);
return head;
}
// Free the linked list
void ll_free(Linked_list_node_p head)
{
Linked_list_node_p node = head;
Linked_list_node_p previous = head;
while (node != NULL)
{
previous = node;
node = node->next;
free(previous);
}
}
// Get the index of a node from its value (TODO useless?) -- kinda assumes unique values
//int ll_get_index(Linked_list_node_p head, void* value)
//{
//
//}
/****************************************************************************
* Linked list header file *
****************************************************************************/
/**
* @file linked_list.h
* @author Celine Mercier
* @date February 22th 2017
* @brief Header file for linked list functions.
*/
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
/**
* @brief Structure for a node in a double linked chain.
*/
typedef struct Linked_list_node {
void* value; /**< A pointer (the value kept).
*/
struct Linked_list_node* next; /**< A pointer on the next node.
*/
struct Linked_list_node* previous; /**< A pointer on the previous node.
*/
} Linked_list_node_t, *Linked_list_node_p;
/**
* @brief Adds a new node at the end of a linked list.
*
* Works even if it is the first node.
*
* @param head A pointer on the first node of the linked list, or NULL if the list is empty.
* @param value The value to associate with the node.
*
* @returns A pointer on the new head node of the linked list.
* @retval NULL if an error occurred.
*
* @since February 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
Linked_list_node_p ll_add(Linked_list_node_p head, void* value);
/**
* @brief Sets a value at a given index of the list.
*
* @param head A pointer on the first node of the linked list, or NULL if the list is empty.
* @param idx The index of the node at which the value should be changed.
* @param value The new value to associate with the node.
*
* @returns A value indicating the success of the operation.
* @retval 0 if the operation was successfully completed.
* @retval -1 if an error occurred.
*
* @since February 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
int ll_set(Linked_list_node_p head, int idx, void* value);
/**
* @brief Gets a node from its index.
*
* @warning The pointer returned is a pointer on the node and not on the value.
*
* @param head A pointer on the first node of the linked list, or NULL if the list is empty.
* @param idx The index of the node to retrieve.
*
* @returns A pointer on the retrieved node.
* @retval NULL if an error occurred.
*
* @since February 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
Linked_list_node_p ll_get(Linked_list_node_p head, int idx);
/**
* @brief Deletes a node.
*
* @param head A pointer on the first node of the linked list.
* @param idx The index of the node to delete.
*
* @returns A pointer on the new head node of the linked list.
* @retval NULL if an error occurred.
*
* @since February 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
Linked_list_node_p ll_delete(Linked_list_node_p head, int idx);
/**
* @brief Frees all the nodes of a linked list.
*
* @param head A pointer on the first node of the linked list.
*
* @since February 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
void ll_free(Linked_list_node_p head);
#endif /* LINKED_LIST_H_ */
......@@ -26,6 +26,7 @@
#include "obidmscolumn.h"
#include "obierrno.h"
#include "hashtable.h"
#include "linked_list.h"
#include "obiblob.h"
......@@ -105,25 +106,25 @@ typedef struct Obiview_infos {
* @brief Structure for an opened view.
*/
typedef struct Obiview {
Obiview_infos_p infos; /**< A pointer on the mapped view informations.
Obiview_infos_p infos; /**< A pointer on the mapped view informations.
*/
OBIDMS_p dms; /**< A pointer on the DMS to which the view belongs.
OBIDMS_p dms; /**< A pointer on the DMS to which the view belongs.
*/
bool read_only; /**< Whether the view is read-only or can be modified.
bool read_only; /**< Whether the view is read-only or can be modified.
*/
OBIDMS_column_p line_selection; /**< A pointer on the column containing the line selection
OBIDMS_column_p line_selection; /**< A pointer on the column containing the line selection
* associated with the view if there is one.
* This line selection is read-only, and when a line from the view is read,
* it is this line selection that is used.
*/
OBIDMS_column_p columns[MAX_NB_OPENED_COLUMNS]; /**< Array of pointers on all the columns of the view.
Linked_list_node_p columns; /**< Double linked chain containing the pointers on all the columns of the view.
*/
hashtable_p column_dict; /**< Hash table storing the pairs of column names or aliases with the associated
hashtable_p column_dict; /**< Hash table storing the pairs of column names or aliases with the associated
* pointers on column pointers (OBIDMS_column_p*).
*/
int nb_predicates; /**< Number of predicates to test when closing the view.
*/
char* (**predicate_functions)(struct Obiview* view); /**< Array of pointers on all predicate functions to test when closing the view.
int nb_predicates; /**< Number of predicates to test when closing the view.
*/
char* (**predicate_functions)(struct Obiview* view); /**< Array of pointers on all predicate functions to test when closing the view.
*/
} Obiview_t, *Obiview_p;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment