mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-04 16:06:53 +08:00
给stack添加迭代器,但为什么heap会报错??
This commit is contained in:
parent
d805d56fc9
commit
07b2c142b8
@ -11,9 +11,11 @@
|
|||||||
#ifndef _COMMON_H_
|
#ifndef _COMMON_H_
|
||||||
#define _COMMON_H_
|
#define _COMMON_H_
|
||||||
|
|
||||||
// #ifdef UNICSTL_CONFIG
|
#define UNICSTL_CONFIG
|
||||||
|
|
||||||
|
#ifdef UNICSTL_CONFIG
|
||||||
#include "unicstl_config.h"
|
#include "unicstl_config.h"
|
||||||
// #endif
|
#endif
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -23,4 +25,8 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#ifdef UNICSTL_ITERATOR
|
||||||
|
#include "iterator.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // _COMMON_H_
|
#endif // _COMMON_H_
|
||||||
|
@ -21,6 +21,7 @@ struct _stack_node
|
|||||||
|
|
||||||
struct _stack
|
struct _stack
|
||||||
{
|
{
|
||||||
|
// ---------- private ----------
|
||||||
struct _stack_node * _head;
|
struct _stack_node * _head;
|
||||||
|
|
||||||
uint32_t _size;
|
uint32_t _size;
|
||||||
@ -28,21 +29,26 @@ struct _stack
|
|||||||
uint32_t _capacity;
|
uint32_t _capacity;
|
||||||
uint32_t _ratio;
|
uint32_t _ratio;
|
||||||
|
|
||||||
|
struct _iterator _iter;
|
||||||
|
|
||||||
|
void (*destory)(struct _stack* self);
|
||||||
|
|
||||||
|
// ---------- public ----------
|
||||||
// kernel
|
// kernel
|
||||||
bool (*push)(struct _stack* self, void* obj);
|
bool (*push)(struct _stack* self, void* obj);
|
||||||
bool (*pop)(struct _stack* self, void* obj);
|
bool (*pop)(struct _stack* self, void* obj);
|
||||||
bool (*peek)(struct _stack* self, void* obj);
|
bool (*peek)(struct _stack* self, void* obj);
|
||||||
|
bool (*empty)(struct _stack* self);
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t (*size)(struct _stack* self);
|
uint32_t (*size)(struct _stack* self);
|
||||||
bool (*empty)(struct _stack* self);
|
|
||||||
uint32_t (*capacity)(struct _stack* self);
|
uint32_t (*capacity)(struct _stack* self);
|
||||||
|
|
||||||
// clear and free node
|
|
||||||
bool (*clear)(struct _stack* self);
|
bool (*clear)(struct _stack* self);
|
||||||
void (*destory)(struct _stack* self);
|
|
||||||
|
// iter
|
||||||
|
iterator_t (*iter)(struct _stack* self);
|
||||||
|
|
||||||
// print
|
// ---------- debug ----------
|
||||||
void (*print)(struct _stack* self);
|
void (*print)(struct _stack* self);
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#define UNICSTL_TREE
|
#define UNICSTL_TREE
|
||||||
#define UNICSTL_HEAP
|
#define UNICSTL_HEAP
|
||||||
#define UNICSTL_GRAPH
|
#define UNICSTL_GRAPH
|
||||||
|
#define UNICSTL_ITERATOR
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief debug
|
* @brief debug
|
||||||
@ -29,5 +30,14 @@
|
|||||||
#define NDEBUG
|
#define NDEBUG
|
||||||
#define UNICSTL_DEBUG
|
#define UNICSTL_DEBUG
|
||||||
|
|
||||||
|
#ifdef UNICSTL_DEBUG
|
||||||
|
#define UNICSTL_DEBUG_STACK
|
||||||
|
#define UNICSTL_DEBUG_QUEUE
|
||||||
|
#define UNICSTL_DEBUG_DEQUE
|
||||||
|
#define UNICSTL_DEBUG_TREE
|
||||||
|
#define UNICSTL_DEBUG_HEAP
|
||||||
|
#define UNICSTL_DEBUG_GRAPH
|
||||||
|
// #define UNICSTL_DEBUG_ITERATOR
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
75
src/stack.c
75
src/stack.c
@ -298,48 +298,87 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const void* stack_iter_next(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
stack_t self = (stack_t)iter->parent;
|
||||||
|
void *obj = self->_head->obj + self->_iter._cur * self->_obj_size;
|
||||||
|
self->_iter._cur += 1;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool stack_iter_hasnext(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
stack_t self = (stack_t)iter->parent;
|
||||||
|
|
||||||
|
if(self->_iter._cur < self->size(self))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t stack_iter(struct _stack* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if (self == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
self->_iter._cur = 0;
|
||||||
|
return &self->_iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
|
|
||||||
|
// ---------- private ----------
|
||||||
// 1. set attr
|
// 1. set attr
|
||||||
self->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
self->_size = 0;
|
self->_size = 0;
|
||||||
self->_capacity = capacity;
|
self->_capacity = capacity;
|
||||||
self->_ratio = 2;
|
self->_ratio = 2;
|
||||||
|
|
||||||
// 2. set function
|
self->_iter.next = stack_iter_next;
|
||||||
// kernel
|
self->_iter.hasnext = stack_iter_hasnext;
|
||||||
self->push = stack2_push;
|
|
||||||
self->pop = stack2_pop;
|
|
||||||
self->peek = stack2_peek;
|
|
||||||
|
|
||||||
// others
|
|
||||||
self->empty = stack_empty;
|
|
||||||
self->size = stack_size;
|
|
||||||
self->capacity = stack_capacity;
|
|
||||||
|
|
||||||
// clear and free node
|
|
||||||
self->clear = stack_clear;
|
|
||||||
self->destory = stack2_destory;
|
|
||||||
// print
|
|
||||||
self->print = stack2_print;
|
|
||||||
|
|
||||||
// 3. set node
|
|
||||||
self->_head = (struct _stack_node*)malloc(sizeof(struct _stack_node));
|
self->_head = (struct _stack_node*)malloc(sizeof(struct _stack_node));
|
||||||
if (self->_head == NULL)
|
if (self->_head == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// self->_head->obj = NULL;
|
|
||||||
self->_head->next = NULL;
|
self->_head->next = NULL;
|
||||||
|
|
||||||
// 4. set array
|
// 4. set array
|
||||||
self->_head->obj = (void *)calloc(self->_capacity, self->_obj_size);
|
self->_head->obj = (void *)calloc(self->_capacity, self->_obj_size);
|
||||||
if (self->_head->obj == NULL)
|
if (self->_head->obj == NULL)
|
||||||
{
|
{
|
||||||
|
free(self->_head);
|
||||||
|
self->_head = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self->destory = stack2_destory;
|
||||||
|
|
||||||
|
// ---------- public ----------
|
||||||
|
// 2. set function
|
||||||
|
// kernel
|
||||||
|
self->push = stack2_push;
|
||||||
|
self->pop = stack2_pop;
|
||||||
|
self->peek = stack2_peek;
|
||||||
|
self->empty = stack_empty;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->size = stack_size;
|
||||||
|
self->capacity = stack_capacity;
|
||||||
|
self->clear = stack_clear;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->iter = stack_iter;
|
||||||
|
|
||||||
|
// ---------- debug ----------
|
||||||
|
self->print = stack2_print;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +107,7 @@ static void test_heap_struct(void)
|
|||||||
|
|
||||||
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
|
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
|
||||||
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
|
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
|
||||||
|
// debug :Why is it occasionally incorrect here?
|
||||||
TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id);
|
TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id);
|
||||||
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name);
|
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user