summaryrefslogtreecommitdiff
path: root/src/tiblst.c
blob: c908c9227c452fe45a3e1cc3fb463fbaef24e9d5 (plain)
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
/*
 *  libtib - Read, write, and evaluate TI BASIC programs
 *  Copyright (C) 2015-2016 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  This program 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>

#include "tiberr.h"
#include "tiblst.h"

struct tib_lst *
tib_new_lst ()
{
  struct tib_lst *out = malloc (sizeof (struct tib_lst));
  out->beg = NULL;
  out->end = NULL;

  return out;
}

static struct tib_el *
el_ref (const struct tib_lst *lst, int index)
{
  for (struct tib_el *el = lst->beg; el != NULL; el = el->next, --index)
    if (0 == index)
      return el;

  return NULL;
}

void
tib_free_lst (struct tib_lst *lst)
{
  while (tib_lst_len (lst))
    tib_lst_remove (lst, 0);

  free (lst);
}

int
tib_lst_insert (struct tib_lst *lst, TIB *t, int index)
{
  int len = tib_lst_len (lst);

  if (index > len)
    return TIB_EINDEX;

  struct tib_el *new = malloc (sizeof (struct tib_el));
  if (NULL == new)
    return TIB_EALLOC;

  tib_incref (t);
  new->val = t;

  if (0 == index)
    new->prev = NULL;
  else
    new->prev = el_ref (lst, index - 1);

  if (index == len)
    new->next = NULL;
  else
    new->next = el_ref (lst, index);

  if (new->next)
    new->next->prev = new;
  else
    lst->end = new;

  if (new->prev)
    new->prev->next = new;
  else
    lst->beg = new;

  return 0;
}

int
tib_lst_push (struct tib_lst *lst, TIB *t)
{
  return tib_lst_insert (lst, t, tib_lst_len (lst));
}

void
tib_lst_remove (struct tib_lst *lst, int index)
{
  struct tib_el *e = el_ref (lst, index);
  if (!e)
    return;

  if (e->next)
    e->next->prev = e->prev;
  else
    lst->end = e->prev;

  if (e->prev)
    e->prev->next = e->next;
  else
    lst->beg = e->next;

  tib_decref (e->val);
  free (e);
}

int
tib_lst_len (const struct tib_lst *lst)
{
  int i = 0;
  struct tib_el *e = lst->beg;

  while (e != NULL)
    {
      ++i;
      e = e->next;
    }

  return i;
}

TIB *
tib_lst_ref (const struct tib_lst *lst, int index)
{
  struct tib_el *i;
  int looped = 0;

  for (i = lst->beg; i != NULL; i = i->next)
    {
      if (looped++ == index)
        return i->val;
    }

  return NULL;
}