PLplot  5.15.0
istack.c
Go to the documentation of this file.
1 //--------------------------------------------------------------------------
2 //
3 // File: istack.c
4 //
5 // Created: 06/06/2001
6 //
7 // Author: Pavel Sakov
8 // CSIRO Marine Research
9 //
10 // Purpose: Handling stack of integers
11 //
12 // Description: None
13 //
14 // Revisions: None
15 //
16 //--------------------------------------------------------------------------
17 
18 #define STACK_NSTART 50
19 #define STACK_NINC 50
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include "istack.h"
24 
25 static void istack_init( istack* s )
26 {
27  s->n = 0;
29  s->v = malloc( STACK_NSTART * sizeof ( int ) );
30 }
31 
33 {
34  istack* s = malloc( sizeof ( istack ) );
35 
36  istack_init( s );
37  return s;
38 }
39 
40 void istack_reset( istack* s )
41 {
42  s->n = 0;
43 }
44 
45 int istack_contains( istack* s, int v )
46 {
47  int i;
48 
49  for ( i = 0; i < s->n; ++i )
50  if ( s->v[i] == v )
51  return 1;
52  return 0;
53 }
54 
55 void istack_push( istack* s, int v )
56 {
57  if ( s->n == s->nallocated )
58  {
59  s->v = realloc( s->v, (size_t) ( s->nallocated + STACK_NINC ) * sizeof ( int ) );
60  s->nallocated += STACK_NINC;
61  }
62 
63  s->v[s->n] = v;
64  s->n++;
65 }
66 
67 int istack_pop( istack* s )
68 {
69  s->n--;
70  return s->v[s->n];
71 }
72 
74 {
75  if ( s != NULL )
76  {
77  free( s->v );
78  free( s );
79  }
80 }
Definition: istack.h:21
void istack_push(istack *s, int v)
Definition: istack.c:55
int istack_contains(istack *s, int v)
Definition: istack.c:45
int nallocated
Definition: istack.h:24
void istack_destroy(istack *s)
Definition: istack.c:73
void istack_reset(istack *s)
Definition: istack.c:40
#define STACK_NINC
Definition: istack.c:19
int * v
Definition: istack.h:25
#define STACK_NSTART
Definition: istack.c:18
istack * istack_create()
Definition: istack.c:32
static void istack_init(istack *s)
Definition: istack.c:25
int n
Definition: istack.h:23
int istack_pop(istack *s)
Definition: istack.c:67