From aeae7b89abe476464af798640893d322916ffa92 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Mon, 17 Jul 2023 20:26:23 +0800 Subject: [PATCH] added GetPointer in Map<> --- inc/hgl/type/Map.cpp | 17 +++++++++++++++++ inc/hgl/type/Map.h | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/inc/hgl/type/Map.cpp b/inc/hgl/type/Map.cpp index aa16001..4ec26d3 100644 --- a/inc/hgl/type/Map.cpp +++ b/inc/hgl/type/Map.cpp @@ -179,6 +179,23 @@ namespace hgl data_list.Insert(FindPos(obj->key),obj); } + /** + * 根据索引取得指定数据的指针
+ * 比如定义了Map,直接返回DATA需要复制会消耗一些时间,直接返回DATA *会更好一些 + */ + template + V *_Map::GetPointer(const K &key)const + { + int index=Find(key); + + if(index==-1) + return(nullptr); + + KVData *obj=GetListObject(data_list,index); + + return &(obj->value); + } + /** * 根据索引取得数据与序号 * @param flag 数据索引 diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index 5d98984..0054b18 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -50,6 +50,7 @@ namespace hgl bool KeyExist(const K &key)const{return(Find(key)!=-1);} ///<确认这个数据是否存在 bool ValueExist(const V &value)const{return(FindByValue(value)!=-1);} ///<确认这个数据是否存在 bool Check(const K &key,const V &value)const; ///<确认数据是否是这个 + virtual V * GetPointer(const K &key)const; ///<取得数据指针 virtual int GetValueAndSerial(const K &,V &) const; ///<取得数据与索引 bool Get(const K &key,V &value)const{return(GetValueAndSerial(key,value)>=0);} ///<取得数据 virtual bool Delete(const K &,V &); ///<将指定数据从列表中移除,并获得这个数据 @@ -124,7 +125,6 @@ namespace hgl return count; } - KVData *GetItem(int n){return GetListObject(data_list,n);} ///<取指定序号的数据 bool GetBySerial(int,K &,V &) const; ///<取指定序号的数据 bool GetKey(int,K &); ///<取指定序号的索引