当前位置: 首页 > news >正文

22、基于共享内存的数据结构——用十个块来提高并发性

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

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

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


        为了提高并发性,把每个结构改成十个同样的结构是一个可行的办法。

目录

一、set-list的分块版本

二、更基础的set的分块版本


        当然从技术角度,我们程序员一般不应该谈论“十个”,程序员眼里应该只有“一和多”。

        不过由于写成固定个数比较简单而且已经达到较好性能,所以就沿用下来。

一、set-list的分块版本

        这是基于上一篇介绍的set-list结构

	//分为N个子树的SET,非标准接口template <typename T_DATA, int PI_N, typename T_DATA_LIST, int PI_N2 >class T_SHM_LIST_SET_GROUP : public CShmActiveObjects{private:vector<IListSet<T_DATA, T_DATA_LIST > *> vISet;T_SHM_LIST_SET<T_DATA,PI_N  ,T_DATA_LIST,PI_N2,1> m_shmset;T_SHM_LIST_SET<T_DATA,PI_N+1,T_DATA_LIST,PI_N2+1,2> m_shmset1;T_SHM_LIST_SET<T_DATA,PI_N+2,T_DATA_LIST,PI_N2+2,3> m_shmset2;T_SHM_LIST_SET<T_DATA,PI_N+3,T_DATA_LIST,PI_N2+3,4> m_shmset3;T_SHM_LIST_SET<T_DATA,PI_N+4,T_DATA_LIST,PI_N2+4,5> m_shmset4;T_SHM_LIST_SET<T_DATA,PI_N+5,T_DATA_LIST,PI_N2+5,6> m_shmset5;T_SHM_LIST_SET<T_DATA,PI_N+6,T_DATA_LIST,PI_N2+6,7> m_shmset6;T_SHM_LIST_SET<T_DATA,PI_N+7,T_DATA_LIST,PI_N2+7,8> m_shmset7;T_SHM_LIST_SET<T_DATA,PI_N+8,T_DATA_LIST,PI_N2+8,9> m_shmset8;T_SHM_LIST_SET<T_DATA,PI_N+9,T_DATA_LIST,PI_N2+9,10> m_shmset9;enum {N_SUBTREE=10};long KeyHashToIndex(long kh)const{//thelog<<kh<<" "<<kh%N_SUBTREE<<endi;if(kh>=0)return kh%N_SUBTREE;else return (-kh)%N_SUBTREE;;}IListSet<T_DATA, T_DATA_LIST > * GetISet(long n)const{//thelog<<"GetISet "<<n<<endi;return vISet[n];}public:struct iterator{long set_index;T_SHM_SIZE handle;iterator():set_index(-1),handle(-1){}bool operator == (iterator const & tmp)const{return set_index==tmp.set_index && handle==tmp.handle;}bool operator != (iterator const & tmp)const{return !(*this==tmp);}};typedef iterator const_iterator;struct sub_iterator{long set_index;T_SHM_SIZE handle;sub_iterator():set_index(-1),handle(-1){}bool operator == (iterator const & tmp)const{return set_index==tmp.set_index && handle==tmp.handle;}bool operator != (iterator const & tmp)const{return !(*this==tmp);}};typedef sub_iterator const_sub_iterator;private:iterator _find(T_DATA const & value)const{iterator it;it.set_index=KeyHashToIndex(value.keyhash());it.handle=GetISet(it.set_index)->isetFind(value);if(it.handle<0)new(&it) iterator;return it;}pair<iterator, bool> _insert(T_DATA const & value){pair<iterator, bool> ret;pair<long,bool> tmp;ret.first.set_index=KeyHashToIndex(value.keyhash());tmp=GetISet(ret.first.set_index)->isetInsert(value);ret.first.handle=tmp.first;ret.second=tmp.second;if(ret.first.handle<0)new(&ret.first) iterator;return ret;}bool _get(T_DATA & value)const{iterator it=_find(value);if(it!=end()){value=GetByHandle(it);		return true;}else{return false;}}bool _getSub(iterator const & it,T_DATA_LIST & sub)const{if (it != end()){IListSet<T_DATA, T_DATA_LIST > * pSet = GetISet(it.set_index);T_DATA_LIST * p= pSet->ilistsetSubGet(it.handle,sub);if(NULL==p){return false;}else{sub=*p;return true;}}else{return false;}}bool _getSubAll(iterator const & it,vector<T_DATA_LIST > & vsub)const{if (it != end()){IListSet<T_DATA, T_DATA_LIST > * pSet = GetISet(it.set_index);return pSet->ilistsetSubGetAll(it.handle,vsub);}else{return false;}}bool _getSubSetAll(iterator const & it,set<T_DATA_LIST > & ssub)const{if (it != end()){IListSet<T_DATA, T_DATA_LIST > * pSet = GetISet(it.set_index);return pSet->ilistsetSubGetSetAll(it.handle,ssub);}else{return false;}}bool _update(T_DATA const & value){iterator it=_find(value);if(it!=end()){GetByHandle(it)=value;return true;}else{pair<iterator, bool> tmp=_insert(value);return tmp.first!=end();}}bool _updateSub(iterator const & it,T_DATA_LIST const & sub){if (it != end()){IListSet<T_DATA, T_DATA_LIST > * pSet = GetISet(it.set_index);T_DATA_LIST * p= pSet->ilistsetSubGet(it.handle,sub);if(NULL==p){pSet->ilistsetSubAdd(it.handle,sub);return true;}else{*p=sub;return true;}}else{return false;}}bool _addSub(iterator const & it,T_DATA_LIST const & sub){if (it != end()){IListSet<T_DATA, T_DATA_LIST > * pSet = GetISet(it.set_index);T_DATA_LIST * p= pSet->ilistsetSubGet(it.handle,sub);if(NULL==p){pSet->ilistsetSubAdd(it.handle,sub);return true;}else{*p=*p+sub;return true;}}else{return false;}}//bool _erase(T_DATA const & value)//{//	iterator it=_find(value);//	if(it!=end())//	{//		GetISet(it.set_index)->isetErase(it.handle);//		return true;//	}//	else//	{//		return false;//	}//}bool _lsgClear(typename IListSet<T_DATA, T_DATA_LIST >::IlsgClear * fun){typename vector<IListSet<T_DATA, T_DATA_LIST > *>::iterator it;long count_main = 0;long count_second = 0;thelog << "根据规则清理......" << endi;for (it = vISet.begin(); it != vISet.end(); ++it){if (!(*it)->ilistsetClear(fun, count_main, count_second))return false;}thelog << "清理完成 清理主数据 " << count_main << " 个 子数据 " << count_second << " 个" << endi;return true;}//从给定位置开始清理一段数据template<typename T_FUN_B1>bool _Clear(T_FUN_B1 fun,iterator itBegin){iterator it,old_it;long count=0;thelog<<"根据规则清理......"<<endi;it=itBegin;while(it!=end()){old_it=it;MoveNext(&it);if(fun(GetByHandle(old_it))){GetISet(old_it.set_index)->isetErase(old_it.handle);++count;}else{break; //第一个不符合的跳出}if(0==count%100000)thelog<<"已清理记录:"<<count<<"条"<<endi;}thelog<<"清理完成,清理["<<count<<"]个。"<<endi;return true;}bool _ForEach(typename IListSet<T_DATA,T_DATA_LIST >::IListSetForEach * fun){typename vector<IListSet<T_DATA, T_DATA_LIST > *>::iterator it;for(it=vISet.begin();it!=vISet.end();++it){if(!(*it)->ilistsetForEach(fun))return false;}return true;}bool _ForEachSub(iterator const & it,typename IListSet<T_DATA,T_DATA_LIST >::IListSetForEachSub * fun){if (it != end()){IListSet<T_DATA, T_DATA_LIST > * pSet = GetISet(it.set_index);return pSet->ilistsetForEachSub(it.handle,fun);}else{return false;}}bool _ForEachShuffle(long iSet, long handle, typename IListSet<T_DATA,T_DATA_LIST >::IListSetForEachShuffle * fun){typename vector<IListSet<T_DATA, T_DATA_LIST > *>::iterator it;if (iSet >= 0 && static_cast<size_t>(iSet) < vISet.size()){thelog << "续传模式 " << iSet << " " << handle << endi;it = vISet.begin() + iSet;}else{thelog << "全量模式 " << iSet << " " << handle << endi;it = vISet.begin();}for(;it!=vISet.end();++it){fun->iSet = it - vISet.begin();//设置iSetif (iSet == fun->iSet){if (!(*it)->ilistsetForEachShuffle(handle, fun))return false;}else{if (!(*it)->ilistsetForEachShuffle(-1, fun))return false;}}return true;}public:virtual char const * GetName()const{return m_shmset.GetName();}virtual bool ReportData()const{//m_shmset.Report();//m_shmset1.Report();//m_shmset2.Report();//m_shmset3.Report();//m_shmset4.Report();//m_shmset5.Report();//m_shmset6.Report();//m_shmset7.Report();//m_shmset8.Report();//m_shmset9.Report();CHtmlDoc::CHtmlTable2 table;ReportHtmlTable(table, false, 0, 20);thelog << endl << table.MakeTextTable() << endi;return true;}virtual bool ExportTextToDir(char const * dir_name)const{string file = dir_name;if (file.size()>0 && file[file.size() - 1] != '/')file += "/";file += this->GetName();file += ".txt";ofstream f;f.open(file.c_str());if (!f.good()){thelog << "打开文件失败 " << file << ende;return false;}string str;long count = 0;thelog << "开始导出..." << endi;for (const_iterator it = begin(); it != end(); MoveNext(&it)){vector<T_DATA_LIST > subs;getSubAll(it, subs);if (0 == subs.size()){}else{for (typename vector<T_DATA_LIST >::iterator sub_it = subs.begin(); sub_it != subs.end(); ++sub_it){++count;GetByHandle(it).toString(str);str += " ";f.write(str.c_str(), str.size());(*sub_it).toString(str);str += "\n";f.write(str.c_str(), str.size());if (0 == count % 100000)thelog << "导出 " << count << " 条" << endi;}}}thelog << "导出完成,共 " << count << " 条" << endi;f.close();return true;}T_SHM_LIST_SET_GROUP(char const * name,char const * name2,int version):m_shmset(name,name2,version),m_shmset1(name,name2,version),m_shmset2(name,name2,version),m_shmset3(name,name2,version),m_shmset4(name,name2,version),m_shmset5(name,name2,version),m_shmset6(name,name2,version),m_shmset7(name,name2,version),m_shmset8(name,name2,version),m_shmset9(name,name2,version){vISet.reserve(N_SUBTREE);vISet.push_back(&m_shmset);vISet.push_back(&m_shmset1);vISet.push_back(&m_shmset2);vISet.push_back(&m_shmset3);vISet.push_back(&m_shmset4);vISet.push_back(&m_shmset5);vISet.push_back(&m_shmset6);vISet.push_back(&m_shmset7);vISet.push_back(&m_shmset8);vISet.push_back(&m_shmset9);if(vISet.size()!=N_SUBTREE)throw "内存不足";if(!AddTable(&m_shmset))throw "内存不足";if(!AddTable(&m_shmset1))throw "内存不足";if(!AddTable(&m_shmset2))throw "内存不足";if(!AddTable(&m_shmset3))throw "内存不足";if(!AddTable(&m_shmset4))throw "内存不足";if(!AddTable(&m_shmset5))throw "内存不足";if(!AddTable(&m_shmset6))throw "内存不足";if(!AddTable(&m_shmset7))throw "内存不足";if(!AddTable(&m_shmset8))throw "内存不足";if(!AddTable(&m_shmset9))throw "内存不足";}//根据配置的大小创建共享内存,仅供管理员使用bool adminRatableCreateShm(){return CreateShm();}bool adminCreateShm(){return CreateShm();}//清理全部数据bool Destroy(){return clear();}//连接和断开,管理员和一般使用者使用bool init(bool isReadOnly){if(!Attach(isReadOnly)){thelog<<"连接到共享内存失败"<<ende;return false;}return true;}bool uninit(){return Detach();}//遍历iterator begin()const{iterator it;it.set_index=0;it.handle=GetISet(it.set_index)->isetBegin();if(it.handle<0)MoveNext(&it);return it;}iterator end()const{iterator it;new(&it) iterator;return it;}T_DATA & GetByHandle(iterator const & it)const{return *GetISet(it.set_index)->isetGet(it.handle);}iterator & MoveNext(iterator * p)const{while(p->set_index<N_SUBTREE){			//thelog<<"当前位置:"<<p->set_index<<" "<<p->handle<<endi;//先++if(p->handle>=0){GetISet(p->set_index)->isetMoveNext(p->handle);//thelog<<"++后位置:"<<p->set_index<<" "<<p->handle<<endi;}//到了endif(p->handle<0){//thelog<<"子树结尾,指向下一个子树数begin"<<endi;++(p->set_index);//thelog<<"新子树位置:"<<p->set_index<<" "<<p->handle<<endi;if(p->set_index<N_SUBTREE){p->handle=GetISet(p->set_index)->isetBegin();//thelog<<"新子树begin位置:"<<p->set_index<<" "<<p->handle<<endi;if(p->handle>=0){break;}}else{//thelog<<"到达所有的结尾"<<endi;}}else{break;}}//仍然是endif(p->handle<0)new(p) iterator;return *p;}//报告内容,调试用string & Report(string & str)const{string tmp;str="";char buf[2048];sprintf(buf,"\n总容量 %ld 总大小 %ld (%ld%%)",capacity(),size(),size()*100/(0==capacity()?1:capacity()));str+=buf;iterator it;long count;for (it = begin(), count = 0; it != end(); MoveNext(&it), ++count){if(count>100){str+="\n......";break;}sprintf(buf, "\n%ld ", it.set_index);str += buf;str += GetByHandle(it).toString(tmp);}return str;}//输出数据到表格,formEnd倒序,start_pos起始位置,倒序时最后一个为0,返回输出的行数long ReportHtmlTable(CHtmlDoc::CHtmlTable2 & table,bool fromEnd,long start_pos,long max_count)const{table.Clear();table.AddCol("PART",CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);table.AddCol("i",CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);T_DATA::AddTableColumns(table);T_DATA_LIST::AddTableColumns(table);const_iterator it;long i;long count=0;if(fromEnd){table.AddLine();table.AddData("不支持倒序显示");return 0;}for (i = 0, it = begin(); it != end() && count < max_count; ++i, MoveNext(&it)){if(i<start_pos)continue;if(count>=max_count)break;vector<T_DATA_LIST > subs;getSubAll(it,subs);if(0==subs.size()){++count;table.AddLine();table.AddData(it.set_index);table.AddData(i);GetByHandle(it).AddTableData(table);}else{for (typename vector<T_DATA_LIST >::iterator sub_it = subs.begin(); sub_it != subs.end(); ++sub_it){++count;table.AddLine();table.AddData(it.set_index);table.AddData(i);GetByHandle(it).AddTableData(table);sub_it->AddTableData(table);}}}return count;}//直接存取bool get(T_DATA * value)const{return get(*value);}bool get(T_DATA & value)const{return _get(value);}bool getSub(iterator const & it,T_DATA_LIST & sub)const{return _getSub(it,sub);}bool getSubAll(iterator const & it,vector<T_DATA_LIST > & vsub)const{return _getSubAll(it,vsub);}bool getSubSetAll(iterator const & it,set<T_DATA_LIST > & ssub)const{return _getSubSetAll(it,ssub);}//直接查找iterator find(T_DATA const & value) const{return _find(value);}bool update(T_DATA const & value){return _update(value);}bool updateSub(iterator const & it,T_DATA_LIST const & sub){return _updateSub(it,sub);}bool addSub(iterator const & it,T_DATA_LIST const & sub){return _addSub(it,sub);}//bool erase(T_DATA const & value)//{//	return _erase(value);//}pair<iterator, bool> insert(T_DATA const & value){return _insert(value);}bool lsgClear(typename IListSet<T_DATA, T_DATA_LIST >::IlsgClear * fun){return _lsgClear(fun);}template<typename T_FUN_B1>bool Clear(T_FUN_B1 fun,iterator itBegin){return _Clear(fun,itBegin);}bool ForEach(typename IListSet<T_DATA,T_DATA_LIST >::IListSetForEach * fun){return _ForEach(fun);}bool ForEachSub(iterator const & it,typename IListSet<T_DATA,T_DATA_LIST >::IListSetForEachSub * fun){return _ForEachSub(it,fun);}typedef typename IListSet<T_DATA,T_DATA_LIST >::IListSetForEachShuffle T_FOR_EACH_SHUFFLE;bool ForEachShuffle(long iSet, long handle, T_FOR_EACH_SHUFFLE * fun){return _ForEachShuffle(iSet, handle, fun);}//编译测试void test(){m_shmset.test();}};

二、更基础的set的分块版本

        这是set结构的分块版本:

	//分为N个子树的SET,非标准接口template<typename T_DATA,int PI_N >class T_SHM_SET_GROUP : public CShmActiveObjects{private:vector<ISet<T_DATA > *> vISet;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N  ,CDemoData,1> m_shmset;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+1,CDemoData,2> m_shmset1;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+2,CDemoData,3> m_shmset2;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+3,CDemoData,4> m_shmset3;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+4,CDemoData,5> m_shmset4;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+5,CDemoData,6> m_shmset5;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+6,CDemoData,7> m_shmset6;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+7,CDemoData,8> m_shmset7;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+8,CDemoData,9> m_shmset8;T_SHMSET_NO_MUTEX_ISET<T_DATA,PI_N+9,CDemoData,10> m_shmset9;enum {N_SUBTREE=10};long KeyHashToIndex(long kh)const{//thelog<<kh<<" "<<kh%N_SUBTREE<<endi;if(kh>=0)return kh%N_SUBTREE;else return (-kh)%N_SUBTREE;;}ISet<T_DATA > * GetISet(long n)const{//thelog<<"GetISet "<<n<<endi;return vISet[n];}public:struct iterator{long set_index;T_SHM_SIZE handle;iterator():set_index(-1),handle(-1){}bool operator == (iterator const & tmp)const{return set_index==tmp.set_index && handle==tmp.handle;}bool operator != (iterator const & tmp)const{return !(*this==tmp);}};typedef iterator const_iterator;private:iterator _find(T_DATA const & value)const{iterator it;it.set_index=KeyHashToIndex(value.keyhash());it.handle=GetISet(it.set_index)->isetFind(value);if(it.handle<0)new(&it) iterator;return it;}iterator _find_lower_bound(T_DATA const & value, bool(*less)(T_DATA const &, T_DATA const &))const{iterator it;it.set_index=KeyHashToIndex(value.keyhash());it.handle=GetISet(it.set_index)->isetFindLowerBound(value,less);if(it.handle<0)new(&it) iterator;return it;}pair<iterator, bool> _insert(T_DATA const & value){pair<iterator, bool> ret;pair<long,bool> tmp;ret.first.set_index=KeyHashToIndex(value.keyhash());tmp=GetISet(ret.first.set_index)->isetInsert(value);ret.first.handle=tmp.first;ret.second=tmp.second;if(ret.first.handle<0)new(&ret.first) iterator;return ret;}bool _get(T_DATA & value)const{iterator it=_find(value);if(it!=end()){value=GetByHandle(it);		return true;}else{return false;}}bool _update(T_DATA const & value){iterator it=_find(value);if(it!=end()){GetByHandle(it)=value;return true;}else{pair<iterator, bool> tmp=_insert(value);return tmp.first!=end();}}bool _erase(T_DATA const & value){iterator it=_find(value);if(it!=end()){GetISet(it.set_index)->isetErase(it.handle);return true;}else{return false;}}template<typename T_FUN_B1>bool _Clear(T_FUN_B1 fun){iterator it,old_it;long count_all=0;long count=0;thelog<<"根据规则清理......"<<endi;it=begin();while(it!=end()){old_it=it;MoveNext(&it);if(fun(GetByHandle(old_it))){GetISet(old_it.set_index)->isetErase(old_it.handle);++count;}++count_all;if(0==count_all%10000)thelog<<count_all<<" "<<count<<endi;}thelog<<"清理完成,清理["<<count<<"]个。"<<endi;return true;}//从给定位置开始清理一段数据template<typename T_FUN_B1>bool _Clear(T_FUN_B1 fun,iterator itBegin){iterator it,old_it;long count=0;thelog<<"根据规则清理......"<<endi;it=itBegin;while(it!=end()){old_it=it;MoveNext(&it);if(fun(GetByHandle(old_it))){GetISet(old_it.set_index)->isetErase(old_it.handle);++count;}else{break; //第一个不符合的跳出}if(0==count%100000)thelog<<"已清理记录:"<<count<<"条"<<endi;}thelog<<"清理完成,清理["<<count<<"]个。"<<endi;return true;}template<typename T_FUN_B1>bool _ForEach(T_FUN_B1 fun){iterator it;long count_all=0;long count=0;thelog << "开始处理。。。" << endi;for(it=begin();it!=end();MoveNext(&it)){if(fun(&GetByHandle(it))){++count;}++count_all;if(0==count_all%10000)thelog<<count_all<<" "<<count<<endi;}thelog << "处理完成,共[" << count_all << "]个,处理[" << count << "]个。" << endi;return true;}bool _ForEachShuffle(long iSet, long handle, typename ISet<T_DATA >::ISetForEach * fun){typename vector<ISet<T_DATA > *>::iterator it;if (iSet >= 0 && static_cast<size_t>(iSet) < vISet.size()){thelog << "续传模式 " << iSet << " " << handle << endi;it = vISet.begin() + iSet;}else{thelog << "全量模式 " << iSet << " " << handle << endi;it = vISet.begin();}for (; it != vISet.end(); ++it){fun->iSet = it - vISet.begin();if (iSet == fun->iSet){if (!(*it)->isetForEachShuffle(handle, fun))return false;}else{if (!(*it)->isetForEachShuffle(-1, fun))return false;}}return true;}public:virtual char const * GetName()const{return m_shmset.GetName();}virtual bool ReportData()const{CHtmlDoc::CHtmlTable2 table;ReportHtmlTable(table, false, 0, 20);thelog << endl << table.MakeTextTable() << endi;return true;}T_SHM_SET_GROUP(char const * name,int version):m_shmset(name,version),m_shmset1(name,version),m_shmset2(name,version),m_shmset3(name,version),m_shmset4(name,version),m_shmset5(name,version),m_shmset6(name,version),m_shmset7(name,version),m_shmset8(name,version),m_shmset9(name,version){vISet.reserve(N_SUBTREE);vISet.push_back(&m_shmset);vISet.push_back(&m_shmset1);vISet.push_back(&m_shmset2);vISet.push_back(&m_shmset3);vISet.push_back(&m_shmset4);vISet.push_back(&m_shmset5);vISet.push_back(&m_shmset6);vISet.push_back(&m_shmset7);vISet.push_back(&m_shmset8);vISet.push_back(&m_shmset9);if(vISet.size()!=N_SUBTREE)throw "内存不足";if(!AddTable(&m_shmset))throw "内存不足";if(!AddTable(&m_shmset1))throw "内存不足";if(!AddTable(&m_shmset2))throw "内存不足";if(!AddTable(&m_shmset3))throw "内存不足";if(!AddTable(&m_shmset4))throw "内存不足";if(!AddTable(&m_shmset5))throw "内存不足";if(!AddTable(&m_shmset6))throw "内存不足";if(!AddTable(&m_shmset7))throw "内存不足";if(!AddTable(&m_shmset8))throw "内存不足";if(!AddTable(&m_shmset9))throw "内存不足";}//根据配置的大小创建共享内存,仅供管理员使用bool adminRatableCreateShm(){return CreateShm();}bool adminCreateShm(){return CreateShm();}//清理全部数据bool Destroy(){return clear();}//连接和断开,管理员和一般使用者使用bool init(bool isReadOnly){if(!Attach(isReadOnly)){thelog<<"连接到共享内存失败"<<ende;return false;}return true;}bool uninit(){return Detach();}//遍历iterator begin()const{iterator it;it.set_index=0;it.handle=GetISet(it.set_index)->isetBegin();if(it.handle<0)MoveNext(&it);return it;}iterator end()const{iterator it;new(&it) iterator;return it;}T_DATA & GetByHandle(iterator const & it)const{return *GetISet(it.set_index)->isetGet(it.handle);}iterator & MoveNext(iterator * p)const{while(p->set_index<N_SUBTREE){			//thelog<<"当前位置:"<<p->set_index<<" "<<p->handle<<endi;//先++if(p->handle>=0){GetISet(p->set_index)->isetMoveNext(p->handle);//thelog<<"++后位置:"<<p->set_index<<" "<<p->handle<<endi;}//到了endif(p->handle<0){//thelog<<"子树结尾,指向下一个子树数begin"<<endi;++(p->set_index);//thelog<<"新子树位置:"<<p->set_index<<" "<<p->handle<<endi;if(p->set_index<N_SUBTREE){p->handle=GetISet(p->set_index)->isetBegin();//thelog<<"新子树begin位置:"<<p->set_index<<" "<<p->handle<<endi;if(p->handle>=0){break;}}else{//thelog<<"到达所有的结尾"<<endi;}}else{break;}}//仍然是endif(p->handle<0)new(p) iterator;return *p;}//报告内容,调试用string & Report(string & str)const{string tmp;str="";//str+=m_shmset.Report(tmp);//str+=m_shmset1.Report(tmp);//str+=m_shmset2.Report(tmp);//str+=m_shmset3.Report(tmp);//str+=m_shmset4.Report(tmp);//str+=m_shmset5.Report(tmp);//str+=m_shmset6.Report(tmp);//str+=m_shmset7.Report(tmp);//str+=m_shmset8.Report(tmp);//str+=m_shmset9.Report(tmp);char buf[2048];sprintf(buf,"\n总容量 %ld 总大小 %ld (%ld%%)",capacity(),size(),size()*100/(0==capacity()?1:capacity()));str+=buf;iterator it;long count;for (it = begin(), count = 0; it != end(); MoveNext(&it), ++count){if(count>100){str+="\n......";break;}sprintf(buf, "\n%ld ", it.set_index);str += buf;str += GetByHandle(it).toString(tmp);}return str;}//输出数据到表格,formEnd倒序,start_pos起始位置,倒序时最后一个为0,返回输出的行数long ReportHtmlTable(CHtmlDoc::CHtmlTable2 & table,bool fromEnd,long start_pos,long max_count)const{table.Clear();table.AddCol("PART",CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);table.AddCol("i",CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);T_DATA::AddTableColumns(table);const_iterator it;long i;long count=0;if(fromEnd){table.AddLine();table.AddData("不支持倒序显示");return 0;}for (i = 0, it = begin(); it != end() && count < max_count; ++i, MoveNext(&it)){if(i<start_pos)continue;if(count>=max_count)break;++count;table.AddLine();table.AddData(it.set_index);table.AddData(i);GetByHandle(it).AddTableData(table);}return count;}//直接存取bool get(T_DATA * value)const{return get(*value);}bool get(T_DATA & value)const{return _get(value);}//直接查找iterator find(T_DATA const & value) const{return _find(value);}iterator lower_bound(T_DATA * value, bool(*less)(T_DATA const &, T_DATA const &))const{return lower_bound(*value,less);}iterator lower_bound(T_DATA & value, bool(*less)(T_DATA const &, T_DATA const &))const{return _find_lower_bound(value,less);}bool update(T_DATA const & value){return _update(value);}bool erase(T_DATA const & value){return _erase(value);}pair<iterator, bool> insert(T_DATA const & value){return _insert(value);}template<typename T_FUN_B1>bool Clear(T_FUN_B1 fun){return _Clear(fun);}template<typename T_FUN_B1>bool Clear(T_FUN_B1 fun,iterator itBegin){return _Clear(fun,itBegin);}template<typename T_FUN_B1>bool ForEach(T_FUN_B1 fun){return _ForEach(fun);}typedef typename ISet<T_DATA >::ISetForEach T_FOR_EACH_SHUFFLE;bool ForEachShuffle(long iSet, long handle, typename ISet<T_DATA >::ISetForEach * fun){return _ForEachShuffle(iSet, handle, fun);}};

(这里是结束)

相关文章:

22、基于共享内存的数据结构——用十个块来提高并发性

初级代码游戏的专栏介绍与文章目录-CSDN博客 我的github&#xff1a;codetoys&#xff0c;所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的&#xff0c;可以在任何平台上使用。 为了提高并发性&#xff0c;把…...

【ffmpeg命令入门】实现画中画

文章目录 前言画中画是什么画中画的外观描述效果展示为什么要用画中画应用场景示例 使用FFmpeg添加画中画示例命令参数解释调整嵌入视频的位置调整嵌入视频的大小处理音频 总结 前言 FFmpeg 是一款强大的多媒体处理工具&#xff0c;广泛用于音视频的录制、转换和流处理。它不仅…...

基于 LangChain+LangGraph 来实现一个翻译项目

相信大家在看文档的时候&#xff0c;有时会比较苦恼&#xff0c;比如 AI 相关的文档都是外文&#xff0c;中文文档比较少&#xff0c;看起来会比较吃力&#xff0c;有的时候会看不懂&#xff0c;翻译软件又翻得很乱&#xff0c;完全看不了&#xff0c;今天就基于 LangChain 和 …...

javascript 如何将 json 格式数组转为 excel 表格| sheetJS

案例 // https://unpkg.com/xlsx0.18.5/dist/xlsx.full.min.js function exportXlsx(jsonData, fileName , mine null) {const workbook XLSX.utils.book_new();// 将JSON数组转换成工作表const worksheet XLSX.utils.json_to_sheet(jsonData);// 向工作簿添加工作表XLSX.…...

网页制作技术在未来会如何影响人们的生活?

网页制作技术在未来会如何影响人们的生活&#xff1f; 李升伟 网页制作技术在未来可能会从以下几个方面显著影响人们的生活&#xff1a; 1. 工作与学习方式的变革&#xff1a;远程办公和在线教育将更加普及和高效。通过精心制作的网页&#xff0c;人们能够实现更便捷的协作…...

【计算机网络】网络层——IPv4地址(个人笔记)

学习日期&#xff1a;2024.7.24 内容摘要&#xff1a;IPv4地址&#xff0c;分类编址&#xff0c;子网&#xff0c;无分类编址 IPv4地址概述 在TCP/IP体系中&#xff0c;IP地址是一个最基本的概念&#xff0c;IPv4地址就是给因特网上的每一台主机的每一个接口分配一个在全世界…...

c++ 学习笔记之多线程:线程锁,条件变量,唤醒指定线程

基于CAS线程加锁方式 CAS&#xff08;Compare-And-Swap&#xff09;和 mutex 都是用于实现线程安全的技术&#xff0c;但它们适用于不同的场景&#xff0c;具有不同的性能和复杂性。下面是对两者的区别和使用场景的详细解释&#xff1a; CAS&#xff08;Compare-And-Swap&…...

《0基础》学习Python——第二十三讲__网络爬虫/<6>爬取哔哩哔哩视频

一、在B站上爬取一段视频&#xff08;B站视频有音频和视频两个部分&#xff09; 1、获取URL 注意&#xff1a;很多平台都有反爬取的机制&#xff0c;B站也不例外 首先按下F12找到第一条复制URL 2、UA伪装&#xff0c;下列图片中&#xff08;注意代码书写格式&#xff09; 3、Co…...

第13周 简历职位功能开发与Zookeeper实战

第13周 简历职位功能开发与Zookeeper实战 本章概述1. Mysql8窗口函数over使用1.1 演示表结构与数据1.2 案例1:获取男女总分数1.3 案例2****************************************************************************************本章概述 1. Mysql8窗口函数over使用 参考案例…...

什么是大型语言模型 (LLM)

本章探讨下&#xff0c;人工智能如何彻底改变我们理解和与语言互动的方式 大型语言模型 (LLM) 代表了人工智能的突破&#xff0c;它采用具有广泛参数的神经网络技术进行高级语言处理。 本文探讨了 LLM 的演变、架构、应用和挑战&#xff0c;重点关注其在自然语言处理 (NLP) 领…...

【人工智能】AI时代:探索个人潜能的新视角

文章目录 &#x1f34a;Al时代的个人发展1 AI的高速发展意味着什么1.1 生产力大幅提升1.2 生产关系的改变1.3 产品范式1.4 产业革命1.5 Al的局限性1.5.1局限一:大模型的幻觉1.5.2 局限二&#xff1a;Token 2 个体如何应对这种改变?2.1 职场人2.2 K12家长2.3 大学生2.4 创业者 …...

pyaudio VAD通过声音音频值分贝大小检测没人说话自动停止录制

效果可能说话声音小可能不被监听到,需要更改QUIET_DB阈值,另外delay_time值是低于阈值多久就可以停止保存当前的语音 import pyaudio import waveimport sys import numpy as npdef record_auto(MIC_INDEX=1):开启麦克风录音,保存至temp/speech_record.wav音频文件音量超过…...

《后端程序猿 · @Value 注释说明》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; CSDN入驻不久&#xff0c;希望大家多多支持&#xff0c;后续会继续提升文章质量&#xff0c;绝不滥竽充数…...

【LeetCode】71.简化路径

1. 题目 2. 分析 3. 代码 我写了一版很复杂的代码&#xff1a; class Solution:def simplifyPath(self, path: str) -> str:operator [] # 操作符的栈dir_name [] # 文件名的栈idx 0cur_dir_name ""while(idx < len(path)):if path[idx] /:operator.ap…...

DockerCompose 安装环境

1. Redis version: 3 services:redis:image: redis:6.2.12container_name: redisports:- "6379:6379"environment:TZ: Asia/Shanghaivolumes:# 本地数据目录要先执行 chmod 777 /usr/local/docker/redis/data 赋予读写权限&#xff0c;否则将无法写入数据- /usr/loc…...

学习笔记之JAVA篇(0724)

p 方法 方法声明格式&#xff1a; [修饰符1 修饰符2 ...] 返回值类型 方法名&#xff08;形式参数列表&#xff09;{ java语句;......; } 方法调用方式 普通方法对象.方法名&#xff08;实参列表&#xff09;静态方法类名.方法名&#xff08;实参列表&#xff09; 方法的详…...

【Android】广播机制

【Android】广播机制 前言 广播机制是Android中一种非常重要的通信机制&#xff0c;用于在应用程序之间或应用程序的不同组件之间传递信息。广播可以是系统广播&#xff0c;也可以是自定义广播。广播机制主要包括标准广播和有序广播两种类型。 简介 在Android中&#xff0c…...

【.NET全栈】ASP.NET开发Web应用——ASP.NET数据绑定技术

文章目录 前言一、绑定技术基础1、单值绑定2、重复值绑定 二、数据源控件1、数据绑定的页面生存周期2、SqlDataSource3、使用参数过滤数据4、更新数据和并发处理5、编程执行SqlDataSource命令6、ObjectDataSource控件介绍7、创建业务对象类8、在ObiectDataSource中使用参数9、使…...

MySQL的账户管理

目录 1 密码策略 1.1 查看数据库当前密码策略&#xff1a; 1.2 查看密码设置策略 1.3 密码强度检查等级解释&#xff08;validate_password.policy&#xff09; 2 新建登录账户 3 账户授权 3.1 赋权原则 3.2 常见的用户权限 3.3 查看权限 3.4 赋权语法 4 实例 4.1 示例1&#x…...

FastGPT 源码调试配置

目录 一、添加 launch.json 文件 二、调试 本文简单介绍如何通过 vscode 对 FastGPT 进行调试。 这里假设已经安装 vsocde 和 FastGPT本地部署。 一、添加 launch.json 文件 vscode 打开 FastGPT 项目,点击 调试 -> 显示所有自动调试配置 -> 添加配置 -> Node.j…...

生成xcframework

打包 XCFramework 的方法 XCFramework 是苹果推出的一种多平台二进制分发格式&#xff0c;可以包含多个架构和平台的代码。打包 XCFramework 通常用于分发库或框架。 使用 Xcode 命令行工具打包 通过 xcodebuild 命令可以打包 XCFramework。确保项目已经配置好需要支持的平台…...

【HarmonyOS 5.0】DevEco Testing:鸿蒙应用质量保障的终极武器

——全方位测试解决方案与代码实战 一、工具定位与核心能力 DevEco Testing是HarmonyOS官方推出的​​一体化测试平台​​&#xff0c;覆盖应用全生命周期测试需求&#xff0c;主要提供五大核心能力&#xff1a; ​​测试类型​​​​检测目标​​​​关键指标​​功能体验基…...

线程与协程

1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指&#xff1a;像函数调用/返回一样轻量地完成任务切换。 举例说明&#xff1a; 当你在程序中写一个函数调用&#xff1a; funcA() 然后 funcA 执行完后返回&…...

C++ 基础特性深度解析

目录 引言 一、命名空间&#xff08;namespace&#xff09; C 中的命名空间​ 与 C 语言的对比​ 二、缺省参数​ C 中的缺省参数​ 与 C 语言的对比​ 三、引用&#xff08;reference&#xff09;​ C 中的引用​ 与 C 语言的对比​ 四、inline&#xff08;内联函数…...

UR 协作机器人「三剑客」:精密轻量担当(UR7e)、全能协作主力(UR12e)、重型任务专家(UR15)

UR协作机器人正以其卓越性能在现代制造业自动化中扮演重要角色。UR7e、UR12e和UR15通过创新技术和精准设计满足了不同行业的多样化需求。其中&#xff0c;UR15以其速度、精度及人工智能准备能力成为自动化领域的重要突破。UR7e和UR12e则在负载规格和市场定位上不断优化&#xf…...

Pinocchio 库详解及其在足式机器人上的应用

Pinocchio 库详解及其在足式机器人上的应用 Pinocchio (Pinocchio is not only a nose) 是一个开源的 C 库&#xff0c;专门用于快速计算机器人模型的正向运动学、逆向运动学、雅可比矩阵、动力学和动力学导数。它主要关注效率和准确性&#xff0c;并提供了一个通用的框架&…...

HashMap中的put方法执行流程(流程图)

1 put操作整体流程 HashMap 的 put 操作是其最核心的功能之一。在 JDK 1.8 及以后版本中&#xff0c;其主要逻辑封装在 putVal 这个内部方法中。整个过程大致如下&#xff1a; 初始判断与哈希计算&#xff1a; 首先&#xff0c;putVal 方法会检查当前的 table&#xff08;也就…...

面向无人机海岸带生态系统监测的语义分割基准数据集

描述&#xff1a;海岸带生态系统的监测是维护生态平衡和可持续发展的重要任务。语义分割技术在遥感影像中的应用为海岸带生态系统的精准监测提供了有效手段。然而&#xff0c;目前该领域仍面临一个挑战&#xff0c;即缺乏公开的专门面向海岸带生态系统的语义分割基准数据集。受…...

GruntJS-前端自动化任务运行器从入门到实战

Grunt 完全指南&#xff1a;从入门到实战 一、Grunt 是什么&#xff1f; Grunt是一个基于 Node.js 的前端自动化任务运行器&#xff0c;主要用于自动化执行项目开发中重复性高的任务&#xff0c;例如文件压缩、代码编译、语法检查、单元测试、文件合并等。通过配置简洁的任务…...

JavaScript 数据类型详解

JavaScript 数据类型详解 JavaScript 数据类型分为 原始类型&#xff08;Primitive&#xff09; 和 对象类型&#xff08;Object&#xff09; 两大类&#xff0c;共 8 种&#xff08;ES11&#xff09;&#xff1a; 一、原始类型&#xff08;7种&#xff09; 1. undefined 定…...