可pop任意字节队列代码的优化

电子说

1.3w人已加入

描述

前言

主要是对代码进行了优化,优化的点有下面几项

1 对代码结构进行了调整。

上次的版本只是一个简单的雏形,这份代码进一步分成了头文件和实现文件。

2 优化了类的封装性,对具体实现进行了隐藏。

因为这个功能是通过头结点的链表的实现的,而在上分代码中,使用者可以通过调用的到头结点信息,然后使用者就可以对这个链表进行任意操作,这样就很危险。所以在这版中,将所有入口都改成了void *数据类型,在实现的地方进行类型转换,增加了安全性。

具体实现

头文件

#include 
#include 
#include 
#include 
#include 


/*
 * MemLink Brief Description
 *
 * link0
 * addr(id, sum, size)->node->node->node->node...
 * 
 * link1
 * addr(id, sum, size)->node->node->node->node...
 * 
 * link2
 * addr(id, sum, size)->node->node->node->node...
 * 
 */


class MemLink
{
public:
    MemLink() { m_mem_link.clear(); }
    ~MemLink() {}


    //create link by id
    int create_link(int id);
    //get link addr, for first para is *addr, !!need to traverse!!
    void *get_addr_by_id(int id);
    //get link addr, for first para is *addr
    void *get_addr_by_index(int index);
    int push_data(void *addr, char *data, int len);
    int read_data(void *addr, char *data, int len);
    int pop_data(void *addr, char *data, int len);


    //delete one link
    int delete_link(void *addr);
    //delete all links
    int delete_memlink();


    //get link description
    int get_id_by_addr(void *addr);
    int get_size_by_addr(void *addr);
    int get_num_by_addr(void *addr);
    int get_memlink_num();
    void printf_memlink_status();


    void link_test();


private:
    std::vector<void *> m_mem_link;


    void *mk_node(int id, char *data, int len);
    int free_node(void *node);
    int push(void *addr, void *node);
    void *pop(void *addr);
    void printf_link(void *addr);   //debug, when data is ascii, can call it
};

实现文件

#include "test_mem_link.h"


typedef struct Node
{
    int id;     //unique id
    int pos;    //data start pos
    int size;   //data size --> value size = size-pos
    int num;    //for head only
    char *data;
    struct Node *next;
} Node;


void *MemLink::mk_node(int id, char *data, int len)
{
    Node *node = new Node;
    if (node == NULL) {
        perror("new Node failed\\n");
        return NULL;
    }
    if (data != NULL) {
        node->data = new char[len]();
        if (node->data == NULL) {
            perror("node->data = new failed");
            return NULL;
        }
        memcpy(node->data, data, len);
    }
    else node->data = NULL;


    node->id = id;
    node->pos = 0;
    node->size = len;
    node->num = 0;
    node->next = NULL;
    return (void *)node;
}


int MemLink::free_node(void *addr)
{
    Node *node = (Node *)addr;
    if (node->data != NULL)
        delete[] node->data;
    node->data = NULL;
    if (node)
        delete node;
    node = NULL;
    return 0;
}


void *MemLink::get_addr_by_id(int id)
{
    int i = 0;
    for (i = 0; i < (int)m_mem_link.size(); i++) {
        if (id == ((Node *)m_mem_link[i])->id) {
            return (Node *)m_mem_link[i];
        }
    }
    return NULL;
}


int MemLink::create_link(int id)
{
    Node *node_ret = (Node *)get_addr_by_id(id);
    if (node_ret != NULL) {
        printf("link id[%d] exist, can't create it\\n", id);
        return -1;
    }


    printf("link id[%d] not exist, create it\\n", id);
    Node *node = (Node *)mk_node(id, NULL, 0);
    if (node == NULL) {
        printf("mk_node failed!\\n");
    }
    else {
        m_mem_link.push_back((void *)node);
    }


    return 0;
}


int MemLink::push(void *addr, void *node)
{
    Node *p = (Node *)addr;
    while (p->next != NULL) {
        p = p->next;
    }


    ((Node *)node)->next = p->next;
    p->next = ((Node *)node);


    ((Node *)addr)->size += ((Node *)node)->size;
    ((Node *)addr)->num++;
    return 0;
}


int MemLink::get_id_by_addr(void *addr)
{
    return ((Node *)addr)->id;
}
int MemLink::get_size_by_addr(void *addr)
{
    return ((Node *)addr)->size;
}
int MemLink::get_num_by_addr(void *addr)
{
    return ((Node *)addr)->num;
}


int MemLink::push_data(void *addr, char *data, int len)
{
    int id = get_id_by_addr(addr);
    Node *phead = (Node *)addr;
    Node *node = (Node *)mk_node(id, data, len);
    if (node == NULL) {
        printf("mk_node failed!\\n");
    }
    push(phead, node);
}


void *MemLink::pop(void *addr)
{
    Node *p = ((Node *)addr)->next;
    if (p != NULL) {
        ((Node *)addr)->size -= p->size - p->pos;
        ((Node *)addr)->next = p->next;
        ((Node *)addr)->num--;
        p->next = NULL;
        return p;
    }
    return NULL;
}


void *MemLink::get_addr_by_index(int index)
{
    return m_mem_link[index];
}


int MemLink::read_data(void *addr, char *data, int len)
{
    Node *phead = (Node *)addr;
    if (len > phead->size) {
        printf("pop data len > link size, read data failed\\n");
        return -1;
    }


    int i_read = 0;
    Node *p = phead->next;
    while (1) {
        if (p == NULL) {
            printf("p == NULL\\n");
            break;
        }


        if (i_read + p->size - p->pos > len) {
            //printf(" < len\\n");
            memcpy(&data[i_read], &p->data[p->pos], len - i_read);
            break;
        }


        if (i_read + p->size - p->pos == len) {
            //printf(" = len\\n");
            memcpy(&data[i_read], &p->data[p->pos], p->size - p->pos);
            break;
        }


        if (i_read + p->size - p->pos < len) {
            //printf(" > len\\n");
            memcpy(&data[i_read], &p->data[p->pos], p->size - p->pos);
            i_read += p->size - p->pos;
            p = p->next;
        }
    }
    return 0;
}


int MemLink::pop_data(void *addr, char *data, int len)
{
    Node *phead = (Node *)addr;
    if (len > phead->size) {
        printf("pop data len > link size, read data failed\\n");
        return -1;
    }


    Node *phead_tmp = phead;
    int i_read = 0;
    while (1) {
        Node *p = phead_tmp->next;
        if (p == NULL) {
            printf("p == NULL\\n");
            break;
        }


        if (i_read + p->size - p->pos > len) {
            // printf(" > len\\n");
            memcpy(&data[i_read], &p->data[p->pos], len - i_read);


            p->pos += len - i_read;
            phead->size -= len - i_read;
            break;
        }


        if (i_read + p->size - p->pos == len) {
            // printf(" = len\\n");
            memcpy(&data[i_read], &p->data[p->pos], p->size - p->pos);


            phead->size -= p->size - p->pos;
            phead_tmp->next = p->next;
            phead->num--;
            free_node(p);
            p = NULL;
            break;
        }


        if (i_read + p->size - p->pos < len) {
            // printf(" < len\\n");
            memcpy(&data[i_read], &p->data[p->pos], p->size - p->pos);
            i_read += p->size - p->pos;


            phead->size -= p->size - p->pos;
            phead_tmp->next = p->next;
            phead->num--;
            free_node(p);
            p = NULL;
        }
    }
    return 0;
}


int MemLink::get_memlink_num()
{
    return m_mem_link.size();
}


void MemLink::printf_memlink_status()
{
    int i = 0;
    for (i = 0; i < (int)m_mem_link.size(); i++) {
        Node *phead = (Node *)m_mem_link[i];
        printf(">link: id: %d, pos: %d, num: %d, size: %d, data: %s\\n", phead->id,
                            phead->pos, phead->num, phead->size, phead->data);
    }
}


void MemLink::printf_link(void *addr)
{
    Node *phead = (Node *)addr;
    printf(">link: id: %d, pos: %d, num: %d, size: %d, data: %s\\n", phead->id,
                        phead->pos, phead->num, phead->size, phead->data);
    Node *p = phead->next;
    while (p) {
        printf("node:  id: %d, pos: %d, size: %d\\n", p->id, p->pos, p->size);
        p = p->next;
    }
}


int MemLink::delete_link(void *addr)
{
    int id = get_id_by_addr(addr);
    Node *phead = (Node *)addr;
    Node *p = phead->next;
    while (phead->next) {
        p = phead->next;
        phead->next = p->next;
        free_node(p);
    }


    std::vector<void *>::iterator it;
    for (it = m_mem_link.begin(); it != m_mem_link.end(); ) {
        //if (((Node *)it)->id == get_id_by_addr(addr)))
        if (*it == addr) {
            //printf("%d = %d\\n", get_id_by_addr(*it), get_id_by_addr(addr));
            it = m_mem_link.erase(it);
            break;
        }
        else it++;
    }


    free_node(phead);
    printf("delete id[%d] link\\n", id);


    return 0;
}


int MemLink::delete_memlink()
{
    int i = 0;
    for (i = 0; i < (int)m_mem_link.size(); i++) {
        int id = get_id_by_addr(m_mem_link[i]);
        Node *phead = (Node *)m_mem_link[i];
        Node *p = phead->next;
        while (phead->next) {
            p = phead->next;
            phead->next = p->next;
            free_node(p);
        }
        printf("delete id[%d] link\\n", id);
        free_node(phead);
    }
    m_mem_link.clear();
    return 0;
}


void MemLink::link_test()
{
    int id = 10;
    create_link(id);


    // push
    char tmp[10] = {0};
    void *addr = get_addr_by_id(id);
    strcpy(tmp, "111222333");
    push_data(addr, tmp, strlen(tmp));
    strcpy(tmp, "444555666");
    push_data(addr, tmp, strlen(tmp));
    strcpy(tmp, "777888999");
    push_data(addr, tmp, strlen(tmp));


    push_data(addr, (char *)"1", strlen("1"));
    push_data(addr, (char *)"2", strlen("2"));
    push_data(addr, (char *)"3", strlen("3"));
    push_data(addr, (char *)"4", strlen("4"));
    push_data(addr, (char *)"5", strlen("5"));
    push_data(addr, (char *)"6", strlen("6"));
    push_data(addr, (char *)"7", strlen("7"));


    id = 20;
    create_link(id);
    addr = get_addr_by_id(id);
    push_data(addr, (char *)"8", strlen("8"));
    push_data(addr, (char *)"9", strlen("9"));
    push_data(addr, (char *)"10", strlen("10"));


    id = 30;
    create_link(id);
    addr = get_addr_by_id(id);
    push_data(addr, (char *)"11", strlen("11"));
    push_data(addr, (char *)"12", strlen("12"));
    push_data(addr, (char *)"13", strlen("13"));




    printf_memlink_status();
    int memlink_num = get_memlink_num();
    printf("\\nmemlink_num = %d\\n", memlink_num);
    for (int i = 0; i < memlink_num; i++) {
        void *addr = get_addr_by_index(i);
        // printf_link(addr);
        if (get_id_by_addr(addr) == 10) {
            // //pop
            // Node *p4 = pop(phead);
            // printf("pop size = %d\\n", p4->size);
            // free_node(p4);


            char read_tmp[11 + 1] = {0};
            pop_data(addr, read_tmp, 11);
            printf("read_data = %s\\n", read_tmp);


            char read_tmp2[8 + 1] = {0};
            pop_data(addr, read_tmp2, 8);
            printf("read_data = %s\\n", read_tmp2);
            // printf_link(addr);
        }
    }


    addr = get_addr_by_id(20);
    delete_link(addr);


    delete_memlink();
}


int main()
{
    MemLink ml;
    ml.link_test();


    return 0;
}

审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分