]> granicus.if.org Git - graphviz/commitdiff
IntStack: track size and capacity as 'size_t' values
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 17 Sep 2021 00:46:31 +0000 (17:46 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 20 Sep 2021 00:24:30 +0000 (17:24 -0700)
Squashes two -Wsign-compare warnings and generally makes this code more
portable.

lib/sparse/BinaryHeap.c
lib/sparse/IntStack.c
lib/sparse/IntStack.h

index eb04498112144d9f33e3bbf40942261c5aef57fb..16c1a9d7c085ae9ef45c7f2e1c553297d7df98f3 100644 (file)
@@ -244,11 +244,10 @@ void BinaryHeap_sanity_check(BinaryHeap h){
     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 */
@@ -265,7 +264,7 @@ void BinaryHeap_sanity_check(BinaryHeap h){
   }
 
   /* 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);
@@ -282,7 +281,7 @@ void BinaryHeap_print(BinaryHeap h, void (*pnt)(void*)){
     }
   }
   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]]);
   }
index ecf7f44dcc2ba260cd362862ea7da273bbdefba7..54d119f39c3c07d33f0c91c9e54f4eceb69fc9ef 100644 (file)
@@ -8,16 +8,18 @@
  * 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;
 }
@@ -30,7 +32,7 @@ void IntStack_delete(IntStack 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;
@@ -39,10 +41,10 @@ static IntStack IntStack_realloc(IntStack s){
   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;
@@ -50,14 +52,13 @@ int IntStack_push(IntStack s, int i){
 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");
 }
index a4f8e7d810cb55e11e28ae3fd32d43a85a1bb2fe..baba5f47273269019ce5d067c832175983195bbc 100644 (file)
 #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;
 };
 
@@ -26,8 +28,8 @@ void IntStack_delete(IntStack s);
 
 #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. */