assert((h->cmp)(heap[i], heap[parentPos]) >= 0);
}
- mask = CALLOC(h->len + (size_t)IntStack_get_length(h->id_stack),
- sizeof(mask[0]));
+ mask = CALLOC(h->len + IntStack_get_length(h->id_stack), sizeof(mask[0]));
/* check that spare keys has negative id_to_pos mapping */
- for (int i = 0; i <= h->id_stack->last; i++){
+ for (size_t i = 0; i <= h->id_stack->last; i++) {
int key_spare = h->id_stack->stack[i];
assert(h->id_to_pos[key_spare] == SIZE_MAX);
mask[key_spare] = 1;/* mask spare ID */
}
/* all IDs, spare or in use, are accounted for and give a contiguous set */
- for (size_t i = 0; i < h->len + (size_t)IntStack_get_length(h->id_stack); i++)
+ for (size_t i = 0; i < h->len + IntStack_get_length(h->id_stack); i++)
assert(mask[i] != 0);
FREE(mask);
}
}
fprintf(stderr, "\nSpare keys =");
- for (int i = 0; i <= h->id_stack->last; i++){
+ for (size_t i = 0; i <= h->id_stack->last; i++) {
fprintf(stderr, "%d(%zu) ", h->id_stack->stack[i],
h->id_to_pos[h->id_stack->stack[i]]);
}
* Contributors: Details at https://graphviz.org
*************************************************************************/
+#include <limits.h>
#include <sparse/general.h>
#include <sparse/IntStack.h>
+#include <stdlib.h>
IntStack IntStack_new(void){
IntStack s;
- int max_len = 1<<5;
+ size_t max_len = 1<<5;
s = MALLOC(sizeof(struct IntStack_struct));
s->max_len = max_len;
- s->last = -1;
+ s->last = SIZE_MAX;
s->stack = MALLOC(sizeof(int)*max_len);
return s;
}
}
static IntStack IntStack_realloc(IntStack s){
- int max_len = s->max_len;
+ size_t max_len = s->max_len;
max_len += MAX(10, max_len / 5);
s->max_len = max_len;
return s;
}
-int IntStack_push(IntStack s, int i){
- /* add an item and return the pos. Return negative value of malloc failed */
- if (s->last >= s->max_len - 1){
- if (!(IntStack_realloc(s))) return -1;
+size_t IntStack_push(IntStack s, int i){
+ // add an item and return the pos. Return SIZE_MAX if malloc failed
+ if (s->last != SIZE_MAX && s->last >= s->max_len - 1) {
+ if (!IntStack_realloc(s)) return SIZE_MAX;
}
s->stack[++(s->last)] = i;
return s->last;
int IntStack_pop(IntStack s, int *flag){
/* remove the last item. If none exist, return -1 */
*flag = 0;
- if (s->last < 0){
+ if (s->last == SIZE_MAX) {
*flag = -1;
return -1;
}
return s->stack[(s->last)--];
}
void IntStack_print(IntStack s){
- int i;
- for (i = 0; i <= s->last; i++) fprintf(stderr,"%d,",s->stack[i]);
+ for (size_t i = 0; i <= s->last; i++) fprintf(stderr, "%d,", s->stack[i]);
fprintf(stderr,"\n");
}
#ifndef IntStack_H
#define IntStack_H
+#include <stddef.h>
+
/* last in first out integer stack */
struct IntStack_struct{
- int last;/* position of the last element, If empty, last = -1 */
- int max_len;
+ size_t last; // position of the last element, If empty, last = SIZE_MAX
+ size_t max_len;
int *stack;
};
#define IntStack_get_length(s) (1+(s)->last)
-int IntStack_push(IntStack s, int i);/* add an item and return the pos (>=0).
- Return negative value of malloc failed */
+size_t IntStack_push(IntStack s, int i); // add an item and return the pos
+ // Return SIZE_MAX if malloc failed
int IntStack_pop(IntStack s, int *flag);/* remove the last item. If none exist, flag = -1, and return -1. */