当前位置: 首页 > 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…...

SQL Server数据迁移新纪元:数据库数据泵(Data Pump)使用指南

SQL Server数据迁移新纪元&#xff1a;数据库数据泵&#xff08;Data Pump&#xff09;使用指南 在数据管理的世界里&#xff0c;数据迁移是一个常见且复杂的过程。SQL Server提供了一个强大的工具——数据库数据泵&#xff08;Data Pump&#xff09;&#xff0c;它可以帮助我…...

Android性能优化之OOM

OOM 什么是OOM&#xff1f;为什么会有OOM&#xff1f;APP的内存限制App的内存限制是多少&#xff1f; 为什么Android系统要设定App的内存限制&#xff1f;Android有GC自动回收资源&#xff0c;为什么还会OOM?容易发生OOM的场景及处理方案如何避免OOM&#xff1f; 什么是OOM&am…...

代码随想录算法训练营day7 | 454.四数相加II、383.赎金信、15.三数之和、18.四数之和

文章目录 454.四数相加II思路 383.赎金信思路 15.三数之和思路剪枝去重 18.四数之和思路剪枝去重复习&#xff1a;C中的类型转换方法 总结 今天是哈希表专题的第二天 废话不多说&#xff0c;直接上题目 454.四数相加II 建议&#xff1a;本题是 使用map 巧妙解决的问题&#x…...

Spark实时(三):Structured Streaming入门案例

文章目录 Structured Streaming入门案例 一、Scala代码如下 二、Java 代码如下 三、以上代码注意点如下 Structured Streaming入门案例 我们使用Structured Streaming来监控socket数据统计WordCount。这里我们使用Spark版本为3.4.3版本&#xff0c;首先在Maven pom文件中导…...

《Java初阶数据结构》----4.<线性表---Stack栈和Queue队列>

前言 大家好&#xff0c;我目前在学习java。之前也学了一段时间&#xff0c;但是没有发布博客。时间过的真的很快。我会利用好这个暑假&#xff0c;来复习之前学过的内容&#xff0c;并整理好之前写过的博客进行发布。如果博客中有错误或者没有读懂的地方。热烈欢迎大家在评论区…...

Android SurfaceFlinger——关联EGL三要素(二十七)

通过前面的文章我们得到了 EGL 的三要素——Display、Surface 和 Context。其中,Display 是一个图形显示系统或者硬件屏幕,Surface 代表一个可以被渲染的图像缓冲区,Context 包含了 OpenGL ES 的状态信息和资源,它是执行 OpenGL 命令的环境。下一步就是调用 eglMakeCurrent…...

Unity3D之TCP网络通信(客户端)

文章目录 概述TCP核心类异步机制 Unity中创建TCP客户端Unity中其它脚本获取TCP客户端接受到的数据后续改进 本文将以Unity3D应用项目作为客户端去连接制定的服务器为例进行相关说明。 Unity官网参考资料&#xff1a; https://developer.unity.cn/projects/6572ea1bedbc2a001ef…...

Kotlin 中 标准库函数

在 Kotlin 中&#xff0c;标准库提供了许多实用的函数&#xff0c;这些函数可以帮助简化代码、提高效率&#xff0c;以下是一些常用的标准库函数及其功能&#xff1a; let: let 函数允许你在对象上执行一个操作&#xff0c;并返回结果。它通常与安全调用操作符 ?. 一起使用&a…...

【教学类-69-01】20240721铠甲勇士扑克牌(随机14个数字+字母)涂色(男孩篇)

背景需求&#xff1a; 【教学类-68-01】20240720裙子涂色&#xff08;女孩篇&#xff09;-CSDN博客文章浏览阅读250次。【教学类-68-01】20240720裙子涂色&#xff08;女孩篇&#xff09;https://blog.csdn.net/reasonsummer/article/details/140578153 前期制作了女孩涂色延…...

Adobe“加速”创意人士开启设计新篇章

近日&#xff0c;Adobe公司宣布了其行业领先的专业设计应用程序——Adobe Illustrator和Adobe Photoshop的突破性创新。这一重大更新不仅为创意专业人士带来了前所未有的设计可能性和工作效率提升&#xff0c;还让不论是插画师、设计师还是摄影师&#xff0c;都能从中受益并创作…...

释疑 803-(1)概述 精炼提纯版

目录 习题 1-01计算机网络可以向用户提供哪些服务? 1-02 试简述分组交换的要点。 1-03 试从多个方面比较电路交换、报文交换和分组交换的主要优缺点。 1-05 互联网基础结构的发展大致分为哪几个阶段?请指出这几个阶段最主要的特点。 1-06 简述互联网标准制定的几个阶段…...

人工智能与机器学习原理精解【6】

文章目录 数值优化基础理论凹凸性定义在国外与国内存在不同国内定义国外定义总结示例与说明注意事项 国内凹凸性二阶定义的例子凹函数例子凸函数例子 凸函数&#xff08;convex function&#xff09;的开口方向凸函数的二阶导数凸函数的二阶定义单变量函数的二阶定义多变量函数…...

JDK、JRE、JVM之间的关系

JDK是Java的开发环境&#xff0c;用JDK开发了JAVA程序后&#xff0c;通过JDK中的编译程序&#xff08;javac&#xff09;将java文件编译成字节码文件&#xff0c;作为运行环境的JRE&#xff0c;字节码文件在JRE上运行&#xff0c;作为虚拟机的JVM解析这些字节码&#xff0c;映射…...

redis构建集群时,一直Waiting for the cluster to join

redis构建集群时&#xff0c;一直Waiting for the cluster to join 前置条件参考 前置条件 这是我搭建的集群相关信息&#xff0c;三台虚拟机&#xff0c;分别是一主一从。在将所有虚拟机中redis服务器用到的tcp端口都打开之后&#xff0c;进行构建集群。但是出现上面的情况。 …...

C++之类与对象(2)

前言 今天将步入学习类的默认成员函数&#xff0c;本节讲解其中的构造函数和析构函数。 1.类的默认成员函数 在 C 中&#xff0c;如果一个类没有显式定义某些成员函数&#xff0c;编译器会自动为该类生成默认的成员函数。以下是编译器可能会生成的默认成员函数&#xff1a; 默…...

「树形结构」基于 Antd 实现一个动态增加子节点+可拖拽的树

效果 如图所示 实现 import { createRoot } from react-dom/client; import React, { useState } from react; import { Tree, Input, Button } from antd; import { PlusOutlined } from ant-design/icons;const { TreeNode } Tree; const { Search } Input;const ini…...

ubuntu那些ppa源在哪

Ubuntu中的 PPA 终极指南 - UBUNTU粉丝之家 什么是PPA PPA 代表个人包存档。 PPA 允许应用程序开发人员和 Linux 用户创建自己的存储库来分发软件。 使用 PPA&#xff0c;您可以轻松获取较新的软件版本或官方 Ubuntu 存储库无法提供的软件。 为什么使用PPA&#xff1f; 正如…...

20240724-然后用idea创建一个Java项目/配置maven环境/本地仓储配置

1.创建一个java项目 &#xff08;1&#xff09;点击页面的create project&#xff0c;然后next &#xff08;2&#xff09;不勾选&#xff0c;继续next &#xff08;3&#xff09;选择新项目名称&#xff0c;新项目路径&#xff0c;然后Finsh&#xff0c;在新打开的页面选择…...

PaddleOCR-PP-OCRv4推理详解及部署实现(下)

目录 前言1. 检测模型1.1 预处理1.2 后处理1.3 推理 2. 方向分类器模型2.1 预处理2.2 后处理2.3 推理 3. 识别模型3.1 预处理3.2 后处理3.3 推理 4. PP-OCRv4部署4.1 源码下载4.2 环境配置4.2.1 配置CMakeLists.txt4.2.2 配置Makefile 4.3 ONNX导出4.4 engine生成4.4.1 检测模型…...

【Golang 面试基础题】每日 5 题(二)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/UWz06 &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 Golang 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏…...

状态模式与订单状态机的实现

状态模式 状态模式&#xff08;State Design Pattern&#xff09;是一种行为设计模式&#xff0c;用于在对象的内部状态改变时改变其行为。这种模式可以将状态的变化封装在状态对象中&#xff0c;使得对象在状态变化时不会影响到其他代码&#xff0c;提升了代码的灵活性和可维…...

【MSP430】MSP430是什么?与STM32对比哪个性能更佳?

一、MSP430是什么&#xff1f; MSP430F5529LP是一款由德州仪器&#xff08;TI&#xff09;推出的16位微控制器单元&#xff08;MCU&#xff09;开发板&#xff0c;具有USB功能&#xff0c;内存配置为128KB闪存和8KB RAM&#xff0c;工作频率高达25MHz。 这款MCU以其高性能和多…...

Win11 操作(四)g502鼠标连接电脑不亮灯无反应

罗技鼠标连接电脑不亮灯无反应 前言 罗技技术&#x1f4a9;中&#x1f4a9;&#xff0c;贴吧技术神中神&#xff01; 最近买了一个g502&#xff0c;结果买回来直接插上电脑连灯都不亮&#xff0c;问了一下客服。客服简单的让我换接口&#xff0c;又是下载ghub之类的&#xf…...

自定义QDialog使用详解

自定义QDialog使用详解 一、创建 QDialog 对象二、QDialog设置布局三、QDialog控制模态行为3.1 模态和非模态区别3.2 QDialog的模态使用四、使用 QDialogButtonBox五、处理对话框的结果六、使用 QDialog 的信号和槽QDialog是Qt框架中用于创建对话框窗口的基本类。对话框窗口通常…...

Pytorch使用教学2-Tensor的维度

在PyTorch使用的过程中&#xff0c;维度转换一定少不了。而PyTorch中有多种维度形变的方法&#xff0c;我们该在什么场景下使用什么方法呢&#xff1f; 本小节我们使用的张量如下&#xff1a; # 一维向量 t1 torch.tensor((1, 2)) # 二维向量 t2 torch.tensor([[1, 2, 3], …...

Interesting bug caused by getattr

题意&#xff1a;由 getattr 引起的有趣的 bug 问题背景&#xff1a; I try to train 8 CNN models with the same structures simultaneously. After training a model on a batch, I need to synchronize the weights of the feature extraction layers in other 7 models. …...

获取后端返回的图形验证码

如果后端返回的直接就是一个图形&#xff0c;有以下几种方式展示 一、直接在img标签里面的src里面调用接口 <img :src"dialogSrc" class"photo" alt"验证码图片" click"changeDialog">let orgUrl "/api/captcha" …...

奇怪的Excel单元格字体颜色格式

使用VBA代码修改单元格全部字符字体颜色是个很简单的任务&#xff0c;例如设置A1单元格字体颜色为红色。 Range("A1").Font.Color RGB(255, 0, 0)有时需要修改部分字符的颜色&#xff0c;如下图所示&#xff0c;将红色字符字体颜色修改为蓝色。代码将会稍许复杂&am…...

浅谈芯片验证中的仿真运行之 timescale (五)提防陷阱

一 仿真单位 timeunit 我们知道,当我们的代码中写清楚延时语句时,若不指定时间单位,则使用此单位; 例如: `timescale 1ns/1ps 则 #15 语句表示delay15ns; 例:如下代码,module a 的timescale是1ns/1ps, module b 是1ps/1ps; module b中的clk,频率是由输入参…...

uniapp 重置表单数据

场景 例如有数据如下 data(){return {queryForm:{value1:undefined,}} } 点击重置时候想重置form的数据&#xff0c; 操作 Object.assign(this.$data.queryForm, this.$options.data().queryForm); 就可以重置数据...