pikapython/src/dataLink.c

128 lines
3.6 KiB
C
Raw Normal View History

2021-10-02 19:20:18 +08:00
/*
2021-11-16 08:46:44 +08:00
* This file is part of the PikaScript project.
* http://github.com/pikastech/pikascript
*
* MIT License
*
2021-11-16 08:49:33 +08:00
* Copyright (c) 2021 lyon liang6516@outlook.com
2021-11-16 08:46:44 +08:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2021-10-02 19:20:18 +08:00
2021-10-01 00:21:50 +08:00
#include "dataLink.h"
#include "dataArg.h"
#include "dataLinkNode.h"
#include "dataMemory.h"
2022-03-09 13:22:55 +08:00
void __link_deinit_pyload(Link* self) {
2021-10-02 19:20:18 +08:00
LinkNode* nowNode = self->firstNode;
while (NULL != nowNode) {
LinkNode* nodeNext = (LinkNode*)arg_getNext((Arg*)nowNode);
2021-10-02 19:20:18 +08:00
linkNode_deinit(nowNode);
nowNode = nodeNext;
}
self = NULL;
2021-10-01 00:21:50 +08:00
}
2022-03-09 13:22:55 +08:00
void link_deinit(Link* self) {
__link_deinit_pyload(self);
pikaFree(self, sizeof(Link));
}
void link_deinit_stack(Link* self) {
__link_deinit_pyload(self);
}
2021-10-01 00:21:50 +08:00
void link_addNode(Link* self, void* content) {
2021-10-02 19:20:18 +08:00
// old first node become new second node
LinkNode* secondNode = self->firstNode;
2021-10-01 00:21:50 +08:00
2021-10-02 19:20:18 +08:00
self->firstNode = content;
// change the first node to new node
arg_setNext(content, (Arg*)secondNode);
2021-10-01 00:21:50 +08:00
}
static void __link_removeNode(Link* self,
void* content,
uint8_t is_deinit_node) {
2021-10-02 19:20:18 +08:00
LinkNode* nodeToDelete = NULL;
LinkNode* nodeNow = self->firstNode;
LinkNode* priorNode = NULL;
2021-11-14 19:22:45 +08:00
LinkNode* nextNode;
2021-10-02 19:20:18 +08:00
while (1) {
if (nodeNow == content) {
nodeToDelete = nodeNow;
break;
}
if (nodeNow == NULL) {
// error, node no found
goto exit;
}
priorNode = nodeNow;
nodeNow = (LinkNode*)arg_getNext((Arg*)nodeNow);
2021-10-01 00:21:50 +08:00
}
2021-10-02 19:20:18 +08:00
nextNode = (LinkNode*)arg_getNext((Arg*)nodeToDelete);
2021-10-02 19:20:18 +08:00
if (nodeToDelete == self->firstNode) {
self->firstNode = (LinkNode*)arg_getNext((Arg*)nodeToDelete);
2021-10-01 00:21:50 +08:00
}
2021-10-02 19:20:18 +08:00
if (NULL == priorNode) {
self->firstNode = nextNode;
goto exit;
}
arg_setNext((Arg*)priorNode, (Arg*)nextNode);
2021-10-01 00:21:50 +08:00
goto exit;
// deinit the node
exit:
if (is_deinit_node) {
linkNode_deinit(nodeToDelete);
}
2021-10-02 19:20:18 +08:00
return;
2021-10-01 00:21:50 +08:00
}
void link_removeNode(Link* self, void* content) {
__link_removeNode(self, content, 1);
}
void link_removeNode_notDeinitNode(Link* self, void* content) {
__link_removeNode(self, content, 0);
}
2021-10-01 00:21:50 +08:00
int32_t link_getSize(Link* self) {
2021-10-02 19:20:18 +08:00
LinkNode* NowNode;
int32_t size = 0;
NowNode = self->firstNode;
while (NULL != NowNode) {
size++;
NowNode = (LinkNode*)arg_getNext((Arg*)NowNode);
2021-10-02 19:20:18 +08:00
}
return size;
2021-10-01 00:21:50 +08:00
}
void link_init(Link* self, void* args) {
2021-10-02 19:20:18 +08:00
self->firstNode = NULL;
2021-10-01 00:21:50 +08:00
}
Link* New_link(void* args) {
2021-10-02 19:20:18 +08:00
Link* self = pikaMalloc(sizeof(Link));
link_init(self, args);
return self;
2021-10-01 00:21:50 +08:00
}