<commit_before>/**
 * The default implementation of single and double linked list.
 *
 * @file list.c
 * @author <NAME>
 * @date 12 Jun 2014
 */

#include "list.h"

/**
 * Struct of the list data structure.
 */
struct List {
	/**
	 * The handle of the first node of this list.
	 * The value NULL signals the list is empty.
	 */
	Node root;
	/**
	 * The handle of the last node of this list.
	 * The value NULL signals the list is empty.
	 */
	Node head;
	/**
	 * The optional assigned destroyHandler which should
	 * be used for node deallocation inside this list.
	 */
	NodeHandler destroyDataHandler;
	/**
	 * The count of nodes in the list.
	 */
	size_t elementCount;
	/**
	 * A flag which indicates that this list is a douple linked list (in the case of true)
	 * or a single linked list in the case of false.
	 */
	bool isDoupleLinkedList;
};


/**
 * Determines the predecessor (previous node) of a specified node.
 *
 * This function is used as fall back implementation for a single linked list
 * which doesn't provide a connection between a node and his predecessor.
 *
 * NOTE: this function is only used internaly inside the list implementation.
 *
 * @param[in] rootNode	The handle of the root node of this list.
 * @param[in] node		The handle of the specified node for which the predecessor should be determined.
 *
 * @return 	The node handle of the predecessor or
 * 		 	NULL if the current node doesn't have a predecessor.
 */
static
Node getPrevNode(Node rootNode, Node node) {
	Node currentNode = rootNode;

	if(rootNode == node) return NULL;

	while(currentNode != NULL && (Node_getNext(currentNode) != node)) {
		currentNode = Node_getNext(currentNode);
	}

	return currentNode;
}

List List_create(bool isDoupleLinkedList, NodeHandler destroyHandler) {
	List list = (List) malloc(sizeof(struct List));
	if(list == NULL) {
		fprintf(stderr, "List creation failed: \"%s\"", strerror(errno));
		return NULL;
	}

	list->elementCount = 0;
	list->root = NULL;
	list->head = NULL;
	list->destroyDataHandler = destroyHandler;
	list->isDoupleLinkedList = isDoupleLinkedList;

	return list;
}

bool List_addFirst(List list, void *data) {
	Node newNode = Node_create(data, list->isDoupleLinkedList);
	if(newNode == NULL) {
		return false;
	}
	List_insertNodeAt(list,newNode, list->root, BEFORE);
	return true;
}

bool List_addLast(List list, void *data) {
	Node newNode = Node_create(data, list->isDoupleLinkedList);
	if(newNode == NULL) {
		return false;
	}
	List_insertNodeAt(list,newNode, list->head, AFTER);
	return true;
}


Node List_insertNodeAt(List list, Node newNode, Node position, NodeInsertDirection dir) {

	if(list == NULL || newNode == NULL  ) {
		return NULL;
	}

	// is the list empty ?
	if(list->root == NULL) {
		list->head = newNode;
		list->root = newNode;
		list->elementCount++;
		return newNode;
	}

	if(position == NULL) return NULL;
<commit_msg>Ensures that the functions List_addFirst and List_addLast returns the correct return value.<commit_after>/**
 * The default implementation of single and double linked list.
 *
 * @file list.c
 * @author <NAME>
 * @date 12 Jun 2014
 */

#include "list.h"

/**
 * Struct of the list data structure.
 */
struct List {
	/**
	 * The handle of the first node of this list.
	 * The value NULL signals the list is empty.
	 */
	Node root;
	/**
	 * The handle of the last node of this list.
	 * The value NULL signals the list is empty.
	 */
	Node head;
	/**
	 * The optional assigned destroyHandler which should
	 * be used for node deallocation inside this list.
	 */
	NodeHandler destroyDataHandler;
	/**
	 * The count of nodes in the list.
	 */
	size_t elementCount;
	/**
	 * A flag which indicates that this list is a douple linked list (in the case of true)
	 * or a single linked list in the case of false.
	 */
	bool isDoupleLinkedList;
};


/**
 * Determines the predecessor (previous node) of a specified node.
 *
 * This function is used as fall back implementation for a single linked list
 * which doesn't provide a connection between a node and his predecessor.
 *
 * NOTE: this function is only used internaly inside the list implementation.
 *
 * @param[in] rootNode	The handle of the root node of this list.
 * @param[in] node		The handle of the specified node for which the predecessor should be determined.
 *
 * @return 	The node handle of the predecessor or
 * 		 	NULL if the current node doesn't have a predecessor.
 */
static
Node getPrevNode(Node rootNode, Node node) {
	Node currentNode = rootNode;

	if(rootNode == node) return NULL;

	while(currentNode != NULL && (Node_getNext(currentNode) != node)) {
		currentNode = Node_getNext(currentNode);
	}

	return currentNode;
}

List List_create(bool isDoupleLinkedList, NodeHandler destroyHandler) {
	List list = (List) malloc(sizeof(struct List));
	if(list == NULL) {
		fprintf(stderr, "List creation failed: \"%s\"", strerror(errno));
		return NULL;
	}

	list->elementCount = 0;
	list->root = NULL;
	list->head = NULL;
	list->destroyDataHandler = destroyHandler;
	list->isDoupleLinkedList = isDoupleLinkedList;

	return list;
}

bool List_addFirst(List list, void *data) {
	Node newNode = Node_create(data, list->isDoupleLinkedList);
	if(newNode == NULL) {
		return false;
	}
	return List_insertNodeAt(list,newNode, list->root, BEFORE) == newNode;
}

bool List_addLast(List list, void *data) {
	Node newNode = Node_create(data, list->isDoupleLinkedList);
	if(newNode == NULL) {
		return false;
	}
	return List_insertNodeAt(list,newNode, list->head, AFTER) == newNode;
}


Node List_insertNodeAt(List list, Node newNode, Node position, NodeInsertDirection dir) {

	if(list == NULL || newNode == NULL  ) {
		return NULL;
	}

	// is the list empty ?
	if(list->root == NULL) {
		list->head = newNode;
		list->root = newNode;
		list->elementCount++;
		return newNode;
	}

	if(position == NULL) return NULL;

{'fraction_non_alphanumeric': 0.10615384615384615, 'fraction_numerical': 0.002393162393162393, 'mean_word_length': 3.7998359310910583, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4365431', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1803, 'n_words': 667}