~ubuntu-branches/ubuntu/lucid/igraph/lucid

« back to all changes in this revision

Viewing changes to src/heap.pmt

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Malaterre
  • Date: 2009-11-16 18:12:42 UTC
  • Revision ID: james.westby@ubuntu.com-20091116181242-mzv9p5fz9uj57xd1
Tags: upstream-0.5.3
ImportĀ upstreamĀ versionĀ 0.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C -*-  */
 
2
/* 
 
3
   IGraph library.
 
4
   Copyright (C) 2007  Gabor Csardi <csardi@rmki.kfki.hu>
 
5
   MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
 
6
   
 
7
   This program is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 2 of the License, or
 
10
   (at your option) any later version.
 
11
   
 
12
   This program is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
   
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software
 
19
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
 
20
   02110-1301 USA
 
21
 
 
22
*/
 
23
 
 
24
#include "memory.h"
 
25
#include "random.h"
 
26
#include "error.h"
 
27
#include "config.h"
 
28
 
 
29
#include <assert.h>
 
30
#include <string.h>             /* memcpy & co. */
 
31
#include <stdlib.h>
 
32
 
 
33
#define PARENT(x)     (((x)+1)/2-1)
 
34
#define LEFTCHILD(x)  (((x)+1)*2-1)
 
35
#define RIGHTCHILD(x) (((x)+1)*2)
 
36
  
 
37
/**
 
38
 * \ingroup heap
 
39
 * \function igraph_heap_init
 
40
 * \brief Initializes an empty heap object.
 
41
 *
 
42
 * Creates an empty heap, but allocates size for some elements.
 
43
 * \param h Pointer to an uninitialized heap object.
 
44
 * \param alloc_size Number of elements to allocate memory for.
 
45
 * \return Error code.
 
46
 * 
 
47
 * Time complexity: O(\p alloc_size), assuming memory allocation is a
 
48
 * linear operation.
 
49
 */
 
50
 
 
51
int FUNCTION(igraph_heap,init)(TYPE(igraph_heap)* h, long int alloc_size) {
 
52
  if (alloc_size <= 0 ) { alloc_size=1; }
 
53
  h->stor_begin=igraph_Calloc(alloc_size, BASE);
 
54
  if (h->stor_begin==0) {
 
55
    IGRAPH_ERROR("heap init failed", IGRAPH_ENOMEM);
 
56
  }
 
57
  h->stor_end=h->stor_begin + alloc_size;
 
58
  h->end=h->stor_begin;
 
59
  h->destroy=1;
 
60
  
 
61
  return 0;  
 
62
}
 
63
 
 
64
/**
 
65
 * \ingroup heap
 
66
 * \function igraph_heap_init_array
 
67
 * \brief Build a heap from an array.
 
68
 * 
 
69
 * Initializes a heap object from an array, the heap is also
 
70
 * built of course (constructor).
 
71
 * \param h Pointer to an uninitialized heap object.
 
72
 * \param data Pointer to an array of base data type.
 
73
 * \param len The length of the array at \p data.
 
74
 * \return Error code.
 
75
 * 
 
76
 * Time complexity: O(n), the number of elements in the heap.
 
77
 */
 
78
 
 
79
int FUNCTION(igraph_heap,init_array)(TYPE(igraph_heap) *h, BASE* data, long int len) {
 
80
  h->stor_begin=igraph_Calloc(len, BASE);
 
81
  if (h->stor_begin==0) {
 
82
    IGRAPH_ERROR("heap init from array failed", IGRAPH_ENOMEM);
 
83
  }
 
84
  h->stor_end=h->stor_begin+len;
 
85
  h->end=h->stor_end;
 
86
  h->destroy=1;
 
87
 
 
88
  memcpy(h->stor_begin, data, len*sizeof(igraph_real_t));
 
89
 
 
90
  FUNCTION(igraph_heap,i_build) (h->stor_begin, h->end-h->stor_begin, 0);
 
91
  
 
92
  return 0;
 
93
}
 
94
 
 
95
/**
 
96
 * \ingroup heap
 
97
 * \function igraph_heap_destroy
 
98
 * \brief Destroys an initialized heap object.
 
99
 * 
 
100
 * \param h The heap object.
 
101
 * 
 
102
 * Time complexity: O(1).
 
103
 */
 
104
 
 
105
void FUNCTION(igraph_heap,destroy)(TYPE(igraph_heap)* h) {  
 
106
  if (h->destroy) {
 
107
    if (h->stor_begin != 0) {
 
108
      igraph_Free(h->stor_begin);
 
109
      h->stor_begin=0;
 
110
    }
 
111
  }
 
112
}
 
113
 
 
114
/**
 
115
 * \ingroup heap
 
116
 * \function igraph_heap_empty
 
117
 * \brief Decides whether a heap object is empty.
 
118
 * 
 
119
 * \param h The heap object.
 
120
 * \return \c TRUE if the heap is empty, \c FALSE otherwise.
 
121
 * 
 
122
 * TIme complexity: O(1).
 
123
 */
 
124
 
 
125
igraph_bool_t FUNCTION(igraph_heap,empty)(TYPE(igraph_heap)* h) {
 
126
  assert(h != NULL);
 
127
  assert(h->stor_begin != NULL);
 
128
  return h->stor_begin == h->end;
 
129
}
 
130
 
 
131
/**
 
132
 * \ingroup heap
 
133
 * \function igraph_heap_push
 
134
 * \brief Add an element.
 
135
 * 
 
136
 * Adds an element to the heap.
 
137
 * \param h The heap object.
 
138
 * \param elem The element to add.
 
139
 * \return Error code.
 
140
 * 
 
141
 * Time complexity: O(log n), n is the number of elements in the
 
142
 * heap if no reallocation is needed, O(n) otherwise. It is ensured
 
143
 * that n push operations are performed in O(n log n) time.
 
144
 */
 
145
 
 
146
int FUNCTION(igraph_heap,push)(TYPE(igraph_heap)* h, BASE elem) {
 
147
  assert(h != NULL);
 
148
  assert(h->stor_begin != NULL);
 
149
        
 
150
  /* full, allocate more storage */
 
151
  if (h->stor_end == h->end) {
 
152
    long int new_size = FUNCTION(igraph_heap,size)(h) * 2;
 
153
    if (new_size == 0) { new_size = 1; }
 
154
    IGRAPH_CHECK(FUNCTION(igraph_heap,reserve)(h, new_size));
 
155
  }
 
156
        
 
157
  *(h->end) = elem;
 
158
  h->end += 1;
 
159
 
 
160
  /* maintain heap */
 
161
  FUNCTION(igraph_heap,i_shift_up)(h->stor_begin, FUNCTION(igraph_heap,size)(h),
 
162
                                   FUNCTION(igraph_heap,size)(h)-1);
 
163
  
 
164
  return 0;
 
165
}
 
166
 
 
167
/**
 
168
 * \ingroup heap
 
169
 * \function igraph_heap_top
 
170
 * \brief Top element.
 
171
 * 
 
172
 * For maximum heaps this is the largest, for minimum heaps the
 
173
 * smallest element of the heap.
 
174
 * \param h The heap object.
 
175
 * \return The top element.
 
176
 *
 
177
 * Time complexity: O(1).
 
178
 */
 
179
 
 
180
BASE FUNCTION(igraph_heap,top)(TYPE(igraph_heap)* h) {
 
181
  assert(h != NULL);
 
182
  assert(h->stor_begin != NULL);
 
183
  assert(h->stor_begin != h->end);
 
184
  
 
185
  return h->stor_begin[0];
 
186
}
 
187
 
 
188
/**
 
189
 * \ingroup heap
 
190
 * \function igraph_heap_delete_top
 
191
 * \brief Return and removes the top element
 
192
 * 
 
193
 * Removes and returns the top element of the heap. For maximum heaps
 
194
 * this is the largest, for minimum heaps the smallest element.
 
195
 * \param h The heap object.
 
196
 * \return The top element.
 
197
 * 
 
198
 * Time complexity: O(log n), n is the number of elements in the
 
199
 * heap.
 
200
 */
 
201
 
 
202
BASE FUNCTION(igraph_heap,delete_top)(TYPE(igraph_heap)* h) {
 
203
  BASE tmp;
 
204
 
 
205
  assert(h != NULL);
 
206
  assert(h->stor_begin != NULL);
 
207
 
 
208
  tmp=h->stor_begin[0];
 
209
  FUNCTION(igraph_heap,i_switch)(h->stor_begin, 0, FUNCTION(igraph_heap,size)(h)-1);
 
210
  h->end -= 1;
 
211
  FUNCTION(igraph_heap,i_sink)(h->stor_begin, h->end-h->stor_begin, 0);
 
212
  
 
213
  return tmp;
 
214
}
 
215
 
 
216
/**
 
217
 * \ingroup heap
 
218
 * \function igraph_heap_size
 
219
 * \brief Number of elements
 
220
 * 
 
221
 * Gives the number of elements in a heap.
 
222
 * \param h The heap object.
 
223
 * \return The number of elements in the heap.
 
224
 * 
 
225
 * Time complexity: O(1).
 
226
 */
 
227
 
 
228
long int FUNCTION(igraph_heap,size)(TYPE(igraph_heap)* h) {
 
229
  assert(h != NULL);
 
230
  assert(h->stor_begin != NULL);
 
231
  return h->end - h->stor_begin;
 
232
}
 
233
 
 
234
/**
 
235
 * \ingroup heap
 
236
 * \function igraph_heap_reserve
 
237
 * \brief Allocate more memory
 
238
 *
 
239
 * Allocates memory for future use. The size of the heap is
 
240
 * unchanged. If the heap is larger than the \p size parameter then
 
241
 * nothing happens.
 
242
 * \param h The heap object.
 
243
 * \param size The number of elements to allocate memory for.
 
244
 * \return Error code.
 
245
 * 
 
246
 * Time complexity: O(\p size) if \p size is larger than the current
 
247
 * number of elements. O(1) otherwise.
 
248
 */
 
249
 
 
250
int FUNCTION(igraph_heap,reserve)(TYPE(igraph_heap)* h, long int size) {
 
251
  long int actual_size=FUNCTION(igraph_heap,size)(h);
 
252
  BASE *tmp;
 
253
  assert(h != NULL);
 
254
  assert(h->stor_begin != NULL);
 
255
  
 
256
  if (size <= actual_size) { return 0; }
 
257
  
 
258
  tmp=igraph_Realloc(h->stor_begin, size, BASE);
 
259
  if (tmp==0) {
 
260
    IGRAPH_ERROR("heap reserve failed", IGRAPH_ENOMEM);
 
261
  }
 
262
  h->stor_begin=tmp;
 
263
  h->stor_end=h->stor_begin + size;
 
264
  h->end=h->stor_begin+actual_size;
 
265
  
 
266
  return 0;
 
267
}
 
268
 
 
269
/**
 
270
 * \ingroup heap
 
271
 * \brief Build a heap, this should not be called directly.
 
272
 */
 
273
 
 
274
void FUNCTION(igraph_heap,i_build)(BASE* arr, 
 
275
                                   long int size, long int head) {
 
276
 
 
277
  if (RIGHTCHILD(head) < size) { 
 
278
    /* both subtrees */
 
279
    FUNCTION(igraph_heap,i_build)(arr, size, LEFTCHILD(head) );
 
280
    FUNCTION(igraph_heap,i_build)(arr, size, RIGHTCHILD(head));
 
281
    FUNCTION(igraph_heap,i_sink)(arr, size, head);
 
282
  } else if (LEFTCHILD(head) < size) {
 
283
    /* only left */
 
284
    FUNCTION(igraph_heap,i_build)(arr, size, LEFTCHILD(head));
 
285
    FUNCTION(igraph_heap,i_sink)(arr, size, head);
 
286
  } else {
 
287
    /* none */
 
288
  }
 
289
}
 
290
 
 
291
/**
 
292
 * \ingroup heap
 
293
 * \brief Shift an element upwards in a heap, this should not be
 
294
 * called directly.
 
295
 */
 
296
 
 
297
void FUNCTION(igraph_heap,i_shift_up)(BASE* arr, long int size, long int elem) {
 
298
  
 
299
  if (elem==0 || arr[elem] HEAPLESS arr[PARENT(elem)]) { 
 
300
    /* at the top */
 
301
  } else {
 
302
    FUNCTION(igraph_heap,i_switch)(arr, elem, PARENT(elem));
 
303
    FUNCTION(igraph_heap,i_shift_up)(arr, size, PARENT(elem));
 
304
  }
 
305
}
 
306
 
 
307
/**
 
308
 * \ingroup heap
 
309
 * \brief Moves an element down in a heap, this function should not be
 
310
 * called directly.
 
311
 */
 
312
 
 
313
void FUNCTION(igraph_heap,i_sink)(BASE* arr, long int size, long int head) {
 
314
 
 
315
  if (LEFTCHILD(head) >= size) { 
 
316
    /* no subtrees */
 
317
  } else if (RIGHTCHILD(head) == size ||
 
318
             arr[LEFTCHILD(head)] HEAPMOREEQ arr[RIGHTCHILD(head)]) {
 
319
    /* sink to the left if needed */
 
320
    if (arr[head] HEAPLESS arr[LEFTCHILD(head)]) {
 
321
      FUNCTION(igraph_heap,i_switch)(arr, head, LEFTCHILD(head));
 
322
      FUNCTION(igraph_heap,i_sink)(arr, size, LEFTCHILD(head));
 
323
    }
 
324
  } else {
 
325
    /* sink to the right */
 
326
    if (arr[head] HEAPLESS arr[RIGHTCHILD(head)]) {
 
327
      FUNCTION(igraph_heap,i_switch)(arr, head, RIGHTCHILD(head));
 
328
      FUNCTION(igraph_heap,i_sink)(arr, size, RIGHTCHILD(head));
 
329
    }
 
330
  }
 
331
}
 
332
 
 
333
/**
 
334
 * \ingroup heap
 
335
 * \brief Switches two elements in a heap, this function should not be
 
336
 * called directly.
 
337
 */
 
338
 
 
339
void FUNCTION(igraph_heap,i_switch)(BASE* arr, long int e1, long int e2) {
 
340
  if (e1!=e2) {
 
341
    BASE tmp=arr[e1];
 
342
    arr[e1]=arr[e2];
 
343
    arr[e2]=tmp;
 
344
  }
 
345
}