![](/img/trans.png)
[英]Optimal data structure (in C++) for random access and looping through elements
[英]Data Structure(s) Allowing For Alteration Through Iteration and Random Selection From Subset (C++)
給定一個固定大小的對象數組 A,假設這些對象的一個小得多的子集滿足某個標准 B。我想以大致相等的頻率完成三項任務:
所有任務都應該能夠在恆定時間內完成,或者盡可能接近恆定時間,不依賴於 A 中的對象數量,也不依賴於滿足條件 B 的對象數量。 如果恆定時間是不可能的,我懷疑是這種情況,那么我想盡快完成兩者,考慮到我之前提到的頻率。 如果這兩個任務被大量重復,什么數據結構適合這兩個任務?
以我下面的 C++ 實現為例。 雖然計時部分(代碼中重復大量次數的部分)與 A(所有分塊)的整體大小無關,但時間復雜度線性取決於 B(藍分塊)(無論所有分塊的數量是否增加)與否),嚴重減慢代碼速度。
#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;
}
正在計時的部分是我經常感興趣的操作; 在實際程序中,選擇要更改哪些圖塊背后的邏輯是不同的。 希望更好的數據結構不需要任何概率分析,但我擔心它仍然可能。
我懷疑,也許,通過在 tile 類中保留一個指針(指向 bluetiles 向量中的元素)來使用雙重間接可能可以讓我在恆定時間內實現這一目標,但我不確定。 我想它至少可以加快速度,因為不再需要搜索 bluetiles,但是在 bluetiles 中刪除元素仍然是線性時間(因為我使用的是向量),所以我真的只是不知道在這里做什么。
你能設計出最快的數據結構來實現這個目標,並從我的例子中提供一個 C++ 實現構建嗎? 還是我所擁有的和它永遠一樣好?
更新:這類似於我為O(1) 中 unordered_set 中的 SO 問題Random element提出的解決方案
您可以實現類似於以下SubsetVector<T>
類的內容,它允許您在 O(1) 中從子集中插入/刪除元素(即標記它們)。 然后它讓你在 O(1) 中找到子集的大小,並在 O(1) 中訪問這個子集中的第 i 個項目。 我想這就是你想要的。 請注意,子集不保證任何特定順序,但這應該可以滿足您的需求。
這個想法是維護兩個向量。
m_entries
包含實際數據。 m_entries[i]
包含元素和m_subset_indices
的索引,如果元素在子集中,否則 -1。m_subset_indices
包含子集中的m_entries
元素的所有索引。這是代碼(已編譯但未測試):
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;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.