(相对)尺寸安全的 C++ 包装 STL 容器;

发布于 2024-09-24 16:16:26 字数 4292 浏览 10 评论 0原文

请耐心等待,因为我是自学 C++,并且在工作中花费了有限的额外时间来尝试了解更多信息(我白天是一名化学工程研究员)。

我有一个非常简单的目标: 1. 制作一个尺寸安全的容器来存放一长串的浮标。 2. 制作该容器的专用版本,充当矩阵。

到目前为止,根据我在这里提出的各种问题的一些反馈,我得出的结论是:

template<typename T>
class VectorDeque
{
public:

  void resize_index(unsigned int index) {
    if ( my_container == VECTOR ) {
      try {
        my_vector.resize(index);
        my_container = VECTOR;
      }
      catch(std::bad_alloc &e) {
        my_deque.resize(index);
        my_container = DEQUE;
      }
    }
    else if ( my_container == DEQUE ) {
      my_deque.resize(index);
    }
  }

  T operator[](unsigned int index) { 
    T ret_val;
    if ( STORAGE_CONTAINER == VECTOR ) {
      ret_val = my_vector[index];
    }
    else if ( STORAGE_CONTAINER == DEQUE ) {
      ret_val = my_deque[index];
      }
  }
private:
  enum STORAGE_CONTAINER { NONE, DEQUE, VECTOR };

  std::vector<T> my_vector;
  std::deque<T> my_deque;
  STORAGE_CONTAINER my_container;

  T& get(int index) { 
    T temp_val;
    if(my_container == VECTOR) {
      temp_val = my_vector[index];
    }
    else if(my_container == DEQUE) {
      temp_val = my_deque[index];
    }

    return temp_val;
  }

};

template<typename T>
class VectorDeque2D: public VectorDeque<T>
{
public:

  template<typename T>
  class VectorDeque2D_Inner_Set
  {
    VectorDeque2D& parent;
    int   first_index;
  public:
    // Just init the temp object
    VectorDeque2D_Inner_Set(My2D& p, int first_Index) : 
      parent(p), 
      first_Index(first_index) {} 
    // Here we get the value.
    T& operator[](int second_index)  const 
    { return parent.get(first_index,second_index);}   
  };

  // Return an object that defines its own operator[] that will access the data.
  // The temp object is very trivial and just allows access to the data via 
  // operator[]
  VectorDeque2D_Inner_Set<T> operator[](unsigned int first_index) { 
    return  VectorDeque2D_Inner_Set<T>(*this, first_index);
  }

  void resize_index_second(unsigned int second_index) {
    if ( my_container == VECTOR ) {
      try {
        for (unsigned int couter=0;couter < my_vector.size(); counter++) {
          my_vector[counter].resize(second_index);
        }
        my_container = VECTOR;
      }
      catch(std::bad_alloc &e) {
        for (unsigned int couter=0;couter < my_deque.size(); counter++) {
          my_deque[counter].resize(second_index);
        }
        my_container = DEQUE;
      }
    }
    else if ( my_container == DEQUE ) {
      for (unsigned int couter=0;couter < my_deque.size(); counter++) {
        my_deque[counter].resize(second_index);
      }
    }
  }

  void resize(unsigned int first_index,
          unsigned int second_index) {
    if ( my_container == VECTOR ) {
      try {
        my_vector.resize(first_index);
        for (unsigned int couter=0;couter < my_vector.size(); counter++) {
          my_vector[counter].resize(second_index);
        }
        my_container = VECTOR;
      }
      catch(std::bad_alloc &e) {
        my_deque.resize(first_index);
        for (unsigned int couter=0;couter < my_deque.size(); counter++) {
          my_deque[counter].resize(second_index);
        }
        my_container = DEQUE;
      }    
    }
    else if ( my_container == DEQUE ) {
      my_deque.resize(first_index);
      for (unsigned int couter=0;couter < my_deque.size(); counter++) {
        my_deque[counter].resize(second_index);
      }
    }
  }
private:
  enum STORAGE_CONTAINER { NONE, DEQUE, VECTOR };

  friend class VectorDeque2D_Inner_Set;

  std::vector<std::vector<T> > my_vector;
  std::deque<std::deque<T> > my_deque;
  STORAGE_CONTAINER my_container;

  T& get(int first_index,int second_index) { 
    T temp_val;
    if(my_container == VECTOR) {
      temp_val = my_vector[first_index][second_index];
    }
    else if(my_container == DEQUE) {
      temp_val = my_deque[first_index][second_index];
    }

    return temp_val;
  }

};

通过这个实现,我尝试:
1. 向包装器的用户提供两个访问选项(“.get(x,y)”和“[x][y]”)
2. 通过拥有一个基础包装类然后继承它来创建矩阵来最大化重用。
3.解决连续内存限制达到时从向量转换为双端队列的问题。

这看起来是一个不错的解决方案吗?建议?

Bear with me because I'm self taught in C++ and am spending my limited extra time on the job to try to learn more about it (I'm a chemical engineering researcher by day).

I have a pretty simple objective:
1. Make a size-safe container to store a long list of floats.
2. Make a specialized version of that container that acts as a matrix.

What I've come up with thus far, based on some feedback on various questions I've posed here is:

template<typename T>
class VectorDeque
{
public:

  void resize_index(unsigned int index) {
    if ( my_container == VECTOR ) {
      try {
        my_vector.resize(index);
        my_container = VECTOR;
      }
      catch(std::bad_alloc &e) {
        my_deque.resize(index);
        my_container = DEQUE;
      }
    }
    else if ( my_container == DEQUE ) {
      my_deque.resize(index);
    }
  }

  T operator[](unsigned int index) { 
    T ret_val;
    if ( STORAGE_CONTAINER == VECTOR ) {
      ret_val = my_vector[index];
    }
    else if ( STORAGE_CONTAINER == DEQUE ) {
      ret_val = my_deque[index];
      }
  }
private:
  enum STORAGE_CONTAINER { NONE, DEQUE, VECTOR };

  std::vector<T> my_vector;
  std::deque<T> my_deque;
  STORAGE_CONTAINER my_container;

  T& get(int index) { 
    T temp_val;
    if(my_container == VECTOR) {
      temp_val = my_vector[index];
    }
    else if(my_container == DEQUE) {
      temp_val = my_deque[index];
    }

    return temp_val;
  }

};

template<typename T>
class VectorDeque2D: public VectorDeque<T>
{
public:

  template<typename T>
  class VectorDeque2D_Inner_Set
  {
    VectorDeque2D& parent;
    int   first_index;
  public:
    // Just init the temp object
    VectorDeque2D_Inner_Set(My2D& p, int first_Index) : 
      parent(p), 
      first_Index(first_index) {} 
    // Here we get the value.
    T& operator[](int second_index)  const 
    { return parent.get(first_index,second_index);}   
  };

  // Return an object that defines its own operator[] that will access the data.
  // The temp object is very trivial and just allows access to the data via 
  // operator[]
  VectorDeque2D_Inner_Set<T> operator[](unsigned int first_index) { 
    return  VectorDeque2D_Inner_Set<T>(*this, first_index);
  }

  void resize_index_second(unsigned int second_index) {
    if ( my_container == VECTOR ) {
      try {
        for (unsigned int couter=0;couter < my_vector.size(); counter++) {
          my_vector[counter].resize(second_index);
        }
        my_container = VECTOR;
      }
      catch(std::bad_alloc &e) {
        for (unsigned int couter=0;couter < my_deque.size(); counter++) {
          my_deque[counter].resize(second_index);
        }
        my_container = DEQUE;
      }
    }
    else if ( my_container == DEQUE ) {
      for (unsigned int couter=0;couter < my_deque.size(); counter++) {
        my_deque[counter].resize(second_index);
      }
    }
  }

  void resize(unsigned int first_index,
          unsigned int second_index) {
    if ( my_container == VECTOR ) {
      try {
        my_vector.resize(first_index);
        for (unsigned int couter=0;couter < my_vector.size(); counter++) {
          my_vector[counter].resize(second_index);
        }
        my_container = VECTOR;
      }
      catch(std::bad_alloc &e) {
        my_deque.resize(first_index);
        for (unsigned int couter=0;couter < my_deque.size(); counter++) {
          my_deque[counter].resize(second_index);
        }
        my_container = DEQUE;
      }    
    }
    else if ( my_container == DEQUE ) {
      my_deque.resize(first_index);
      for (unsigned int couter=0;couter < my_deque.size(); counter++) {
        my_deque[counter].resize(second_index);
      }
    }
  }
private:
  enum STORAGE_CONTAINER { NONE, DEQUE, VECTOR };

  friend class VectorDeque2D_Inner_Set;

  std::vector<std::vector<T> > my_vector;
  std::deque<std::deque<T> > my_deque;
  STORAGE_CONTAINER my_container;

  T& get(int first_index,int second_index) { 
    T temp_val;
    if(my_container == VECTOR) {
      temp_val = my_vector[first_index][second_index];
    }
    else if(my_container == DEQUE) {
      temp_val = my_deque[first_index][second_index];
    }

    return temp_val;
  }

};

With this implementation I tried to:
1. Present the user of the wrapper with two options for access (".get(x,y)" and "[x][y]")
2. Maximize reuse by having a based wrapped class and then inheriting it to make the matrix.
3. Solve the problem of transitioning from a vector to a deque if the continuous memory limit is hit.

Does this seem like a decent solution? Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-10-01 16:16:26

你看过 Boost::Matrix?该库中已经内置了许多数字和线性代数的东西。

编辑:

阅读有关在达到大小限制时从向量转换为双端队列的评论后,请使用deque。像这样变得“花哨”会降低你的生产力。专注于手头的问题,让收藏担心记忆。双端队列对于大型数组来说相当快,并且与向量相比,只有在释放内存时才会受到影响。

Have you looked at Boost::Matrix? There's a lot of numeric and linear algebra things already built within that library.

EDIT:

After reading your comment about transitioning from a vector to a deque when size limits are reached, go with deque. Getting "fancy" like that is slowing your productivity down. Focus on the problem at hand and let the collection worry about the memory. Deque is quite fast for large arrays and only suffers when you release memory in comparison to vector.

美煞众生 2024-10-01 16:16:26

对我来说,在一次使用期间在两者之间转换似乎不太值得付出努力。

如果您想通过自己的方式来完成此操作,您可以定义第二个模板参数,该参数允许在编译时指定容器类型。那么您就不需要同时使用 vectordeque 作为成员,并且类型切换代码就会消失。

template<typename T, typename CONTAINER>
class VectorDeque
{
// snip
private:
  CONTAINER<T> _storage;
};

Transitioning between the two during a single usage seems unlikely to be worth the effort, to me.

If you want to do this via roll your own, you could define a second template parameter that allows the container type to be specified at compile-time. Then you would not need both vector and deque as members and the type switching code goes away.

template<typename T, typename CONTAINER>
class VectorDeque
{
// snip
private:
  CONTAINER<T> _storage;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文