1 #include <petsc/private/logimpl.h> /*I "petscsys.h" I*/
2
3 /*@C
4 PetscIntStackDestroy - This function destroys a stack.
5
6 Not Collective, No Fortran Support
7
8 Input Parameter:
9 . stack - The stack
10
11 Level: developer
12
13 .seealso: `PetscIntStackCreate()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
14 @*/
PetscIntStackDestroy(PetscIntStack stack)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 }
23
24 /*@C
25 PetscIntStackEmpty - This function determines whether any items have been pushed.
26
27 Not Collective, No Fortran Support
28
29 Input Parameter:
30 . stack - The stack
31
32 Output Parameter:
33 . empty - `PETSC_TRUE` if the stack is empty
34
35 Level: developer
36
37 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
38 @*/
PetscIntStackEmpty(PetscIntStack stack,PetscBool * empty)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 }
47
48 /*@C
49 PetscIntStackTop - This function returns the top of the stack.
50
51 Not Collective, No Fortran Support
52
53 Input Parameter:
54 . stack - The stack
55
56 Output Parameter:
57 . top - The integer on top of the stack
58
59 Level: developer
60
61 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`
62 @*/
PetscIntStackTop(PetscIntStack stack,int * top)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 }
71
72 /*@C
73 PetscIntStackPush - This function pushes an integer on the stack.
74
75 Not Collective, No Fortran Support
76
77 Input Parameters:
78 + stack - The stack
79 - item - The integer to push
80
81 Level: developer
82
83 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPop()`, `PetscIntStackTop()`
84 @*/
PetscIntStackPush(PetscIntStack stack,int item)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 }
96
97 /*@C
98 PetscIntStackPop - This function pops an integer from the stack.
99
100 Not Collective, No Fortran Support
101
102 Input Parameter:
103 . stack - The stack
104
105 Output Parameter:
106 . item - The integer popped
107
108 Level: developer
109
110 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackTop()`
111 @*/
PetscIntStackPop(PetscIntStack stack,int * item)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 }
124
125 /*@C
126 PetscIntStackCreate - This function creates a stack.
127
128 Not Collective, No Fortran Support
129
130 Output Parameter:
131 . stack - The stack
132
133 Level: developer
134
135 .seealso: `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
136 @*/
PetscIntStackCreate(PetscIntStack * stack)137 PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
138 {
139 PetscFunctionBegin;
140 PetscAssertPointer(stack, 1);
141 PetscCall(PetscNew(stack));
142
143 (*stack)->top = -1;
144 (*stack)->max = 128;
145
146 PetscCall(PetscCalloc1((*stack)->max, &(*stack)->stack));
147 PetscFunctionReturn(PETSC_SUCCESS);
148 }
149