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:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的,可以在任何平台上使用。 为了提高并发性,把…...

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

基于 LangChain+LangGraph 来实现一个翻译项目
相信大家在看文档的时候,有时会比较苦恼,比如 AI 相关的文档都是外文,中文文档比较少,看起来会比较吃力,有的时候会看不懂,翻译软件又翻得很乱,完全看不了,今天就基于 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.…...

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

【计算机网络】网络层——IPv4地址(个人笔记)
学习日期:2024.7.24 内容摘要:IPv4地址,分类编址,子网,无分类编址 IPv4地址概述 在TCP/IP体系中,IP地址是一个最基本的概念,IPv4地址就是给因特网上的每一台主机的每一个接口分配一个在全世界…...

c++ 学习笔记之多线程:线程锁,条件变量,唤醒指定线程
基于CAS线程加锁方式 CAS(Compare-And-Swap)和 mutex 都是用于实现线程安全的技术,但它们适用于不同的场景,具有不同的性能和复杂性。下面是对两者的区别和使用场景的详细解释: CAS(Compare-And-Swap&…...

《0基础》学习Python——第二十三讲__网络爬虫/<6>爬取哔哩哔哩视频
一、在B站上爬取一段视频(B站视频有音频和视频两个部分) 1、获取URL 注意:很多平台都有反爬取的机制,B站也不例外 首先按下F12找到第一条复制URL 2、UA伪装,下列图片中(注意代码书写格式) 3、Co…...

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

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

【人工智能】AI时代:探索个人潜能的新视角
文章目录 🍊Al时代的个人发展1 AI的高速发展意味着什么1.1 生产力大幅提升1.2 生产关系的改变1.3 产品范式1.4 产业革命1.5 Al的局限性1.5.1局限一:大模型的幻觉1.5.2 局限二: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 注释说明》
📢 大家好,我是 【战神刘玉栋】,有10多年的研发经验,致力于前后端技术栈的知识沉淀和传播。 💗 🌻 CSDN入驻不久,希望大家多多支持,后续会继续提升文章质量,绝不滥竽充数…...

【LeetCode】71.简化路径
1. 题目 2. 分析 3. 代码 我写了一版很复杂的代码: 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 赋予读写权限,否则将无法写入数据- /usr/loc…...

学习笔记之JAVA篇(0724)
p 方法 方法声明格式: [修饰符1 修饰符2 ...] 返回值类型 方法名(形式参数列表){ java语句;......; } 方法调用方式 普通方法对象.方法名(实参列表)静态方法类名.方法名(实参列表) 方法的详…...

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

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

MySQL的账户管理
目录 1 密码策略 1.1 查看数据库当前密码策略: 1.2 查看密码设置策略 1.3 密码强度检查等级解释(validate_password.policy) 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数据迁移新纪元:数据库数据泵(Data Pump)使用指南 在数据管理的世界里,数据迁移是一个常见且复杂的过程。SQL Server提供了一个强大的工具——数据库数据泵(Data Pump),它可以帮助我…...

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

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

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

《Java初阶数据结构》----4.<线性表---Stack栈和Queue队列>
前言 大家好,我目前在学习java。之前也学了一段时间,但是没有发布博客。时间过的真的很快。我会利用好这个暑假,来复习之前学过的内容,并整理好之前写过的博客进行发布。如果博客中有错误或者没有读懂的地方。热烈欢迎大家在评论区…...

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

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

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

【教学类-69-01】20240721铠甲勇士扑克牌(随机14个数字+字母)涂色(男孩篇)
背景需求: 【教学类-68-01】20240720裙子涂色(女孩篇)-CSDN博客文章浏览阅读250次。【教学类-68-01】20240720裙子涂色(女孩篇)https://blog.csdn.net/reasonsummer/article/details/140578153 前期制作了女孩涂色延…...

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