Эффективный многорядный вектор - PullRequest
0 голосов
/ 27 февраля 2019

Мне нужна эффективная реализация вектора с несколькими строками, каждая из которых имеет одинаковое количество столбцов, что не слишком уродливо в C ++.В настоящее время у меня есть следующее:

class BaseVector {
protected: // variables
  int64_t _capacity;
  int64_t _nColumns;

protected:
  template<typename taItem> void Allocate(taItem * &p, const int64_t nItems) {
    p = static_cast<taItem*>(MemPool::Instance().Acquire(sizeof(taItem)*nItems));
    if (p == nullptr) {
      __debugbreak();
    }
  }
  template<typename taItem> void Reallocate(taItem * &p, const int64_t newCap) {
    taItem *np;
    Allocate(np, newCap);
    Utils::AlignedNocachingCopy(np, p, _nColumns * sizeof(taItem));
    MemPool::Instance().Release(p, _capacity * sizeof(taItem));
    p = np;
  }
  // Etc for Release() operation

public:
  explicit BaseVector(const int64_t initCap) : _capacity(initCap), _nColumns(0) { }
  void Clear() { _nColumns = 0; }
  int64_t Size() const { return _nColumns; }
};

class DerivedVector : public BaseVector {
    __m256d *_pRowA;
    __m256i *_pRowB;
    uint64_t *_pRowC;
    uint8_t *_pRowD;
    // Etc. for other rows
public:
    DerivedVector(const int64_t nColumns) : BaseVector(nColumns) {
        Allocate(_pRowA, nColumns);
        Allocate(_pRowB, nColumns);
        Allocate(_pRowC, nColumns);
        Allocate(_pRowD, nColumns);
        // Etc. for the other rows
    }
    void IncSize() {
        if(_nColumns >= _capacity) {
            const int64_t newCap = _capacity + (_capacity >> 1) + 1;
            Reallocate(_pRowA, newCap);
            Reallocate(_pRowB, newCap);
            Reallocate(_pRowC, newCap);
            Reallocate(_pRowD, newCap);
            // Etc. for other rows
            _capacity = newCap;
        }
        _nColumns++; 
    }
    ~DerivedVector() {
        // Call here the Release() operation for all rows
    }
};

Проблема с этим подходом состоит в том, что может быть 30 строк, поэтому мне приходится печатать вручную (и повторяться) 30 раз Allocate, 30 раз Reallocate30 раз Release и т. Д.

Так есть ли в C ++ способ сохранить этот код сухим и быстрым?Я в порядке с макросами, но не тяжелый полиморфизм в каждом доступе к ячейке в векторе, потому что это снизит производительность.

...