Actual source code: stack.c


  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: {
 24:   PetscFree(stack->stack);
 25:   PetscFree(stack);
 26:   return 0;
 27: }

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

 32:   Not Collective

 34:   Input Parameter:
 35: . stack - The stack

 37:   Output Parameter:
 38: . empty - PETSC_TRUE if the stack is empty

 40:   Level: developer

 42: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
 43: @*/
 44: PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool  *empty)
 45: {
 47:   if (stack->top == -1) *empty = PETSC_TRUE;
 48:   else *empty = PETSC_FALSE;
 49:   return 0;
 50: }

 52: /*@C
 53:   PetscIntStackTop - This function returns the top of the stack.

 55:   Not Collective

 57:   Input Parameter:
 58: . stack - The stack

 60:   Output Parameter:
 61: . top - The integer on top of the stack

 63:   Level: developer

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

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

 77:   Not Collective

 79:   Input Parameters:
 80: + stack - The stack
 81: - item  - The integer to push

 83:   Level: developer

 85: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPop(), PetscIntStackTop()
 86: @*/
 87: PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
 88: {
 89:   int            *array;

 91:   stack->top++;
 92:   if (stack->top >= stack->max) {
 93:     PetscMalloc1(stack->max*2, &array);
 94:     PetscArraycpy(array, stack->stack, stack->max);
 95:     PetscFree(stack->stack);

 97:     stack->stack = array;
 98:     stack->max  *= 2;
 99:   }
100:   stack->stack[stack->top] = item;
101:   return 0;
102: }

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

107:   Not Collective

109:   Input Parameter:
110: . stack - The stack

112:   Output Parameter:
113: . item  - The integer popped

115:   Level: developer

117: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackTop()
118: @*/
119: PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
120: {
123:   *item = stack->stack[stack->top--];
124:   return 0;
125: }

127: /*@C
128:   PetscIntStackCreate - This function creates a stack.

130:   Not Collective

132:   Output Parameter:
133: . stack - The stack

135:   Level: developer

137: .seealso: PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
138: @*/
139: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
140: {
141:   PetscIntStack  s;

144:   PetscNew(&s);

146:   s->top = -1;
147:   s->max = 128;

149:   PetscCalloc1(s->max, &s->stack);
150:   *stack = s;
151:   return 0;
152: }