Actual source code: stack.c
 
   petsc-3.6.2 2015-10-02
   
  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> /*I    "petscsys.h"   I*/
 12: /*@C
 13:   PetscIntStackDestroy - This function destroys a stack.
 15:   Not Collective
 17:   Input Parameter:
 18: . stack - The stack
 20:   Level: developer
 22: .keywords: log, stack, destroy
 23: .seealso: PetscIntStackCreate(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
 24: @*/
 25: PetscErrorCode PetscIntStackDestroy(PetscIntStack stack)
 26: {
 30:   PetscFree(stack->stack);
 31:   PetscFree(stack);
 32:   return(0);
 33: }
 37: /*@C
 38:   PetscIntStackEmpty - This function determines whether any items have been pushed.
 40:   Not Collective
 42:   Input Parameter:
 43: . stack - The stack
 45:   Output Parameter:
 46: . empty - PETSC_TRUE if the stack is empty
 48:   Level: developer
 50: .keywords: log, stack, empty
 51: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
 52: @*/
 53: PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool  *empty)
 54: {
 57:   if (stack->top == -1) *empty = PETSC_TRUE;
 58:   else *empty = PETSC_FALSE;
 59:   return(0);
 60: }
 64: /*@C
 65:   PetscIntStackTop - This function returns the top of the stack.
 67:   Not Collective
 69:   Input Parameter:
 70: . stack - The stack
 72:   Output Parameter:
 73: . top - The integer on top of the stack
 75:   Level: developer
 77: .keywords: log, stack, top
 78: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop()
 79: @*/
 80: PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
 81: {
 84:   *top = stack->stack[stack->top];
 85:   return(0);
 86: }
 90: /*@C
 91:   PetscIntStackPush - This function pushes an integer on the stack.
 93:   Not Collective
 95:   Input Parameters:
 96: + stack - The stack
 97: - item  - The integer to push
 99:   Level: developer
101: .keywords: log, stack, push
102: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPop(), PetscIntStackTop()
103: @*/
104: PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
105: {
106:   int            *array;
110:   stack->top++;
111:   if (stack->top >= stack->max) {
112:     PetscMalloc1(stack->max*2, &array);
113:     PetscMemcpy(array, stack->stack, stack->max * sizeof(int));
114:     PetscFree(stack->stack);
116:     stack->stack = array;
117:     stack->max  *= 2;
118:   }
119:   stack->stack[stack->top] = item;
120:   return(0);
121: }
125: /*@C
126:   PetscIntStackPop - This function pops an integer from the stack.
128:   Not Collective
130:   Input Parameter:
131: . stack - The stack
133:   Output Parameter:
134: . item  - The integer popped
136:   Level: developer
138: .keywords: log, stack, pop
139: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackTop()
140: @*/
141: PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
142: {
145:   if (stack->top == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
146:   *item = stack->stack[stack->top--];
147:   return(0);
148: }
152: /*@C
153:   PetscIntStackCreate - This function creates a stack.
155:   Not Collective
157:   Output Parameter:
158: . stack - The stack
160:   Level: developer
162: .keywords: log, stack, pop
163: .seealso: PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
164: @*/
165: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
166: {
167:   PetscIntStack  s;
172:   PetscNew(&s);
174:   s->top = -1;
175:   s->max = 128;
177:   PetscCalloc1(s->max, &s->stack);
178:   *stack = s;
179:   return(0);
180: }