github源码指引:共享内存、数据结构与算法:单向链表

初级代码游戏的专栏介绍与文章目录-CSDN博客

我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。

这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。


        专题:共享内存、数据结构与算法_初级代码游戏的博客-CSDN博客

        链表也是一种常用的数据结构,由于是共享内存,因此不会是单独一个一个的链表,而是所有链表放在同一个共享内存块里面。

一、介绍

        头文件:shmMultiList.h

        模板名:T_MULTI_LIST

        链表的接口比较特别:

        从这个类开始我把管理接口用一个独立函数引出来,免得太混乱:IShmActiveObject* getIShmActiveObject()。

        iterator默认isEnd,通过++找到同一个链表的下一个节点。

        删除有两个,从这个入口开始删掉整个链表或只是删掉其中一个节点。 

        iterator只能向前。

二、演示代码

#include "shmMultiList.h"
class CTest_T_MULTI_LIST
{
public:
	struct DemoData : public CActiveObjectBase
	{
		typedef DemoData T_ME;
		long a = 0;
		long b = 0;
		long c = 0;
		sstring<8> s;

		//用于需要排序的场合
		bool operator < (T_ME const& tmp)const
		{
			return a == tmp.a ? (b == tmp.b ? c < tmp.c : b < tmp.b) : a < tmp.a;
		}
		//部分比较函数
		static bool Less_A(T_ME const& tmp1, T_ME const& tmp2)
		{
			return tmp1.a < tmp2.a;
		}
		//某些场合也需要等于
		bool operator == (T_ME const& tmp)const { return !(*this < tmp) && !(tmp < *this); }

		friend ostream& operator << (ostream& o, T_ME const& d)
		{
			return o << d.a << " " << d.b << " " << d.c << " " << d.s.c_str();
		}
		//关键字的hash值,用于分块场合,应保证hash值的最后一部分仍然是平均分布的
		long keyhash()const { return a; }

		//用于输出数据的场合
		string& toString(string& str)const
		{
			char buf[2048];
			sprintf(buf, "%ld %ld %ld %s", a, b, c, s.c_str());
			return str = buf;
		}
		//用于表格输出
		static bool AddTableColumns(CHtmlDoc::CHtmlTable2& table)
		{
			table.AddCol("A", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			table.AddCol("B", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			table.AddCol("C", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			table.AddCol("S", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			return true;
		}
		bool AddTableData(CHtmlDoc::CHtmlTable2& table)const
		{
			table.AddData(a);
			table.AddData(b);
			table.AddData(c);
			table.AddData(s.c_str());
			return true;
		}
	};
	typedef T_MULTI_LIST<DemoData, PI_TEST_1 > T_CONTINER;
	static int test_T_MULTI_LIST(int argc, char** argv)
	{
		T_CONTINER a("test", 1);
		a.getIShmActiveObject()->DestoryShm();
		if (!a.getIShmActiveObject()->CreateShm())return __LINE__;
		thelog << endi;
		if (!a.getIShmActiveObject()->Attach(false))return __LINE__;
		thelog << endi;

		string str;

		vector<T_CONTINER::iterator > v_heads;
		for (int i = 0; i < 2; ++i)
		{
			T_CONTINER::iterator head;
			DemoData tmp;
			tmp.a = i;
			
			if (!a.AddHead(head, tmp))return __LINE__;
			thelog << "添加成功 " << i << " : " << tmp.toString(str) << endi;
			v_heads.push_back(head);

			for (int j = 10; j < 12; ++j)
			{
				DemoData tmp2;
				tmp2.a = i;
				tmp2.b = j;

				if (!a.AddTail(head, tmp2))return __LINE__;
				thelog << "添加成功 " << i << " " << j << " : " << tmp2.toString(str) << endi;
			}
		}
		for (vector<T_CONTINER::iterator >::const_iterator it = v_heads.begin(); it != v_heads.end(); ++it)
		{
			for(auto x=*it;!x.isEnd();++x)
			{
				thelog << x.handle << " : " << x->toString(str) << endi;
			}
		}

		a.getIShmActiveObject()->RunCmdUI();

		return 0;
	}
};

        代码演示了添加和遍历。对于第一个节点,添加在头还是尾是没有区别的。

三、运行结果

        rebuild.sh编译,run.sh运行,命令25(初次运行需要先创建主共享内存,命令0-1):

----------------------------------------
命令表:(q=exit)
0 管理
1 test_BinaryPool
3 test_CMultiProcessServer
4 test_CMultiProcessServer_view
5 test_CMultiProcessServer_speed
6 test_CMultiProcessServer_direct
7 test_RebuildSet
12 UDP测试客户端
13 UDP测试服务端
14 CStressTesting
15 CTestCSimpleMultiProcess_mutex atomic
16 CTest_hash
17 T_SHM_HASH
20 T_ARRAY
21 T_SHMSET_NO_MUTEX
22 T_SHMSET(带互斥)
23 T_SHMSET_lower_bound(带互斥)
24 StringPool2x
25 T_MULTI_LIST
88 test_CMyRWMutex
89 test_T_SHM_SET_GROUP
90 test_shm_IActiveObject
91 test_ShmMultiMap
99 test_ParseFromXml
........................................
----------------------------------------
请选择命令:(q=exit default=25):

[09-03 15:16:51][应用][信息] 用户输入的是:25
[09-03 15:16:51][应用][信息][shmArray.h              : 960(AttachToShm)][  0.00]test 连接共享内存成功 PI_N 1 PART 0 shmid = 98305 p 0x7f1e02d76000
[09-03 15:16:51][应用][信息][shmEnv.cpp              : 624(GetShmConfig)][  0.00]未配置 SHM_CONFIG default.test
[09-03 15:16:51][应用][出错][shmEnv.h                : 304(GetRegFromDb)][  0.00]共享内存 default test 0 不存在,可能的原因:主机重启或手工删除
[09-03 15:16:51][应用][信息][shmArray.h              :1024(_DestoryShm)][  0.00]test 0 连接到共享内存失败
[09-03 15:16:51][应用][信息][shmEnv.cpp              : 624(GetShmConfig)][  0.00]未配置 SHM_CONFIG default.test
[09-03 15:16:51][应用][信息][shmArray.h              :1074(_CreateShm)][  0.00]default test 0 未配置的最大记录数
[09-03 15:16:51][应用][信息][shmArray.h              :1076(_CreateShm)][  0.00]使用默认值 1024
[09-03 15:16:51][应用][信息][shmArray.h              : 395(_CreateShmIfNeed)][  0.00]创建新共享内存成功,id = 131073
[09-03 15:16:51][应用][信息][shmArray.h              :1129(_CreateShm)][  0.00]基础共享内存创建完成
[09-03 15:16:51][应用][信息][shmfctest2.h            : 441(test_T_MULTI_LIST)][  0.00]
[09-03 15:16:51][应用][信息][shmArray.h              : 960(AttachToShm)][  0.00]test 连接共享内存成功 PI_N 1 PART 0 shmid = 131073 p 0x7f1e02d76000
[09-03 15:16:51][应用][信息][shmfctest2.h            : 443(test_T_MULTI_LIST)][  0.00]
[09-03 15:16:51][应用][信息][shmfctest2.h            : 455(test_T_MULTI_LIST)][  0.00]添加成功 0 : 0 0 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 465(test_T_MULTI_LIST)][  0.00]添加成功 0 10 : 0 10 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 465(test_T_MULTI_LIST)][  0.00]添加成功 0 11 : 0 11 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 455(test_T_MULTI_LIST)][  0.00]添加成功 1 : 1 0 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 465(test_T_MULTI_LIST)][  0.00]添加成功 1 10 : 1 10 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 465(test_T_MULTI_LIST)][  0.00]添加成功 1 11 : 1 11 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 472(test_T_MULTI_LIST)][  0.00]0 : 0 0 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 472(test_T_MULTI_LIST)][  0.00]1 : 0 10 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 472(test_T_MULTI_LIST)][  0.00]2 : 0 11 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 472(test_T_MULTI_LIST)][  0.00]3 : 1 0 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 472(test_T_MULTI_LIST)][  0.00]4 : 1 10 0
[09-03 15:16:51][应用][信息][shmfctest2.h            : 472(test_T_MULTI_LIST)][  0.00]5 : 1 11 0
[09-03 15:16:51][应用][信息][shmIActiveObject.h      : 457(RunCmdUI)][  0.00]
test

b:返回上一层
1:创建 2:连接(只读) 3:连接(可写) 4:断开 5:禁用互斥 6:清除数据 7:创建私有 8:删除共享内存 9:显示 10:数据
11:从数据库加载 12:保存到数据库
21:从目录加载 22:保存到目录 23:导出为文本文件
31:从目录加载到私有内存 32:销毁私有内存
97:repair 98:check 99:ToDo 100:shell(q=exit ):


(这里是文档结束)

  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值