<commit_before>/**
 * @file pwm_list.c
 * @brief
 * @author <NAME>
 * @version 0.0.1
 * @date 2015-09-13
 */

#include <libudev.h>

#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include "pwm_internal.h"
#include "pwm.h"

/**
 * @brief Create a new list to hold pwms.
 *
 * @return A new empty list to hold pwms.
 */
struct us_pwm_list_t *
us_pwm_list_new()
{
  struct us_pwm_list_t *list = NULL;
  list = calloc(sizeof(struct us_pwm_list_t), 1);
  usp_ref_init(list, us_pwm_list_delete);
  return list;
}

/**
 * @brief Destroy the list of pwms. This simply decrements the refcount on
 * each of the items in the list by one. If something ref'd a pwm it will
 * still prevent the pwm from being free'd until its refcount goes to 0.
 *
 * @param ctx The list of pwms to delete.
 */
void
us_pwm_list_delete(void *ctx)
{
  struct us_pwm_list_t *list = ctx;
  struct us_pwm_list_entry_t *entry, *next;
  us_pwm_list_foreach_safe(list, entry, next) {
    us_pwm_unref(entry->uspl_pwm);
    free(entry);
  }
  free(list);
}

/**
 * @brief Add a pwm to a list of pwms. Note: a pwm can only belong
 * to one list of pwms.
 *
 * @param list The list of pwms to add the pwm to.
 * @param pwm The pwm to add to the list of pwms.
 *
 * @return A status code.
 */
int
us_pwm_list_add(struct us_pwm_list_t *list, struct us_pwm_t *pwm)
{
  struct us_pwm_list_entry_t *list_entry = NULL;
  assert(list != NULL);
  assert(pwm != NULL);

  list_entry = malloc(sizeof(struct us_pwm_list_entry_t));
  list_entry->uspl_pwm = pwm;
  usp_ref(pwm);

  list->uspl_count++;

  if(list->uspl_head != NULL) {
    list_entry->uspl_next = list->uspl_head;
  }
  list->uspl_head = list_entry;

  return USP_OK;
}

/**
 * @brief Get the head of the list.<commit_msg>I swear I know how to linked list<commit_after>/**
 * @file pwm_list.c
 * @brief
 * @author <NAME>
 * @version 0.0.1
 * @date 2015-09-13
 */

#include <libudev.h>

#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include "pwm_internal.h"
#include "pwm.h"

/**
 * @brief Create a new list to hold pwms.
 *
 * @return A new empty list to hold pwms.
 */
struct us_pwm_list_t *
us_pwm_list_new()
{
  struct us_pwm_list_t *list = NULL;
  list = malloc(sizeof(struct us_pwm_list_t));
  list->uspl_count = 0;
  list->uspl_ctrl = NULL;
  list->uspl_head = NULL;
  usp_ref_init(list, us_pwm_list_delete);
  return list;
}

/**
 * @brief Destroy the list of pwms. This simply decrements the refcount on
 * each of the items in the list by one. If something ref'd a pwm it will
 * still prevent the pwm from being free'd until its refcount goes to 0.
 *
 * @param ctx The list of pwms to delete.
 */
void
us_pwm_list_delete(void *ctx)
{
  struct us_pwm_list_t *list = ctx;
  struct us_pwm_list_entry_t *entry, *next;
  us_pwm_list_foreach_safe(list, entry, next) {
    us_pwm_unref(entry->uspl_pwm);
    free(entry);
  }
  free(list);
}

/**
 * @brief Add a pwm to a list of pwms. Note: a pwm can only belong
 * to one list of pwms.
 *
 * @param list The list of pwms to add the pwm to.
 * @param pwm The pwm to add to the list of pwms.
 *
 * @return A status code.
 */
int
us_pwm_list_add(struct us_pwm_list_t *list, struct us_pwm_t *pwm)
{
  struct us_pwm_list_entry_t *list_entry = NULL;
  assert(list != NULL);
  assert(pwm != NULL);

  list_entry = malloc(sizeof(struct us_pwm_list_entry_t));
  list_entry->uspl_pwm = pwm;
  usp_ref(pwm);

  list->uspl_count++;

  list_entry->uspl_next = list->uspl_head;
  list->uspl_head = list_entry;
  return USP_OK;
}

/**
 * @brief Get the head of the list.
{'fraction_non_alphanumeric': 0.12238723872387239, 'fraction_numerical': 0.007150715071507151, 'mean_word_length': 3.6508951406649617, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, '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': '3046147', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1469, 'n_words': 421}