Exemplo n.º 1
0
/*
  Push value onto the stack. Return 0 if stack push succeeds.
*/
bool carp_stack_push (carp_stack *s, carp_value i) {
    assert(s != NULL);

    if (carp_stack_full(s)) {
        // give stack 2n + 1 its existing space, hopefully more efficiently allocating
        carp_value new_height = 2*s->max_height + 1;
        carp_value *new_contents = realloc(s->contents, new_height * sizeof *new_contents);
        if (new_contents == NULL)
            return 1;

        // if all is well, update pointer and capacity
        s->max_height = new_height;
        s->contents = new_contents;
    }

    assert(!carp_stack_full(s));
    // push the value and increase the height
    s->contents[(*s->height)] = i;
    (*s->height)++;

    return 0;
}
Exemplo n.º 2
0
/*
  Push value onto the stack. Return 0 if stack push succeeds.
*/
carp_bool carp_stack_push (carp_stack *s, carp_value i) {
  assert(s != NULL);

  if (carp_stack_full(s)) {
    // give stack 2n + 1 its existing space, hopefully more efficiently allocating
    carp_value *contents = realloc(s->contents, (2*(*s->height) + 1) * sizeof *contents);
    if (contents == NULL)
      return 1;

    // and of course update max_height
    s->max_height *= 2;
    s->max_height++;

    // and update the pointer
    s->contents = contents;
  }

  // in either case, push a value and increase the height
  s->contents[(*s->height)] = i;
  (*s->height)++;

  return 0;
}