-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #1, update c++ stl stack to c stack
- Loading branch information
1 parent
087cf37
commit 8336d4e
Showing
5 changed files
with
158 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Source files to be used in the library | ||
set(LIB_SOURCES | ||
src/logger.c | ||
src/stack.c | ||
src/aproflib.cpp | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef COMAIR_STACK_H | ||
#define COMAIR_STACK_H | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
|
||
struct stack_elem { | ||
int funcId; // function id | ||
unsigned long ts; // time stamp | ||
unsigned long rms; | ||
unsigned long cost; | ||
struct stack_elem *next; | ||
}; | ||
|
||
struct stack { | ||
struct stack_elem *top; | ||
}; | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
|
||
|
||
/* | ||
* Converts a pointer to the stack's element ptr into a pointer | ||
* to the structure that ptr is embedded inside. Supply the name | ||
* of the outer struct, 'type', and the member name of the stack | ||
* element 'member'. Taken from the Linux kernel container_of(). | ||
*/ | ||
#define stack_entry(ptr, type, member) ({ \ | ||
const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | ||
(type *)( (char *)__mptr - offsetof(type,member) );}) | ||
|
||
void stack_init(struct stack *stack); | ||
void push(struct stack *stack, struct stack_elem *elem); | ||
struct stack_elem *pop(struct stack *); | ||
struct stack_elem *top(struct stack *); | ||
int size(struct stack *); | ||
int isEmpty(struct stack *); | ||
|
||
/* | ||
* Returns a stack with the cleared nodes contained. | ||
* You must iterate through the stack, convert the pointers | ||
* and free the pointers manually. | ||
*/ | ||
struct stack clear(struct stack *); | ||
|
||
}; | ||
#endif | ||
|
||
#endif //COMAIR_STACK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "stack.h" | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <assert.h> | ||
|
||
void stack_init(struct stack *stack) { | ||
assert(stack != NULL); | ||
stack->top = NULL; | ||
} | ||
|
||
void push(struct stack *stack, struct stack_elem *elem) { | ||
elem->next = stack->top; | ||
stack->top = elem; | ||
} | ||
|
||
struct stack_elem * pop(struct stack *stack) { | ||
|
||
if (isEmpty(stack) == 1) | ||
return NULL; | ||
|
||
else { | ||
struct stack_elem *node = stack->top; | ||
stack->top = stack->top->next; | ||
return node; | ||
} | ||
} | ||
|
||
struct stack_elem * top(struct stack *stack) { | ||
|
||
if (isEmpty(stack) == 1) | ||
return NULL; | ||
|
||
else | ||
return stack->top; | ||
} | ||
|
||
int size(struct stack *stack) { | ||
|
||
if (isEmpty(stack) == 1) | ||
return 0; | ||
|
||
else { | ||
struct stack_elem *temp; | ||
temp = stack->top; | ||
|
||
int count = 0; | ||
while (temp != NULL) { | ||
count++; | ||
temp = temp->next; | ||
} | ||
|
||
return count; | ||
} | ||
} | ||
|
||
int isEmpty(struct stack *stack) { | ||
|
||
if (stack->top == NULL) | ||
return 1; | ||
|
||
else | ||
return 0; | ||
} | ||
|
||
struct stack clear(struct stack *stack) { | ||
|
||
if (isEmpty(stack) == 0) { | ||
|
||
struct stack cleared_nodes; | ||
stack_init(&cleared_nodes); | ||
|
||
struct stack_elem *elem; | ||
elem = stack->top; | ||
|
||
while (elem != NULL) { | ||
|
||
push(&cleared_nodes, elem); | ||
|
||
elem = stack->top->next; | ||
stack->top = NULL; | ||
stack->top = elem; | ||
} | ||
|
||
stack->top = NULL; | ||
|
||
return cleared_nodes; | ||
} | ||
} |