修改list相关接口描述以及调通由于删除deque的接口导致的编译问题

This commit is contained in:
建峰 2025-04-29 14:53:54 +08:00
parent 446ede15db
commit 851f815120
7 changed files with 71 additions and 68 deletions

View File

@ -85,19 +85,18 @@ void* iter_next(); // 迭代器下一个元素
// -------------------- 扩展功能 -------------------- // -------------------- 扩展功能 --------------------
// 元素相关操作 // 元素相关操作
bool append(const void* obj); // 追加元素 <push_back> 一般用于list bool append(const void* obj); // 追加元素 <push_back> 一般用于list
bool delete(const void* obj); // 删除元素 // bool remove(const void *obj); // 删除元素 <暂不使用该命名>
// bool remove(const void *obj); // 删除元素 <同delete>
bool find(const void* obj); // 查找元素 <返回值bool/uint32_t/void*待定> bool find(const void* obj); // 查找元素 <返回值bool/uint32_t/void*待定>
bool contains(const void* obj); // 判断元素是否存在 <返回bool> bool contains(const void* obj); // 判断元素是否存在 <返回bool>
uint32_t count(const void* obj); // 统计元素个数 uint32_t count(const void* obj); // 统计元素obj的个数
// 索引相关操作 // 索引相关操作
uint32_t index(void *obj); // 获取元素索引 uint32_t index(void *obj); // 获取元素索引
bool insert(uint32_t index, const void* obj); // 插入元素 <非树> bool insert(uint32_t index, const void* obj); // 插入元素 <非树>
bool erase(uint32_t index); // 删除元素 bool delete(uint32_t index, void* obj); // 删除元素
// bool erase(uint32_t index); // 删除元素<暂时不用该命名>
bool set(uint32_t index, const void* obj); // 设置元素 bool set(uint32_t index, const void* obj); // 设置元素
bool get(uint32_t index, void* obj); // 获取元素 bool get(uint32_t index, void* obj); // 获取元素
``` ```
## 特点 ## 特点

View File

@ -140,6 +140,7 @@ static void demo_deque_num(void)
} }
} }
#if 0
printf("----- push_front -----\n"); printf("----- push_front -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
@ -173,6 +174,7 @@ static void demo_deque_num(void)
printf("\n"); printf("\n");
} }
} }
#endif
deque_free(&deque); deque_free(&deque);
} }
@ -334,7 +336,6 @@ static void demo_deque_struct(void)
printf("----- print -----\n"); printf("----- print -----\n");
deque->print(deque); deque->print(deque);
printf("\n"); printf("\n");
#endif
printf("----- get -----\n"); printf("----- get -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -346,6 +347,7 @@ static void demo_deque_struct(void)
printf("\n"); printf("\n");
} }
} }
#endif
deque_free(&deque); deque_free(&deque);
} }

View File

@ -32,15 +32,15 @@ static void demo_list_num(void)
printf("\n"); printf("\n");
printf("----- pop -----\n"); printf("----- pop -----\n");
list->pop(list, 9, NULL); list->delete(list, 9, NULL);
list->print(list); list->print(list);
printf("\n"); printf("\n");
list->pop(list, 0, NULL); list->delete(list, 0, NULL);
list->print(list); list->print(list);
printf("\n"); printf("\n");
list->pop(list, 4, NULL); list->delete(list, 4, NULL);
list->print(list); list->print(list);
printf("\n"); printf("\n");
@ -129,7 +129,7 @@ static void demo_list_num(void)
printf("----- pop -----\n"); printf("----- pop -----\n");
for (i = 0; i < len + 1; i++) for (i = 0; i < len + 1; i++)
{ {
list->pop(list, 0, &temp); list->pop(list, &temp);
if (list->empty(list)) if (list->empty(list))
{ {
@ -167,15 +167,15 @@ static void demo_list_struct(void)
printf("\n"); printf("\n");
printf("----- pop -----\n"); printf("----- pop -----\n");
list->pop(list, 9, NULL); list->delete(list, 9, NULL);
list->print(list); list->print(list);
printf("\n"); printf("\n");
list->pop(list, 0, NULL); list->delete(list, 0, NULL);
list->print(list); list->print(list);
printf("\n"); printf("\n");
list->pop(list, 4, NULL); list->delete(list, 4, NULL);
list->print(list); list->print(list);
printf("\n"); printf("\n");
@ -286,7 +286,7 @@ static void demo_list_struct(void)
printf("----- pop -----\n"); printf("----- pop -----\n");
for (i = 0; i < len + 1; i++) for (i = 0; i < len + 1; i++)
{ {
list->pop(list, 0, &temp); list->pop(list, &temp);
if (list->empty(list)) if (list->empty(list))
{ {

View File

@ -35,16 +35,18 @@ struct _list
// -------------------- public -------------------- // -------------------- public --------------------
// kernel // kernel
bool (*append)(struct _list *self, void *obj); // Append object to the end of the list. bool (*append)(struct _list *self, void *obj);
bool (*insert)(struct _list *self, int index, void *obj); // Insert object before index. bool (*pop)(struct _list *self, void *obj);
bool (*pop)(struct _list *self, int index, void *obj); // Remove and return item at index.
int (*index)(struct _list *self, void *obj); // Return first index of obj. Return -1 if the obj is not present. bool (*insert)(struct _list *self, int index, void *obj);
bool (*remove)(struct _list *self, void *obj); // Remove first occurrence of obj. bool (*delete)(struct _list *self, int index, void *obj);
bool (*get)(struct _list *self, int index, void *obj); bool (*get)(struct _list *self, int index, void *obj);
bool (*set)(struct _list *self, int index, void *obj); bool (*set)(struct _list *self, int index, void *obj);
int (*index)(struct _list *self, void *obj);
// bool (*contains)(struct _list *self, void *obj);
// base // base
uint32_t (*size)(struct _list *self); uint32_t (*size)(struct _list *self);
uint32_t (*capacity)(struct _list *self); uint32_t (*capacity)(struct _list *self);
@ -54,15 +56,8 @@ struct _list
// iter // iter
iterator_t (*iter)(struct _list *self); iterator_t (*iter)(struct _list *self);
// sort // config
// bool (*reverse)(struct _list* self); // Reverse *IN PLACE*. compare_fun_t compare; // !!! you have to implement this function
/**
Sort the list in ascending order and return false.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
order of two equal elements is maintained).
The reverse flag can be set to sort in descending order.
*/
// bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2));
// -------------------- debug -------------------- // -------------------- debug --------------------
void (*print)(struct _list *self); void (*print)(struct _list *self);

View File

@ -35,6 +35,27 @@ static bool list_append(struct _list* self, void* obj)
return true; return true;
} }
static bool list_pop(struct _list* self, void* obj)
{
assert(self != NULL);
if (self->empty(self))
{
return false;
}
uint32_t index = self->size(self) - 1;
uint32_t offset = index * self->_obj_size;
uint32_t offset_next = (index + 1) * self->_obj_size;
if (obj != NULL)
{
memmove(obj, (char*)self->obj + offset, self->_obj_size);
}
memmove((char*)self->obj + offset, (char*)self->obj + offset_next, self->_obj_size);
self->_size -= 1;
return true;
}
static bool list_insert(struct _list* self, int index, void* obj) static bool list_insert(struct _list* self, int index, void* obj)
{ {
assert(index >= 0 && index < (int)self->size(self)); assert(index >= 0 && index < (int)self->size(self));
@ -58,7 +79,7 @@ static bool list_insert(struct _list* self, int index, void* obj)
return true; return true;
} }
static bool list_pop(struct _list* self, int index, void* obj) static bool list_delete(struct _list* self, int index, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
@ -90,12 +111,6 @@ static int list_index(struct _list* self, void* obj)
return 0; return 0;
} }
static bool list_remove(struct _list* self, void* obj)
{
assert(self != NULL);
return true;
}
static bool list_clear(struct _list* self) static bool list_clear(struct _list* self)
{ {
assert(self != NULL); assert(self != NULL);
@ -142,16 +157,6 @@ static bool list_empty(struct _list* self)
return !self->size(self); return !self->size(self);
} }
static bool list_reverse(struct _list* self)
{
return true;
}
static bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2))
{
return true;
}
// free // free
static void list_destory(struct _list* self) static void list_destory(struct _list* self)
{ {
@ -240,27 +245,25 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
// -------------------- public -------------------- // -------------------- public --------------------
// kernel // kernel
list->append = list_append; list->append = list_append;
list->insert = list_insert;
list->pop = list_pop; list->pop = list_pop;
list->empty = list_empty; list->insert = list_insert;
list->delete = list_delete;
list->get = list_get;
list->set = list_set;
list->index = list_index;
// list->contains = list_contains;
// base // base
list->clear = list_clear; list->clear = list_clear;
list->size = list_size; list->size = list_size;
list->empty = list_empty;
// iter // iter
list->iter = list_iter; list->iter = list_iter;
// others
list->index = list_index;
list->remove = list_remove;
list->get = list_get;
list->set = list_set;
// list->reverse = list_reverse;
// list->sort = list_sort;
// -------------------- debug -------------------- // -------------------- debug --------------------
list->print = list_print; list->print = list_print;

View File

@ -113,6 +113,7 @@ static void test_deque_num(void)
} }
} }
#if 0
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i])); TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
@ -130,6 +131,7 @@ static void test_deque_num(void)
{ {
TEST_ASSERT_TRUE(deque->get(deque, i, &temp)); TEST_ASSERT_TRUE(deque->get(deque, i, &temp));
} }
#endif
deque_free(&deque); deque_free(&deque);
TEST_ASSERT_NULL(deque); TEST_ASSERT_NULL(deque);
@ -254,10 +256,12 @@ static void test_deque_struct(void)
TEST_ASSERT_TRUE(deque->push_front(deque, &data[i])); TEST_ASSERT_TRUE(deque->push_front(deque, &data[i]));
} }
#if 0
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
TEST_ASSERT_TRUE(deque->get(deque, i, &temp)); TEST_ASSERT_TRUE(deque->get(deque, i, &temp));
} }
#endif
deque_free(&deque); deque_free(&deque);
TEST_ASSERT_NULL(deque); TEST_ASSERT_NULL(deque);

View File

@ -80,10 +80,10 @@ static void test_list_pop(void)
} }
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
TEST_ASSERT_TRUE(list->pop(list, 0, &temp)); TEST_ASSERT_TRUE(list->pop(list, &temp));
} }
TEST_ASSERT_TRUE(list->empty(list)); TEST_ASSERT_TRUE(list->empty(list));
// TEST_ASSERT_FALSE(list->pop(list, 1, &temp)); TEST_ASSERT_FALSE(list->pop(list, &temp));
list_free(&list); list_free(&list);
} }
@ -129,9 +129,9 @@ static void test_list_num(void)
{ {
TEST_ASSERT_TRUE(list->append(list, &data[i])); TEST_ASSERT_TRUE(list->append(list, &data[i]));
} }
TEST_ASSERT_TRUE(list->pop(list, 9, NULL)); TEST_ASSERT_TRUE(list->delete(list, 9, NULL));
TEST_ASSERT_TRUE(list->pop(list, 0, NULL)); TEST_ASSERT_TRUE(list->delete(list, 0, NULL));
TEST_ASSERT_TRUE(list->pop(list, 4, NULL)); TEST_ASSERT_TRUE(list->delete(list, 4, NULL));
TEST_ASSERT_TRUE(list->clear(list)); TEST_ASSERT_TRUE(list->clear(list));
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -182,7 +182,7 @@ static void test_list_num(void)
for (i = 0; i < len + 1; i++) for (i = 0; i < len + 1; i++)
{ {
TEST_ASSERT_TRUE(list->pop(list, 0, &temp)); TEST_ASSERT_TRUE(list->pop(list, &temp));
if (list->empty(list)) if (list->empty(list))
{ {
@ -213,9 +213,9 @@ static void test_list_struct(void)
{ {
TEST_ASSERT_TRUE(list->append(list, &data[i])); TEST_ASSERT_TRUE(list->append(list, &data[i]));
} }
TEST_ASSERT_TRUE(list->pop(list, 9, NULL)); TEST_ASSERT_TRUE(list->delete(list, 9, NULL));
TEST_ASSERT_TRUE(list->pop(list, 0, NULL)); TEST_ASSERT_TRUE(list->delete(list, 0, NULL));
TEST_ASSERT_TRUE(list->pop(list, 4, NULL)); TEST_ASSERT_TRUE(list->delete(list, 4, NULL));
TEST_ASSERT_TRUE(list->clear(list)); TEST_ASSERT_TRUE(list->clear(list));
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -272,7 +272,7 @@ static void test_list_struct(void)
for (i = 0; i < len + 1; i++) for (i = 0; i < len + 1; i++)
{ {
TEST_ASSERT_TRUE(list->pop(list, 0, &temp)); TEST_ASSERT_TRUE(list->pop(list, &temp));
if (list->empty(list)) if (list->empty(list))
{ {