<commit_before>/**
 * @file      dlist.c
 * @brief     Doubly linked list implementation.
 * @author    <NAME>
 * @date      08 Apr 2016
 * @copyright Copyright 2016 <NAME>. All rights reserved.
 *            This project is released under the MIT license.
 */

#include "dlist.h"
#include "logger.h"

#include <errno.h>

/**
 * @see See header file for interface comments.
 */
bool dlist_insertafter(struct dlist * const list,
                       struct dlist_node * const node,
                       void * const val)
{
    bool ret = false;
    struct dlist_node *new = NULL;

    if (list == NULL)
    {
        logger_printf(LOGGER_LEVEL_ERROR,
                      "%s: parameter validation failed\n",
                      __FUNCTION__);
    }
    else
    {
        new = malloc(sizeof(struct dlist_node));

        if (new == NULL)
        {
            logger_printf(LOGGER_LEVEL_ERROR,
                          "%s: failed to allocate node (%d)\n",
                          __FUNCTION__,
                          errno);
        }
        else
        {
            new->val = val;

            if (list->size == 0)
            {
                new->next  = new->prev  = NULL;
                list->head = list->tail = new;
            }
            else if (list->tail == node)
            {
                list->tail       = new;
                list->tail->next = NULL;
                list->tail->prev = node;
                node->next       = list->tail;
            }
            else
            {
                new->next = node->next;
                new->prev = node;

                (node->next)->prev = new;
                node->next         = new;
            }

            list->size++;
            ret = true;
        }
    }

    return ret;
}

/**
 * @see See header file for interface comments.
 */
bool dlist_insertbefore(struct dlist * const list,
                        struct dlist_node * const node,
                        void * const val)
{
    bool ret = false;
    struct dlist_node *new = NULL;

    if (list == NULL)
    {
        logger_printf(LOGGER_LEVEL_ERROR,
                      "%s: parameter validation failed\n",
                      __FUNCTION__);
    }
    else
    {
        new = malloc(sizeof(struct dlist_node));

        if (new == NULL)
        {
            logger_printf(LOGGER_LEVEL_ERROR,
                          "%s: failed to allocate node (%d)\n",
                          __FUNCTION__,
                          errno);
        }
        else
        {
            new->val = val;

            if (list->size == 0)
            {
                new->next  = new->prev  = NULL;
                list->head = list->tail = new;
            }
            else if (list->head == node)
            {
                list->head       = new;
                list->head->next = node;
                list->head->prev = NULL;
                node->prev       = list->head;
            }
            else
            {
                new->next = node;
                new->prev = node->prev;

                (node->prev)->next = new;
                node->prev         = new;
            }

            list->size++;
            ret = true;
        }
    }

    return ret;
}

/**
 * @see See header file for interface comments.
 */
bool dlist_inserthead(struct dlist * const list, void * const val)
{
    return dlist_insertbefore(list, list->head, val);
}

/**
 * @see See header file for interface comments.
 */
bool dlist_inserttail(struct dlist * const list, void * const val)
{
    return dlist_insertafter(list, list->tail, val);
}

/**
 * @see See header file for interface comments.
 */
bool dlist_remove(struct dlist * const list, struct dlist_node * const node)
{
    bool ret = false;

    if ((list == NULL) || (list->size == 0) || (node == NULL))
    {
        logger_printf(LOGGER_LEVEL_ERROR,
                      "%s: parameter validation failed\n",
                      __FUNCTION__);
    }
    else
    {
        if (list->size == 1)
        {
            list->head = list->tail = NULL;
        }
        else if (list->head == node)
        {
            list->head       = node->next;
            list->head->prev = NULL;
        }
        else if (list->tail == node)
        {
            list->tail       = node->prev;
            list->tail->next = NULL;
        }
        else
        {
            (node->next)->prev = node->prev;
            (node->prev)->next = node->next;
        }

        free(node);

        list->size--;
        ret = true;
    }

    return ret;
}

/**
 * @see See header file for interface comments.
 */<commit_msg>dlist: updated memory allocation APIs<commit_after>/**
 * @file      dlist.c
 * @brief     Doubly linked list implementation.
 * @author    <NAME>
 * @date      08 Apr 2016
 * @copyright Copyright 2016 <NAME>. All rights reserved.
 *            This project is released under the MIT license.
 */

#include "dlist.h"
#include "logger.h"
#include "util_mem.h"

#include <errno.h>

/**
 * @see See header file for interface comments.
 */
bool dlist_insertafter(struct dlist * const list,
                       struct dlist_node * const node,
                       void * const val)
{
    bool ret = false;
    struct dlist_node *new = NULL;

    if (list == NULL)
    {
        logger_printf(LOGGER_LEVEL_ERROR,
                      "%s: parameter validation failed\n",
                      __FUNCTION__);
    }
    else
    {
        new = UTILMEM_MALLOC(struct dlist_node, 1);

        if (new == NULL)
        {
            logger_printf(LOGGER_LEVEL_ERROR,
                          "%s: failed to allocate node (%d)\n",
                          __FUNCTION__,
                          errno);
        }
        else
        {
            new->val = val;

            if (list->size == 0)
            {
                new->next  = new->prev  = NULL;
                list->head = list->tail = new;
            }
            else if (list->tail == node)
            {
                list->tail       = new;
                list->tail->next = NULL;
                list->tail->prev = node;
                node->next       = list->tail;
            }
            else
            {
                new->next = node->next;
                new->prev = node;

                (node->next)->prev = new;
                node->next         = new;
            }

            list->size++;
            ret = true;
        }
    }

    return ret;
}

/**
 * @see See header file for interface comments.
 */
bool dlist_insertbefore(struct dlist * const list,
                        struct dlist_node * const node,
                        void * const val)
{
    bool ret = false;
    struct dlist_node *new = NULL;

    if (list == NULL)
    {
        logger_printf(LOGGER_LEVEL_ERROR,
                      "%s: parameter validation failed\n",
                      __FUNCTION__);
    }
    else
    {
        new = UTILMEM_MALLOC(struct dlist_node, 1);

        if (new == NULL)
        {
            logger_printf(LOGGER_LEVEL_ERROR,
                          "%s: failed to allocate node (%d)\n",
                          __FUNCTION__,
                          errno);
        }
        else
        {
            new->val = val;

            if (list->size == 0)
            {
                new->next  = new->prev  = NULL;
                list->head = list->tail = new;
            }
            else if (list->head == node)
            {
                list->head       = new;
                list->head->next = node;
                list->head->prev = NULL;
                node->prev       = list->head;
            }
            else
            {
                new->next = node;
                new->prev = node->prev;

                (node->prev)->next = new;
                node->prev         = new;
            }

            list->size++;
            ret = true;
        }
    }

    return ret;
}

/**
 * @see See header file for interface comments.
 */
bool dlist_inserthead(struct dlist * const list, void * const val)
{
    return dlist_insertbefore(list, list->head, val);
}

/**
 * @see See header file for interface comments.
 */
bool dlist_inserttail(struct dlist * const list, void * const val)
{
    return dlist_insertafter(list, list->tail, val);
}

/**
 * @see See header file for interface comments.
 */
bool dlist_remove(struct dlist * const list, struct dlist_node * const node)
{
    bool ret = false;

    if ((list == NULL) || (list->size == 0) || (node == NULL))
    {
        logger_printf(LOGGER_LEVEL_ERROR,
                      "%s: parameter validation failed\n",
                      __FUNCTION__);
    }
    else
    {
        if (list->size == 1)
        {
            list->head = list->tail = NULL;
        }
        else if (list->head == node)
        {
            list->head       = node->next;
            list->head->prev = NULL;
        }
        else if (list->tail == node)
        {
            list->tail       = node->prev;
            list->tail->next = NULL;
        }
        else
        {
            (node->next)->prev = node->prev;
            (node->prev)->next = node->next;
        }

        UTILMEM_FREE(node);

        list->size--;
        ret = true;
    }

    return ret;
}

/**
 * @see See header file for interface comments.
 */
{'fraction_non_alphanumeric': 0.1163212988677633, 'fraction_numerical': 0.003204443494979705, 'mean_word_length': 1.1593634686346863, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 139, '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': '2581842', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2558, 'n_words': 656}