2016-08-22 51 views
2

我已經實現了一個對圖像進行計算的類。處理是在給定圖像的一個子集上完成的(可以說每1000箇中有100個),並且每個圖像都需要不同次數的迭代才能完成。處理使用GPU,因此不可能一次使用所有圖像。當圖像的處理完成後,該圖像被移除並且另一個被添加。所以我用三個不同的載體image_outcomeimage_indeximage_operation保持infromations有關圖片:更喜歡什麼數據結構而不是操縱多個向量

  1. image_outcomestd::vector<float>和它的每個元素是作爲標準來決定何時該圖像是一個值完了。
  2. image_index是一個std::vector<int>,它保存原始數據集中圖像的索引。
  3. image_operationstd::vector<MyEnumValue>,其中包含用於更新image_outcome的操作。屬於enum類型,其值是許多可能的操作之一。

還有兩個功能,一個用於刪除已完成的圖像,另一個用於添加儘可能多的圖像(如果輸入中仍有足夠的圖像)。

  1. remove_images()函數獲取所有三個矩陣和圖像矩陣,並使用std::vector.erase()刪除元素。
  2. add_images()再次採用三個矩陣,圖像矩陣將新圖像和相關信息添加到矢量。

因爲我使用的具有相同索引每個向量的erase()(也以類似的方式來添加)我當時就想:

  1. 使用私人struct有三個矢量(嵌套結構)。
  2. 使用使用三個向量(嵌套類)實現的私有class
  3. 使用vec以外的其他數據結構。

代碼的一塊進行水平例如可以是以下基金:代替用3個載體,用戶定義的對象的單個載體將更好地工作一個struct

class ComputationClass { 
    public: 
    // the constructor initializes the member variables 
    ComputationClass(); 
    void computation_algorithm(std::vector<cv::Mat> images); 

    private: 
    // member variables which define the algorithms parameters 
    // add_images() and remove_images() functions take more than these 
    // arguments, but I only show the relevant here 
    add_images(std::vector<float>&, std::vector<int>&, std::vector<MyEnumValue>&); 
    remove_images(std::vector<float>&, std::vector<int>&, std::vector<MyEnumValue>&); 
}; 

void ComputationClass::computation_algorithm(std::vector<cv::Mat> images) { 
    std::vector<float> image_output; 
    std::vector<int> image_index; 
    std::vector<MyEnumValue> image_operation; 

    add_images(image_output, image_index, image_operation); 

    while (there_are_still_images_to_process) { 
    // make computations by updating the image_output vector 
    // check which images finished computing 
    remove_images(image_output, image_index, image_operation); 
    add_images(image_output, image_index, image_operation); 
    } 
} 
+2

包含「float」,「int」和「MyEnumValue」的結構,然後是該結構的向量? –

+0

關聯在一起的數據應該保持在一起,否則最終難以維護它。它也會減少緩存失誤。 – StoryTeller

回答

2

我想,。

std::vector<MyImage> images; 

class MyImage { 
    Image OImage; // the actual image 
    float fOutcome; 
    int dIndex; 
    MyEnumValue eOperation; 
    bool getIsDone() { 
     return fOutcome > 0; // random condition 
    } 
} 

您可以添加到載體或擦除矢量符合條件

if((*it).getIsDone()) { 
    VMyVector.erase(it); 
} 

在我看來,保持3個載體即走平行很容易犯錯誤,很難修改。

+0

我建議使用指針,它會更有效率。 std :: vector > images; –

+0

3向量可能會更快。 – VladimirS

+0

我是否應該在另一個類中實現此類,因爲它將包含在另一個項目中,並且我想提供最好的封裝。 – Pey