summaryrefslogtreecommitdiff
path: root/cd/src/intcgm/list.c
blob: 6fefbbcc6a1d2e1443cadacd344f51a3ba5be0dc (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
/*
* list.c
* Generic List Manager
* TeCGraf
*
*/

#include <stdio.h>
#include <stdlib.h>
#include "list.h"

TList *cgm_NewList ( void )
{
 TList *l = (TList *) malloc ( sizeof (TList) );

 list_nba(l) = 8;
 list_head(l) = (void **)malloc(list_nba(l)*sizeof(void*));
 list_n(l) = 0;

 return l;
}

TList *cgm_AppendList ( TList *l, void *i )
{
 if ( l == NULL )
   return NULL;

 if (list_n(l) == list_nba(l))
 {
  list_nba(l) += 32;
  list_head(l) = (void **)realloc(list_head(l),list_nba(l)*sizeof(void*));
 }

 list_head(l)[list_n(l)]=i;
 list_n(l)++;

 return l;
}

TList *cgm_AddList ( TList *l, int n, void *info )
{
 int i;

 if ( l == NULL)
   return NULL;

 if ( n < 1)
  n=1;

 if ( n > list_n(l) )
   return cgm_AppendList( l, info );

 --n;           /* o usuario ve a lista iniciando em 1 e a */
                /* lista e' implementada iniciando em 0     */

 if (list_n(l) == list_nba(l))
 {
  list_nba(l) *= 2;
  list_head(l) = (void **)realloc(list_head(l),list_nba(l)*sizeof(void*));
 }

 for (i=list_n(l)-1; i>=n; --i)
   list_head(l)[i+1]=list_head(l)[i];

 list_head(l)[n]=info;
 list_n(l)++;
 return l;
}

TList *cgm_DelList ( TList *l, int n )
{
 int i;

 if ( l == NULL || list_n(l) == 0 || n < 0)
   return NULL;

 if ( n < 1)
  n=1;

 if ( n > list_n(l))
  n=list_n(l);

 --n;           /* o usuario ve a lista iniciando em 1 e a */
                /* lista e' implementada iniciando em 0     */
 list_n(l)--;

 for (i=n; i<list_n(l); ++i)
   list_head(l)[i]=list_head(l)[i+1];

 return l;
}

void *cgm_GetList ( TList *l, int n )
{
 if ( l == NULL || n <= 0)
   return NULL;

 return n > list_n(l) ? NULL : list_head(l)[n-1];
}

int cgm_DelEntry ( TList *l, void *entry )
{
 int i=1;

 if ( l == NULL || list_n(l) == 0)
   return 0;

 for (i=0; i< list_n(l); ++i)
  if (list_head(l)[i]==entry)
  {
   cgm_DelList(l,i+1);              /* o usuario ve a lista iniciando em 1 e a */
                                /* lista e' implementada iniciando em 0     */
   return i+1;
  }
 return 0;
}