Actual source code: stack.c
petsc-3.13.6 2020-09-29
Report Typos and Errors

      2: /*
      3:      This defines part of the private API for logging performance information. It is intended to be used only by the
      4:    PETSc PetscLog...() interface and not elsewhere, nor by users. Hence the prototypes for these functions are NOT
      5:    in the public PETSc include files.

      7: */
      8:  #include <petsc/private/logimpl.h>

     10: /*@C
     11:   PetscIntStackDestroy - This function destroys a stack.

     13:   Not Collective

     15:   Input Parameter:
     16: . stack - The stack

     18:   Level: developer

     20: .seealso: PetscIntStackCreate(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
     21: @*/
     22: PetscErrorCode PetscIntStackDestroy(PetscIntStack stack)
     23: {

     27:   PetscFree(stack->stack);
     28:   PetscFree(stack);
     29:   return(0);
     30: }

     32: /*@C
     33:   PetscIntStackEmpty - This function determines whether any items have been pushed.

     35:   Not Collective

     37:   Input Parameter:
     38: . stack - The stack

     40:   Output Parameter:
     41: . empty - PETSC_TRUE if the stack is empty

     43:   Level: developer

     45: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
     46: @*/
     47: PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool  *empty)
     48: {
     51:   if (stack->top == -1) *empty = PETSC_TRUE;
     52:   else *empty = PETSC_FALSE;
     53:   return(0);
     54: }

     56: /*@C
     57:   PetscIntStackTop - This function returns the top of the stack.

     59:   Not Collective

     61:   Input Parameter:
     62: . stack - The stack

     64:   Output Parameter:
     65: . top - The integer on top of the stack

     67:   Level: developer

     69: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop()
     70: @*/
     71: PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
     72: {
     75:   *top = stack->stack[stack->top];
     76:   return(0);
     77: }

     79: /*@C
     80:   PetscIntStackPush - This function pushes an integer on the stack.

     82:   Not Collective

     84:   Input Parameters:
     85: + stack - The stack
     86: - item  - The integer to push

     88:   Level: developer

     90: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPop(), PetscIntStackTop()
     91: @*/
     92: PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
     93: {
     94:   int            *array;

     98:   stack->top++;
     99:   if (stack->top >= stack->max) {
    100:     PetscMalloc1(stack->max*2, &array);
    101:     PetscArraycpy(array, stack->stack, stack->max);
    102:     PetscFree(stack->stack);

    104:     stack->stack = array;
    105:     stack->max  *= 2;
    106:   }
    107:   stack->stack[stack->top] = item;
    108:   return(0);
    109: }

    111: /*@C
    112:   PetscIntStackPop - This function pops an integer from the stack.

    114:   Not Collective

    116:   Input Parameter:
    117: . stack - The stack

    119:   Output Parameter:
    120: . item  - The integer popped

    122:   Level: developer

    124: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackTop()
    125: @*/
    126: PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
    127: {
    130:   if (stack->top == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
    131:   *item = stack->stack[stack->top--];
    132:   return(0);
    133: }

    135: /*@C
    136:   PetscIntStackCreate - This function creates a stack.

    138:   Not Collective

    140:   Output Parameter:
    141: . stack - The stack

    143:   Level: developer

    145: .seealso: PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
    146: @*/
    147: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
    148: {
    149:   PetscIntStack  s;

    154:   PetscNew(&s);

    156:   s->top = -1;
    157:   s->max = 128;

    159:   PetscCalloc1(s->max, &s->stack);
    160:   *stack = s;
    161:   return(0);
    162: }