From 214b9bb66badf345ebfc6d1be1f7035d2f96f020 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 24 Mar 2024 00:51:32 +0800 Subject: [PATCH] added SeriesPool<>::InitRandomSeries() function. --- inc/hgl/type/SeriesPool.h | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/inc/hgl/type/SeriesPool.h b/inc/hgl/type/SeriesPool.h index 72105d0..469e436 100644 --- a/inc/hgl/type/SeriesPool.h +++ b/inc/hgl/type/SeriesPool.h @@ -1,5 +1,6 @@ #pragma once -#include +#include +#include namespace hgl { @@ -18,7 +19,7 @@ namespace hgl public: - const T * GetRawData ()const{return series_data;} ///<取得原始数据指针 + T * GetRawData ()const{return series_data;} ///<取得原始数据指针 const T GetMaxCount ()const{return max_count;} ///<取得最大数量 const T GetFreeCount()const{return access-series_data;} ///<取得空闲数量 @@ -70,6 +71,34 @@ namespace hgl delete[] series_data; } + /** + * 初始化随机序号 + */ + void InitRandomSeries(T min_value=0,T max_value=0) + { + if(min_value==0&&max_value==0) + max_value=max_count-1; + + SortedSets ss; //使用排序集合用来储存随机序号 + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dis(min_value,max_value); + + T *p=series_data; + T value; + + do + { + value=dis(gen); + + if(ss.Add(value)>=0) //加入随机序号,因为使用的是集合,所以不会重复 + { + *p=value; //加入到序号池中 + ++p; + } + }while(ss.GetCount()