luz-list.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/* luz - MicroRaptor Gui
 * Copyright (c) 2014 Øyvind Kolås <pippin@hodefoting.com>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __LUZ_LIST__
#define  __LUZ_LIST__

#include <stdlib.h>

/* The whole luz_list implementation is in the header and will be inlined
 * wherever it is used.
 */

typedef struct _LuzList LuzList;
  struct _LuzList {void *data;LuzList *next;
  void (*freefunc)(void *data, void *freefunc_data);
  void *freefunc_data;
}
;
void luz_list_free (LuzList **list);

void luz_list_prepend_full (LuzList **list, void *data,
    void (*freefunc)(void *data, void *freefunc_data),
    void *freefunc_data)
{
  LuzList *new_=calloc (sizeof (LuzList), 1);
  new_->next=*list;
  new_->data=data;
  new_->freefunc=freefunc;
  new_->freefunc_data = freefunc_data;
  *list = new_;
}

int luz_list_length (LuzList *list)
{
  int length = 0;
  LuzList *l;
  for (l = list; l; l = l->next, length++);
  return length;
}

void luz_list_prepend (LuzList **list, void *data)
{
  LuzList *new_=calloc (sizeof (LuzList), 1);
  new_->next= *list;
  new_->data=data;
  *list = new_;
}

void
luz_list_reverse (LuzList **list)
{
  LuzList *new_ = NULL;
  LuzList *l;
  for (l = *list; l; l=l->next)
    luz_list_prepend (&new_, l->data);
  luz_list_free (list);
  *list = new_;
}

void luz_list_append_full (LuzList **list, void *data,
    void (*freefunc)(void *data, void *freefunc_data),
    void *freefunc_data)
{
  LuzList *new_= calloc (sizeof (LuzList), 1);
  new_->data=data;
  new_->freefunc = freefunc;
  new_->freefunc_data = freefunc_data;
  if (*list)
    {
      LuzList *last;
      for (last = *list; last->next; last=last->next);
      last->next = new_;
      return;
    }
  *list = new_;
  return;
}

void luz_list_append (LuzList **list, void *data)
{
  luz_list_append_full (list, data, NULL, NULL);
}

void luz_list_remove (LuzList **list, void *data)
{
  LuzList *iter, *prev = NULL;
  if ((*list)->data == data)
    {
      if ((*list)->freefunc)
        (*list)->freefunc ((*list)->data, (*list)->freefunc_data);
      prev = (void*)(*list)->next;
      free (*list);
      *list = prev;
      return;
    }
  for (iter = *list; iter; iter = iter->next)
    if (iter->data == data)
      {
        if (iter->freefunc)
          iter->freefunc (iter->data, iter->freefunc_data);
        prev->next = iter->next;
        free (iter);
        break;
      }
    else
      prev = iter;
}

void luz_list_free (LuzList **list)
{
  while (*list)
    luz_list_remove (list, (*list)->data);
}

LuzList *luz_list_nth (LuzList *list, int no)
{
  while(no-- && list)
    list = list->next;
  return list;
}

LuzList *luz_list_find (LuzList *list, void *data)
{
  for (;list;list=list->next)
    if (list->data == data)
      break;
  return list;
}

static LuzList*
luz_list_merge_sorted (LuzList* list1,
                       LuzList* list2,
    int(*compare)(const void *a, const void *b, void *userdata), void *userdata
)
{
  if (list1 == NULL)
     return(list2);
  else if (list2==NULL)
     return(list1);

  if (compare (list1->data, list2->data, userdata) >= 0)
  {
    list1->next = luz_list_merge_sorted (list1->next,list2, compare, userdata);
    /*list1->next->prev = list1;
      list1->prev = NULL;*/
    return list1;
  }
  else
  {
    list2->next = luz_list_merge_sorted (list1,list2->next, compare, userdata);
    /*list2->next->prev = list2;
      list2->prev = NULL;*/
    return list2;
  }
}

static void
luz_list_split_half (LuzList*  head,
                     LuzList** list1,
                     LuzList** list2)
{
  LuzList* fast;
  LuzList* slow;
  if (head==NULL || head->next==NULL)
  {
    *list1 = head;
    *list2 = NULL;
  }
  else
  {
    slow = head;
    fast = head->next;

    while (fast != NULL)
    {
      fast = fast->next;
      if (fast != NULL)
      {
        slow = slow->next;
        fast = fast->next;
      }
    }

    *list1 = head;
    *list2 = slow->next;
    slow->next = NULL;
  }
}

void luz_list_sort (LuzList **head,
    int(*compare)(const void *a, const void *b, void *userdata),
    void *userdata)
{
  LuzList* list1;
  LuzList* list2;

  /* Base case -- length 0 or 1 */
  if ((*head == NULL) || ((*head)->next == NULL))
  {
    return;
  }

  luz_list_split_half (*head, &list1, &list2);
  luz_list_sort (&list1, compare, userdata);
  luz_list_sort (&list2, compare, userdata);
  *head = luz_list_merge_sorted (list1, list2, compare, userdata);
}

void
luz_list_insert_sorted (LuzList **list, void *data,
                       int(*compare)(const void *a, const void *b, void *userdata),
                       void *userdata)
{
  luz_list_prepend (list, data);
  luz_list_sort (list, compare, userdata);
}

static inline void *luz_list_last (LuzList *list)
{
  if (list)
    {
      LuzList *last;
      for (last = list; last->next; last=last->next);
      return last->data;
    }
  return NULL;
}

#endif