封装红黑树代码-3 c++

首先展示测试结果
测试结果
在这里插入图片描述

内存测试结果
在这里插入图片描述

// 测试源代码
/*************************************************************************
    > File Name: test_map.cc
    > Author: hsz
    > Brief:
    > Created Time: Mon 05 Dec 2022 09:58:35 AM CST
 ************************************************************************/

#include <utils/map.h>
#include <utils/string8.h>
#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <gtest/gtest.h>

using namespace std;

int main(int argc, char **argv)
{
    eular::Map<eular::String8, int> mapObj;
    std::cout << "test map insert\n";
    std::cout << "\tinsert to map: [\"hello\", 100]\n";
    std::cout << "\tinsert to map: [\"world\", 200]\n";
    std::cout << "\tinsert to map: [\"world\", 300] (will failed)\n";
    mapObj.insert("hello", 100);
    mapObj.insert("world", 200);
    mapObj.insert("world", 300); // will failed
    EXPECT_EQ(mapObj.size(), 2);

    {
        std::cout << "test map find.\n";
        auto it = mapObj.find("hello");
        EXPECT_TRUE(it != mapObj.end());
    }
   
    {
        std::cout << "test map value.\n";
        auto val = mapObj.value("hello");
        EXPECT_EQ(val, 100);
    }

    {
        std::cout << "test map operator[]\n";
        auto &val = mapObj["hello"];
        EXPECT_EQ(val, 100);
        val = 400;
        EXPECT_EQ(mapObj.value("hello"), 400);
    }

    {
        std::cout << "test map erase.\n";
        auto it = mapObj.insert("test", 10000);
        EXPECT_TRUE(it != mapObj.end());
        mapObj.erase("test");
        EXPECT_EQ(mapObj.size(), 2);
    }

    {
        std::cout << "test map foreach.\n";
        for (auto it = mapObj.begin(); it != mapObj.end(); ++it) {
            std::cout << "\tkey = " << it.key() << ", value = " << it.value() << std::endl; 
        }
        std::cout << "test map foreach and erase.\n";
        mapObj.insert("test", 1000);
        for (auto it = mapObj.begin(); it != mapObj.end(); ) {
            std::cout << "\tkey = " << it.key() << ", value = " << it.value() << std::endl;
            if (it.key() == "test") {
                it = mapObj.erase(it);
            } else {
                ++it;
            }
        }
        EXPECT_EQ(mapObj.size(), 2);
    }

    {
        std::cout << "test map reforeach.\n";
        for (auto it = mapObj.rbegin(); it != mapObj.rend(); --it) {
            std::cout << "\tkey = " << it.key() << ", value = " << it.value() << std::endl; 
        }
    }

    {
        std::cout << "test map copy.\n";
        auto mapNew = mapObj;
        mapNew.insert("new", 600);
        std::cout << "insert to new map [\"new\", 600] and foreach new map\n";
        eular::Map<eular::String8, int>::const_iterator cit;
        for (cit = mapNew.begin(); cit != mapNew.end(); ++cit) {
            std::cout << "\tkey = " << cit.key() << ", value = " << cit.value() << std::endl; 
        }
        std::cout << "foreach old map\n";
        for (auto it = mapObj.begin(); it != mapObj.end(); ++it) {
            std::cout << "\tkey = " << it.key() << ", value = " << it.value() << std::endl; 
        }
    }

    {
        std::cout << "test map assign\n";
        eular::Map<eular::String8, int> mapNew;
        mapNew = mapObj;
        mapNew.insert("new", 600);
        std::cout << "insert to new map [\"new\", 600] and foreach new map\n";
        eular::Map<eular::String8, int>::const_iterator cit;
        for (cit = mapNew.begin(); cit != mapNew.end(); ++cit) {
            std::cout << "\tkey = " << cit.key() << ", value = " << cit.value() << std::endl; 
        }
        std::cout << "foreach old map\n";
        for (auto it = mapObj.begin(); it != mapObj.end(); ++it) {
            std::cout << "\tkey = " << it.key() << ", value = " << it.value() << std::endl; 
        }
    }

    {
        std::cout << "test map clear.\n";
        mapObj.clear();
        EXPECT_EQ(mapObj.size(), 0);
    }

    return 0;
}

源码详见github: https://github.com/TonyBeen/eular

参考:QMap
封装C++代码参考了QMap的设计,其中包括引用计数,placement new的alignment以及接口的设计方式都来自QMap,但是底层红黑树的插入删除是linux内核源码,这样做的目的是尽可能的减少bug的出现以及性能的提高

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值