2016-08-22 49 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