Browse Source

update: interfaces of `LayerQueue`

master
Dnomd343 3 weeks ago
parent
commit
b97cdf49d1
  1. 77
      src/core/fast_cal/internal/layer_queue.inl
  2. 15
      src/core/fast_cal/layer_queue.h
  3. 2
      src/core/main.cc

77
src/core/fast_cal/internal/layer_queue.inl

@ -3,98 +3,71 @@
namespace klotski {
template <typename T>
LayerQueue<T>::LayerQueue(std::initializer_list<T> first_layer, const size_t reserve) {
// data_.resize(reserve);
static_assert(std::is_trivial_v<T>);
// 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<T>
LayerQueue<T>::LayerQueue(std::initializer_list<T> first_layer, const size_t max_size)
: layer_end_(first_layer.size()), queue_end_(0) {
data_ = static_cast<T*>(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 <typename T>
requires std::is_trivial_v<T>
LayerQueue<T>::~LayerQueue() {
// delete []data_;
std::free(data_);
}
template <typename T>
void LayerQueue<T>::emplace(T node) {
data_[queue_end_] = node;
++queue_end_;
requires std::is_trivial_v<T>
T LayerQueue<T>::current() const {
return data_[queue_begin_];
}
template <typename T>
T LayerQueue<T>::current() const {
return data_[queue_begin_];
requires std::is_trivial_v<T>
void LayerQueue<T>::emplace(T node) {
data_[queue_end_] = node;
++queue_end_;
}
template <typename T>
requires std::is_trivial_v<T>
void LayerQueue<T>::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 <typename T>
requires std::is_trivial_v<T>
[[nodiscard]] bool LayerQueue<T>::is_ending() const {
return queue_begin_ == queue_end_;
}
template <typename T>
requires std::is_trivial_v<T>
[[nodiscard]] bool LayerQueue<T>::is_new_layer() const {
return queue_begin_ == layer_begin_;
}
//template <typename T>
//std::vector<T> LayerQueue<T>::layer_cases() const {
// std::vector<T> layer_cases;
// for (size_t offset = layer_begin_; offset < layer_end_; ++offset) {
// layer_cases.emplace_back(data_[offset]);
// }
// return layer_cases;
//}
template <typename T>
requires std::is_trivial_v<T>
std::vector<T> LayerQueue<T>::last_layer() const {
std::vector<T> 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 <typename T>
requires std::is_trivial_v<T>
std::vector<std::vector<T>> LayerQueue<T>::all_layers() const {
std::vector<std::vector<T>> result;
for (size_t i = 0; i < layers_.size() - 1; ++i) {
size_t begin = layers_[i];
size_t end = layers_[i + 1];
std::vector<T> 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<T> layer {data_ + layer_offset_[i], data_ + layer_offset_[i + 1]};
result.emplace_back(layer);
}
return result;

15
src/core/fast_cal/layer_queue.h

@ -7,12 +7,13 @@
namespace klotski {
template <typename T>
requires std::is_trivial_v<T>
class LayerQueue final {
public:
~LayerQueue();
/// Construct from first layer cases and reserve size.
LayerQueue(std::initializer_list<T> first_layer, size_t reserve);
/// Construct from first layer nodes and reserve size.
LayerQueue(std::initializer_list<T> 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<T> data_ {};
T *data_ {nullptr};
std::vector<size_t> layers_ {};
// std::vector<std::pair<size_t, size_t>> layers_ {};
size_t layer_begin_ {0}, layer_end_;
size_t queue_begin_ {0}, queue_end_;
std::vector<size_t> layer_offset_ {0};
};
} // namespace klotski

2
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();

Loading…
Cancel
Save