-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.c
More file actions
121 lines (103 loc) · 3.04 KB
/
Copy patharray.c
File metadata and controls
121 lines (103 loc) · 3.04 KB
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
/*
Repair -- an implementation of Larsson and Moffat's compression and
decompression algorithms.
Copyright (C) 2010-current_year Gonzalo Navarro
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Author's contact: Gonzalo Navarro, Dept. of Computer Science, University of
Chile. Blanco Encalada 2120, Santiago, Chile. gnavarro@dcc.uchile.cl
*/
// extendible array for pairs
#include <stdlib.h>
#include "array.h"
#include "records.h"
int insertArray (Tarray *A, int pair)
{ int *npairs;
int max,size,i,pos,id,fst;
Trecord *rec = ((Trarray*)A->Rec)->records;
if (A->size == A->maxsize)
{ if (A->maxsize == 0)
{ A->maxsize = A->minsize;
A->pairs = mymalloc (A->maxsize * sizeof(int),__LINE__,__FILE__);
A->fst = 0;
}
else
{ max = A->maxsize;
A->maxsize /= A->factor;
npairs = mymalloc (A->maxsize * sizeof(int),__LINE__,__FILE__);
size = A->size;
fst = A->fst;
for (i=0;i<size;i++)
{ id = A->pairs[fst];
npairs[i] = id;
rec[id].hpos = i;
fst = (fst+1) % max;
}
free(A->pairs);
A->pairs = npairs;
A->fst = 0;
}
}
pos = (A->fst + A->size) % A->maxsize;
A->pairs[pos] = pair;
A->size++;
return pos;
}
void deleteArray (Tarray *A)
{ int *npairs;
int size,i,id,max,fst;
Trecord *rec = ((Trarray*)A->Rec)->records;
A->fst = (A->fst+1) % A->maxsize;
A->size--;
if (A->size == 0)
{ A->maxsize = 0;
free (A->pairs);
A->pairs = NULL;
A->fst = 0;
}
else if ((A->size < A->maxsize * A->factor * A->factor) &&
(A->maxsize * A->factor >= A->minsize))
{ max = A->maxsize;
A->maxsize *= A->factor;
npairs = mymalloc (A->maxsize * sizeof(int),__LINE__,__FILE__);
size = A->size;
fst = A->fst;
for (i=0;i<size;i++)
{ id = A->pairs[fst];
npairs[i] = id;
rec[id].hpos = i;
fst = (fst+1) % max;
}
free(A->pairs);
A->pairs = npairs;
A->fst = 0;
}
}
Tarray createArray (void *Rec, float factor, int minsize)
{ Tarray A;
A.Rec = Rec;
A.pairs = NULL;
A.maxsize = 0;
A.size = 0;
A.fst = 0;
A.factor = factor;
A.minsize = minsize;
return A;
}
void destroyArray (Tarray *A)
{ if (A->maxsize == 0) return;
free (A->pairs);
A->pairs = NULL;
A->maxsize = 0;
A->size = 0;
A->fst = 0;
}