Xmipp  v3.23.11-Nereus
stack.cpp
Go to the documentation of this file.
1 #include "defines.h"
2 #ifdef LOGGING
3 #include "log.h"
4 #endif
5 #include "stack.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 
10 int initialize_Stack(struct stack_T *stack, int size)
11 {
12  int ret=SUCCESS; // Return value.
13 
14  // Initialize stack attributes.
15  stack->size = size;
16  stack->nElements = 0;
17  stack->data = (struct Convex_T *) malloc(sizeof(struct Convex_T)*size);
18 
19  // Check error allocating memory.
20  if (stack->data == NULL)
21  {
22  ret = FAILURE;
23  printf("Error allocating memory in initialize_Stack");
24 #ifdef LOGGING
25  sprintf( log_Text, "Error allocating memory in initialize_Stack\n");
26  write_Log( log_Text);
27 #endif
28  }
29 
30  return(ret);
31 }
32 
33 struct Convex_T peak(struct stack_T *stack)
34 {
35  // Get peak element of the stack.
36  return(stack->data[stack->nElements-1]);
37 }
38 
39 struct Convex_T peak_Prev(struct stack_T *stack)
40 {
41  // Get the top 2 elements of the stack.
42  return(stack->data[stack->nElements-2]);
43 }
44 
45 void update_Peak(struct stack_T *stack, int edge)
46 {
47  // Update edge of peak of the stack.
48  stack->data[stack->nElements-1].edge_ID = edge;
49 }
50 
51 void push(struct stack_T *stack, struct Convex_T edge)
52 {
53  // Insert a new element in the top of the stack.
54  stack->data[stack->nElements] = edge;
55  stack->nElements++;
56 }
57 
58 
59 void pop(struct stack_T *stack)
60 {
61  // Check if stack is not empty.
62  if (stack->nElements > 0)
63  {
64  // Decrease number of elements.
65  stack->nElements--;
66  }
67 }
68 
69 int get_Stack_nElements(struct stack_T *stack)
70 {
71  // Return number of elements.
72  return(stack->nElements);
73 }
74 
75 void reset_Stack(struct stack_T *stack)
76 {
77  // Reset data of the stack (without resizing).
78  stack->nElements = 0;
79  memset( stack->data, 0, sizeof(struct Convex_T)*stack->size);
80 }
81 
82 void print_Stack(struct stack_T *stack)
83 {
84  int i=0; // Loop counter.
85 
86  // Print all elements in stack.
87  for (i=0; i<stack->nElements ;i++)
88  {
89  printf("Position: %d. Edge id: %d with origin vertex %d.\n",
90  i,
91  stack->data[i].edge_ID,
92  stack->data[i].vertex_Index);
93  }
94 }
95 
96 void finalize_Stack(struct stack_T *stack)
97 {
98  // Deallocate memory.
99  stack->size = 0;
100  stack->nElements = 0;
101  if (stack->data != NULL)
102  {
103  free(stack->data);
104  stack->data = NULL;
105  }
106 }
#define FAILURE
Definition: defines.h:20
int initialize_Stack(struct stack_T *stack, int size)
Definition: stack.cpp:10
void print_Stack(struct stack_T *stack)
Definition: stack.cpp:82
void push(struct stack_T *stack, struct Convex_T edge)
Definition: stack.cpp:51
Definition: stack.h:15
void finalize_Stack(struct stack_T *stack)
Definition: stack.cpp:96
#define i
int size
Definition: stack.h:17
int vertex_Index
Definition: stack.h:11
#define SUCCESS
Definition: defines.h:21
int edge_ID
Definition: stack.h:12
free((char *) ob)
void update_Peak(struct stack_T *stack, int edge)
Definition: stack.cpp:45
int nElements
Definition: stack.h:18
int get_Stack_nElements(struct stack_T *stack)
Definition: stack.cpp:69
Definition: stack.h:9
struct Convex_T peak(struct stack_T *stack)
Definition: stack.cpp:33
struct Convex_T peak_Prev(struct stack_T *stack)
Definition: stack.cpp:39
struct Convex_T * data
Definition: stack.h:19
void reset_Stack(struct stack_T *stack)
Definition: stack.cpp:75
void pop(struct stack_T *stack)
Definition: stack.cpp:59