diff --git a/src/core/fast_cal/internal/layer_queue.inl b/src/core/fast_cal/internal/layer_queue.inl index 588fd2a..d71ec1e 100644 --- a/src/core/fast_cal/internal/layer_queue.inl +++ b/src/core/fast_cal/internal/layer_queue.inl @@ -3,98 +3,71 @@ namespace klotski { template -LayerQueue::LayerQueue(std::initializer_list first_layer, const size_t reserve) { - // data_.resize(reserve); - - static_assert(std::is_trivial_v); - - // data_ = new T[reserve]; - data_ = (T*)std::malloc(sizeof(T) * reserve); - - queue_begin_ = 0; - queue_end_ = 0; - for (auto x : first_layer) { - emplace(x); +requires std::is_trivial_v +LayerQueue::LayerQueue(std::initializer_list first_layer, const size_t max_size) + : layer_end_(first_layer.size()), queue_end_(0) { + data_ = static_cast(std::malloc(sizeof(T) * max_size)); + for (const auto node : first_layer) { + emplace(node); } - - layer_begin_ = 0; - layer_end_ = first_layer.size(); - - layers_.reserve(139); -// layers_.emplace_back(0, layer_end_); - layers_.emplace_back(0); - layers_.emplace_back(layer_end_); + layer_offset_.reserve(139); // TODO: confirm the max layer number + layer_offset_.emplace_back(layer_end_); } template +requires std::is_trivial_v LayerQueue::~LayerQueue() { - // delete []data_; std::free(data_); } template -void LayerQueue::emplace(T node) { - data_[queue_end_] = node; - ++queue_end_; +requires std::is_trivial_v +T LayerQueue::current() const { + return data_[queue_begin_]; } template -T LayerQueue::current() const { - return data_[queue_begin_]; +requires std::is_trivial_v +void LayerQueue::emplace(T node) { + data_[queue_end_] = node; + ++queue_end_; } template +requires std::is_trivial_v void LayerQueue::next() { ++queue_begin_; if (queue_begin_ == layer_end_ && !is_ending()) { layer_begin_ = layer_end_; layer_end_ = queue_end_; -// layers_.emplace_back(layer_begin_, layer_end_); - layers_.emplace_back(layer_end_); + layer_offset_.emplace_back(layer_end_); } } template +requires std::is_trivial_v [[nodiscard]] bool LayerQueue::is_ending() const { return queue_begin_ == queue_end_; } template +requires std::is_trivial_v [[nodiscard]] bool LayerQueue::is_new_layer() const { return queue_begin_ == layer_begin_; } -//template -//std::vector LayerQueue::layer_cases() const { -// std::vector layer_cases; -// for (size_t offset = layer_begin_; offset < layer_end_; ++offset) { -// layer_cases.emplace_back(data_[offset]); -// } -// return layer_cases; -//} - template +requires std::is_trivial_v std::vector LayerQueue::last_layer() const { - std::vector layer; - layer.reserve(layer_end_ - layer_begin_); - for (auto offset = layer_begin_; offset < layer_end_; ++offset) { - layer.emplace_back(data_[offset]); // TODO: copy directly - } - return layer; + return {data_ + layer_begin_, data_ + layer_end_}; } template +requires std::is_trivial_v std::vector> LayerQueue::all_layers() const { std::vector> result; - for (size_t i = 0; i < layers_.size() - 1; ++i) { - size_t begin = layers_[i]; - size_t end = layers_[i + 1]; - - std::vector layer {}; - layer.reserve(end - begin); - for (auto offset = begin; offset < end; ++offset) { - layer.emplace_back(data_[offset]); // TODO: copy directly - } + for (size_t i = 0; i < layer_offset_.size() - 1; ++i) { + std::vector layer {data_ + layer_offset_[i], data_ + layer_offset_[i + 1]}; result.emplace_back(layer); } return result; diff --git a/src/core/fast_cal/layer_queue.h b/src/core/fast_cal/layer_queue.h index ffdc749..d92f723 100644 --- a/src/core/fast_cal/layer_queue.h +++ b/src/core/fast_cal/layer_queue.h @@ -7,12 +7,13 @@ namespace klotski { template +requires std::is_trivial_v class LayerQueue final { public: ~LayerQueue(); - /// Construct from first layer cases and reserve size. - LayerQueue(std::initializer_list first_layer, size_t reserve); + /// Construct from first layer nodes and reserve size. + LayerQueue(std::initializer_list first_layer, size_t max_size); // ------------------------------------------------------------------------------------- // @@ -44,14 +45,10 @@ public: // ------------------------------------------------------------------------------------- // private: - size_t layer_begin_, layer_end_; - size_t queue_begin_, queue_end_; - - // std::vector data_ {}; T *data_ {nullptr}; - - std::vector layers_ {}; - // std::vector> layers_ {}; + size_t layer_begin_ {0}, layer_end_; + size_t queue_begin_ {0}, queue_end_; + std::vector layer_offset_ {0}; }; } // namespace klotski diff --git a/src/core/main.cc b/src/core/main.cc index b313b1c..5d59966 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -56,7 +56,7 @@ int main() { // } // for (auto x : fc.furthest()) { - // std::cout << x << std::endl; + // std::cout << x.to_common_code() << std::endl; // } fc.furthest();