C++ 允许通过迭代和随机选择子集(C+;+;)进行更改的数据结构
给定一个固定大小的对象数组a,假设这些对象中较小的一个子集满足特定条件B。我希望以大致相同的频率完成三项任务:C++ 允许通过迭代和随机选择子集(C+;+;)进行更改的数据结构,c++,c++11,pointers,data-structures,random-access,C++,C++11,Pointers,Data Structures,Random Access,给定一个固定大小的对象数组a,假设这些对象中较小的一个子集满足特定条件B。我希望以大致相同的频率完成三项任务: 我希望在访问by索引中的对象时,能够随时将当前不符合条件B的对象更改为符合条件B 我希望在按索引访问对象时,能够将当前满足条件B的对象更改为不再满足条件B 我还希望能够从满足条件B的对象中选择一个随机对象 所有任务应能在恒定时间内完成,或尽可能接近恒定时间,不依赖于A中的对象数量,也不依赖于满足标准B的对象数量。如果恒定时间是不可能的,我怀疑是这样的,那么考虑到我前面提到的频率,我想尽
例如,我下面的C++实现。虽然计时部分(代码中重复了大量次的部分)与a(alltiles)的总体大小无关,但时间复杂度与B(bluetiles)成线性关系(不管alltiles的数量是否增加),这严重降低了代码的速度
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <algorithm>
using namespace std;
enum color {RED, GREEN, BLUE};
const int NUM_ATTEMPTS = 10000;
const int INITIAL_NUM_BLUE_TILES = 1000;
const int TOTAL_TILES = 1000000;
struct tile
{
int color = RED;
};
struct room
{
vector<tile> alltiles;
vector<tile*> bluetiles;
room(vector<tile> v) : alltiles(v) {}
};
int main()
{
srand (time(NULL));
// set up the initial room, time complexity here is irrelevant
room myroom(vector<tile>(1*TOTAL_TILES));
for(int i = 0; i < INITIAL_NUM_BLUE_TILES; i++)
{
myroom.alltiles[i].color = BLUE;
myroom.bluetiles.push_back(&myroom.alltiles[i]);
}
auto begin = std::chrono::high_resolution_clock::now();
for(int attempt_num = 0; attempt_num < NUM_ATTEMPTS; attempt_num++)
{
// access a BLUE tile by index from alltiles to change its color to RED
myroom.alltiles[5].color = RED; // constant time
myroom.bluetiles.erase(std::remove(myroom.bluetiles.begin(), myroom.bluetiles.end(), &myroom.alltiles[5]), myroom.bluetiles.end()); // linear time, oh no!
// access a RED tile by index from alltiles to change its color to BLUE
myroom.alltiles[5].color = BLUE; // constant time
myroom.bluetiles.push_back(&myroom.alltiles[5]); // constant time
// randomly choose from ONLY the blue tiles
int rand_index = rand() % myroom.bluetiles.size(); // constant time
myroom.bluetiles[rand_index]->color = GREEN; // constant time
myroom.bluetiles[rand_index]->color = BLUE; // constant time
// so now I have constant time access to a random blue tile
}
auto end = std::chrono::high_resolution_clock::now();
double runtime = std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count();
cout << runtime << " ms" << endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
枚举颜色{红、绿、蓝};
const int NUM_尝试次数=10000;
const int INITIAL_NUM_BLUE_TILES=1000;
const int TOTAL_TILES=1000000;
结构砖
{
int颜色=红色;
};
结构室
{
矢量全瓦;
矢量蓝瓷砖;
房间(向量v):所有瓷砖(v){}
};
int main()
{
srand(时间(空));
//设置初始房间,这里的时间复杂度无关紧要
房间myroom(向量(1*总瓦数));
对于(int i=0;icolor=GREEN;//恒定时间
myroom.bluetiles[rand\u index]->color=BLUE;//恒定时间
//所以现在我有恒定的时间访问一个随机的蓝色瓷砖
}
自动结束=标准::时钟::高分辨率时钟::现在();
double runtime=std::chrono::duration_cast(end-begin).count();
cout更新:这类似于我为SO问题提出的解决方案
您可以实现如下SubsetVector
类,该类允许您在O(1)中插入/删除子集中的元素(即标记元素)。然后,它允许您在O(1)中查找子集的大小,并从O(1)中的该子集访问第i项.我认为这是您想要的。请注意,子集不保证任何特定的顺序,但这应该可以满足您的需要
其思想是保持两个向量
包含实际数据的m_条目
。m_条目[i]
包含元素和m_子集索引的索引(如果元素在子集中),否则为-1
m_subset_index
包含子集中的m_条目
元素的所有索引
以下是代码(已编译但未测试):
模板
类子部门
{
私人:
结构条目
{
T元素;
int index_在_子集中=-1;
};
公众:
显式子扇区(无符号大小=0):m_条目(大小)
{
m_子集_指数。储量(规模);
}
无效推回(常量T和元素)
{
m_entries.push_back(Entry{element,-1});
}
常量T&运算符[](无符号索引)常量{返回m_项[index].element;}
运算符[](无符号索引){返回m_项[index].element;}
void insert_in_子集(无符号索引)
{
if(m_条目[索引]。_子集中的索引<0){
m_条目的[index].index_in_subset=m_subset_index.size();
m_子集_索引。向后推_(索引);
}
}
从_子集中删除_无效(无符号索引)
{
if(m_条目[索引]。_子集中的索引>=0){
自动子集索引=m\u条目[索引]。在子集中索引;
自动&entry_to_fix=m_entries[m_subset_index.back()];
std::swap(m_subset_index[subset_index],m_subset_index.back());
_子集中的_fix.index_条目=子集索引;
m_subset_index.pop_back();
m_条目[索引]。_子集中的索引_=-1;
}
}
无符号子集_size()常量
{
返回m_subset_index.size();
}
T&子集索引(无符号子集索引)
{
自动索引=m_子集索引。at(子集索引);
返回m_条目.at(index).element;
}
常量T&subset_at(无符号子集索引)常量
{
自动索引=m_子集索引。at(子集索引);
返回m_条目.at(index).element;
}
私人:
std::向量m_项;
向量m_子集指数;
};
myroom.bluetiles.erase(std::remove(myroom.bluetiles.begin()、myroom.bluetiles.end()、&myroom.alltiles[5]),
template <class T>
class SubsetVector
{
private:
struct Entry
{
T element;
int index_in_subset = -1;
};
public:
explicit SubsetVector(unsigned size = 0) : m_entries(size)
{
m_subset_indices.reserve(size);
}
void push_back(const T & element)
{
m_entries.push_back(Entry{element, -1});
}
const T & operator[](unsigned index) const { return m_entries[index].element; }
T & operator[](unsigned index) { return m_entries[index].element; }
void insert_in_subset(unsigned index)
{
if (m_entries[index].index_in_subset < 0) {
m_entries[index].index_in_subset = m_subset_indices.size();
m_subset_indices.push_back(index);
}
}
void erase_from_subset(unsigned index)
{
if (m_entries[index].index_in_subset >= 0) {
auto subset_index = m_entries[index].index_in_subset;
auto & entry_to_fix = m_entries[m_subset_indices.back()];
std::swap(m_subset_indices[subset_index], m_subset_indices.back());
entry_to_fix.index_in_subset = subset_index;
m_subset_indices.pop_back();
m_entries[index].index_in_subset = -1;
}
}
unsigned subset_size() const
{
return m_subset_indices.size();
}
T & subset_at(unsigned subset_index)
{
auto index = m_subset_indices.at(subset_index);
return m_entries.at(index).element;
}
const T & subset_at(unsigned subset_index) const
{
auto index = m_subset_indices.at(subset_index);
return m_entries.at(index).element;
}
private:
std::vector<Entry> m_entries;
std::vector<unsigned> m_subset_indices;
};