Actual source code: stack.c

  1: #include <petsc/private/logimpl.h>

  3: /*@C
  4:   PetscIntStackDestroy - This function destroys a stack.

  6:   Not Collective

  8:   Input Parameter:
  9: . stack - The stack

 11:   Level: developer

 13: .seealso: `PetscIntStackCreate()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
 14: @*/
 15: PetscErrorCode PetscIntStackDestroy(PetscIntStack stack)
 16: {
 17:   PetscFunctionBegin;
 18:   PetscAssertPointer(stack, 1);
 19:   PetscCall(PetscFree(stack->stack));
 20:   PetscCall(PetscFree(stack));
 21:   PetscFunctionReturn(PETSC_SUCCESS);
 22: }

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

 27:   Not Collective

 29:   Input Parameter:
 30: . stack - The stack

 32:   Output Parameter:
 33: . empty - `PETSC_TRUE` if the stack is empty

 35:   Level: developer

 37: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
 38: @*/
 39: PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool *empty)
 40: {
 41:   PetscFunctionBegin;
 42:   PetscAssertPointer(stack, 1);
 43:   PetscAssertPointer(empty, 2);
 44:   *empty = stack->top == -1 ? PETSC_TRUE : PETSC_FALSE;
 45:   PetscFunctionReturn(PETSC_SUCCESS);
 46: }

 48: /*@C
 49:   PetscIntStackTop - This function returns the top of the stack.

 51:   Not Collective

 53:   Input Parameter:
 54: . stack - The stack

 56:   Output Parameter:
 57: . top - The integer on top of the stack

 59:   Level: developer

 61: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`
 62: @*/
 63: PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
 64: {
 65:   PetscFunctionBegin;
 66:   PetscAssertPointer(stack, 1);
 67:   PetscAssertPointer(top, 2);
 68:   *top = stack->stack[stack->top];
 69:   PetscFunctionReturn(PETSC_SUCCESS);
 70: }

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

 75:   Not Collective

 77:   Input Parameters:
 78: + stack - The stack
 79: - item  - The integer to push

 81:   Level: developer

 83: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPop()`, `PetscIntStackTop()`
 84: @*/
 85: PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
 86: {
 87:   PetscFunctionBegin;
 88:   PetscAssertPointer(stack, 1);
 89:   if (++stack->top >= stack->max) {
 90:     stack->max *= 2;
 91:     PetscCall(PetscRealloc(stack->max * sizeof(*stack->stack), &stack->stack));
 92:   }
 93:   stack->stack[stack->top] = item;
 94:   PetscFunctionReturn(PETSC_SUCCESS);
 95: }

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

100:   Not Collective

102:   Input Parameter:
103: . stack - The stack

105:   Output Parameter:
106: . item - The integer popped

108:   Level: developer

110: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackTop()`
111: @*/
112: PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
113: {
114:   PetscFunctionBegin;
115:   PetscAssertPointer(stack, 1);
116:   PetscCheck(stack->top != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
117:   if (item) {
118:     PetscAssertPointer(item, 2);
119:     PetscCall(PetscIntStackTop(stack, item));
120:   }
121:   --stack->top;
122:   PetscFunctionReturn(PETSC_SUCCESS);
123: }

125: /*@C
126:   PetscIntStackCreate - This function creates a stack.

128:   Not Collective

130:   Output Parameter:
131: . stack - The stack

133:   Level: developer

135: .seealso: `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
136: @*/
137: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
138: {
139:   PetscFunctionBegin;
140:   PetscAssertPointer(stack, 1);
141:   PetscCall(PetscNew(stack));

143:   (*stack)->top = -1;
144:   (*stack)->max = 128;

146:   PetscCall(PetscCalloc1((*stack)->max, &(*stack)->stack));
147:   PetscFunctionReturn(PETSC_SUCCESS);
148: }