"lib/sparse/BinaryHeap.h",
"lib/sparse/DotIO.c",
"lib/sparse/DotIO.h",
- "lib/sparse/IntStack.c",
- "lib/sparse/IntStack.h",
"lib/sparse/LinkedList.c",
"lib/sparse/LinkedList.h",
"lib/sparse/QuadTree.c",
for (size_t i = 0; i < max_len; i++) h->id_to_pos[i] = SIZE_MAX;
h->pos_to_id = CALLOC(max_len, sizeof(h->pos_to_id[0]));
- h->id_stack = IntStack_new();
+ h->id_stack = (int_stack_t){0};
h->cmp = cmp;
return h;
}
if (!h) return;
free(h->id_to_pos);
free(h->pos_to_id);
- IntStack_delete(h->id_stack);
+ int_stack_free(&h->id_stack);
if (del) for (size_t i = 0; i < h->len; i++) del((h->heap)[i]);
free(h->heap);
free(h);
int BinaryHeap_insert(BinaryHeap h, void *item){
size_t len = h->len;
assert(len <= (size_t)INT_MAX);
- int id = (int)len, flag;
+ int id = (int)len;
/* insert an item, and return its ID. This ID can be used later to extract the item */
if (len > h->max_len - 1) {
if (BinaryHeap_realloc(h) == NULL) return BinaryHeap_error_malloc;
}
- /* check if we have IDs in the stack to reuse. If no, then assign the last pos as the ID */
- id = IntStack_pop(h->id_stack, &flag);
- if (flag) id = (int)len;
+ // check if we have IDs in the stack to reuse
+ if (!int_stack_is_empty(&h->id_stack)) {
+ id = int_stack_pop(&h->id_stack);
+ }
h->heap[len] = item;
h->id_to_pos[id] = len;
item = (h->heap)[pos];
- IntStack_push(h->id_stack, id);
+ int_stack_push(&h->id_stack, id);
if (pos < h->len - 1){/* move the last item to occupy the position of extracted item */
swap(h, pos, h->len - 1);
assert((h->cmp)(heap[i], heap[parentPos]) >= 0);
}
- mask = CALLOC(h->len + IntStack_get_length(h->id_stack), sizeof(mask[0]));
+ mask = CALLOC(h->len + int_stack_size(&h->id_stack), sizeof(mask[0]));
/* check that spare keys has negative id_to_pos mapping */
- for (size_t i = 0; i <= h->id_stack->last; i++) {
- int key_spare = h->id_stack->stack[i];
+ for (size_t i = 0; i < int_stack_size(&h->id_stack); i++) {
+ int key_spare = int_stack_get(&h->id_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 + IntStack_get_length(h->id_stack); i++)
+ for (size_t i = 0; i < h->len + int_stack_size(&h->id_stack); i++)
assert(mask[i] != 0);
free(mask);
}
}
fprintf(stderr, "\nSpare keys =");
- for (size_t i = 0; i <= h->id_stack->last; i++) {
- fprintf(stderr, "%d(%" PRISIZE_T ") ", h->id_stack->stack[i],
- h->id_to_pos[h->id_stack->stack[i]]);
+ for (size_t i = 0; i < int_stack_size(&h->id_stack); i++) {
+ fprintf(stderr, "%d(%" PRISIZE_T ") ", int_stack_get(&h->id_stack, i),
+ h->id_to_pos[int_stack_get(&h->id_stack, i)]);
}
fprintf(stderr, "\n");
}
#pragma once
+#include <cgraph/list.h>
#include <sparse/general.h>
-#include <sparse/IntStack.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
+DEFINE_LIST(int_stack, int)
+
/* binary heap code.
Caution: items inserted should be kept untouched, e.g., the value of the item should be kepted unchanged while the heap is still in use!
To change the valud of am item, use BinaryHeap_reset
pos_to_id[id_to_pos[i]] = i, for i not in the id_stack & i < length(id_stack)+len
id_to_pos[pos_to_id[i]] = i, 0 <= i < len
*/
- IntStack id_stack;/* a stack that store IDs that is no longer used, to
+ int_stack_t id_stack;/* a stack that store IDs that is no longer used, to
be assigned to newly inserted elements.
For sanity check, the union of items in
the id_stack, and that is pos_to_id[1:len],
colorutil.h
DotIO.h
general.h
- IntStack.h
LinkedList.h
mq.h
QuadTree.h
colorutil.c
DotIO.c
general.c
- IntStack.c
LinkedList.c
mq.c
QuadTree.c
+++ /dev/null
-/*************************************************************************
- * Copyright (c) 2011 AT&T Intellectual Property
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * 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;
- size_t max_len = 1<<5;
-
- s = MALLOC(sizeof(struct IntStack_struct));
- s->max_len = max_len;
- s->last = SIZE_MAX;
- s->stack = MALLOC(sizeof(int)*max_len);
- return s;
-}
-
-void IntStack_delete(IntStack s){
- if (s){
- free(s->stack);
- free(s);
- }
-}
-
-static IntStack IntStack_realloc(IntStack s){
- size_t max_len = s->max_len;
-
- max_len += MAX(10, max_len / 5);
- s->max_len = max_len;
- s->stack = REALLOC(s->stack, sizeof(int)*max_len);
- if (!s->stack) return NULL;
- return s;
-}
-
-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 == SIZE_MAX) {
- *flag = -1;
- return -1;
- }
- return s->stack[s->last--];
-}
-void IntStack_print(IntStack s){
- for (size_t i = 0; i <= s->last; i++) fprintf(stderr, "%d,", s->stack[i]);
- fprintf(stderr,"\n");
-}
+++ /dev/null
-/*************************************************************************
- * Copyright (c) 2011 AT&T Intellectual Property
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors: Details at https://graphviz.org
- *************************************************************************/
-
-#pragma once
-
-#include <stddef.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* last in first out integer stack */
-struct IntStack_struct{
- size_t last; // position of the last element, If empty, last = SIZE_MAX
- size_t max_len;
- int *stack;
-};
-
-typedef struct IntStack_struct* IntStack;
-
-IntStack IntStack_new(void);
-
-void IntStack_delete(IntStack s);
-
-#define IntStack_get_length(s) (1+(s)->last)
-
-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. */
-
-void IntStack_print(IntStack s);
-
-#ifdef __cplusplus
-}
-#endif
-I$(top_srcdir)/lib/cgraph \
-I$(top_srcdir)/lib/cdt
-noinst_HEADERS = SparseMatrix.h general.h BinaryHeap.h IntStack.h DotIO.h \
+noinst_HEADERS = SparseMatrix.h general.h BinaryHeap.h DotIO.h \
LinkedList.h colorutil.h color_palette.h mq.h clustering.h QuadTree.h
noinst_LTLIBRARIES = libsparse_C.la
-libsparse_C_la_SOURCES = SparseMatrix.c general.c BinaryHeap.c IntStack.c DotIO.c \
+libsparse_C_la_SOURCES = SparseMatrix.c general.c BinaryHeap.c DotIO.c \
LinkedList.c colorutil.c color_palette.c mq.c clustering.c QuadTree.c
EXTRA_DIST = gvsparse.vcxproj*
<ClCompile Include="color_palette.c" />
<ClCompile Include="DotIO.c" />
<ClCompile Include="general.c" />
- <ClCompile Include="IntStack.c" />
<ClCompile Include="LinkedList.c" />
<ClCompile Include="mq.c" />
<ClCompile Include="QuadTree.c" />
<ClCompile Include="general.c">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="IntStack.c">
- <Filter>Source Files</Filter>
- </ClCompile>
<ClCompile Include="LinkedList.c">
<Filter>Source Files</Filter>
</ClCompile>