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:   PetscFunctionBegin;
 26:   PetscCall(PetscFree(stack->stack));
 27:   PetscCall(PetscFree(stack));
 28:   PetscFunctionReturn(PETSC_SUCCESS);
 29: }

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

 34:   Not Collective

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

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

 42:   Level: developer

 44: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
 45: @*/
 46: PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool *empty)
 47: {
 48:   PetscFunctionBegin;
 51:   *empty = stack->top == -1 ? PETSC_TRUE : PETSC_FALSE;
 52:   PetscFunctionReturn(PETSC_SUCCESS);
 53: }

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

 58:   Not Collective

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

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

 66:   Level: developer

 68: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`
 69: @*/
 70: PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
 71: {
 72:   PetscFunctionBegin;
 75:   *top = stack->stack[stack->top];
 76:   PetscFunctionReturn(PETSC_SUCCESS);
 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:   PetscFunctionBegin;
 96:   if (++stack->top >= stack->max) {
 97:     stack->max *= 2;
 98:     PetscCall(PetscRealloc(stack->max * sizeof(*stack->stack), &stack->stack));
 99:   }
100:   stack->stack[stack->top] = item;
101:   PetscFunctionReturn(PETSC_SUCCESS);
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: {
121:   PetscFunctionBegin;
123:   PetscCheck(stack->top != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
124:   if (item) {
126:     PetscCall(PetscIntStackTop(stack, item));
127:   }
128:   --stack->top;
129:   PetscFunctionReturn(PETSC_SUCCESS);
130: }

132: /*@C
133:   PetscIntStackCreate - This function creates a stack.

135:   Not Collective

137:   Output Parameter:
138: . stack - The stack

140:   Level: developer

142: .seealso: `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
143: @*/
144: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
145: {
146:   PetscFunctionBegin;
148:   PetscCall(PetscNew(stack));

150:   (*stack)->top = -1;
151:   (*stack)->max = 128;

153:   PetscCall(PetscCalloc1((*stack)->max, &(*stack)->stack));
154:   PetscFunctionReturn(PETSC_SUCCESS);
155: }