Actual source code: stack.c

petsc-3.3-p7 2013-05-11
  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) {
 58:     *empty = PETSC_TRUE;
 59:   } else {
 60:     *empty = PETSC_FALSE;
 61:   }
 62:   return(0);
 63: }

 67: /*@C
 68:   PetscIntStackTop - This function returns the top of the stack.

 70:   Not Collective

 72:   Input Parameter:
 73: . stack - The stack

 75:   Output Parameter:
 76: . top - The integer on top of the stack

 78:   Level: developer

 80: .keywords: log, stack, top
 81: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop()
 82: @*/
 83: PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
 84: {
 87:   *top = stack->stack[stack->top];
 88:   return(0);
 89: }

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

 96:   Not Collective

 98:   Input Parameters:
 99: + stack - The stack
100: - item  - The integer to push

102:   Level: developer

104: .keywords: log, stack, push
105: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPop(), PetscIntStackTop()
106: @*/
107: PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
108: {
109:   int            *array;

113:   stack->top++;
114:   if (stack->top >= stack->max) {
115:     PetscMalloc(stack->max*2 * sizeof(int), &array);
116:     PetscMemcpy(array, stack->stack, stack->max * sizeof(int));
117:     PetscFree(stack->stack);
118:     stack->stack = array;
119:     stack->max  *= 2;
120:   }
121:   stack->stack[stack->top] = item;
122:   return(0);
123: }

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

130:   Not Collective

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

135:   Output Parameter:
136: . item  - The integer popped

138:   Level: developer

140: .keywords: log, stack, pop
141: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackTop()
142: @*/
143: PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
144: {
147:   if (stack->top == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
148:   *item = stack->stack[stack->top--];
149:   return(0);
150: }

154: /*@C
155:   PetscIntStackCreate - This function creates a stack.

157:   Not Collective

159:   Output Parameter:
160: . stack - The stack

162:   Level: developer

164: .keywords: log, stack, pop
165: .seealso: PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
166: @*/
167: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
168: {
169:   PetscIntStack       s;

174:   PetscNew(struct _n_PetscIntStack, &s);
175:   s->top = -1;
176:   s->max = 128;
177:   PetscMalloc(s->max * sizeof(int), &s->stack);
178:   PetscMemzero(s->stack, s->max * sizeof(int));
179:   *stack = s;
180:   return(0);
181: }