File: IntStack.c

package info (click to toggle)
graphviz 2.42.2-7%2Bdeb12u1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 95,764 kB
  • sloc: ansic: 1,051,543; cpp: 9,107; tcl: 4,897; makefile: 4,862; sh: 4,506; yacc: 4,190; xml: 2,970; cs: 1,921; objc: 1,157; lex: 625; java: 560; perl: 445; python: 255; awk: 241; javascript: 146; ruby: 64; php: 59; sed: 1
file content (95 lines) | stat: -rw-r--r-- 2,308 bytes parent folder | download | duplicates (6)
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
/* $Id$Revision: */
/* vim:set shiftwidth=4 ts=8: */

/*************************************************************************
 * 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: See CVS logs. Details at http://www.graphviz.org/
 *************************************************************************/

#include "general.h"
#include "IntStack.h"

IntStack IntStack_new(void){
  IntStack s;
  int max_len = 1<<5;

  s = MALLOC(sizeof(struct IntStack_struct));
  s->max_len = max_len;
  s->last = -1;
  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){
  int max_len = s->max_len;

  max_len = max_len + MAX(10,0.2*max_len);
  s->max_len = max_len;
  s->stack = REALLOC(s->stack, sizeof(int)*max_len);
  if (!s->stack) return NULL;
  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;
  }
  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){
    *flag = -1;
    return -1;
  }
  return s->stack[(s->last)--];
}
void IntStack_print(IntStack s){
  /* remove the last item. If none exist, return -1 */
  int i;
  for (i = 0; i <= s->last; i++) fprintf(stderr,"%d,",s->stack[i]);
  fprintf(stderr,"\n");
}

/*
main(){

  IntStack s;
  int i, len = 1, pos, flag;

  for (;;){
    s = IntStack_new();
    fprintf(stderr,"=============== stack with %d elements ============\n",len);
    for (i = 0; i < len; i++){
      pos = IntStack_push(s, i);
      if (pos < 0){
	fprintf(stderr," fail to push element %d, quit\n", i);
	exit(1);
      }
    }    
    for (i = 0; i < len+1; i++){
      IntStack_pop(s, &flag);
      if (flag) {
	fprintf(stderr, "no more element\n");
      }
    }
    IntStack_delete(s);
    len *= 2;
  }
}
*/