Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iterator>
- #include <cassert>
- #include <iterator>
- #include <queue>
- #include <type_traits>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <fstream>
- //class PDB;
- template<typename T> std::vector<uint8_t> PDB<T>::genPdb(T start, int const permuts) {
- struct Node {
- T elem;
- uint8_t depth;
- };
- std::queue<Node> q;
- //this single line is literally the entirety of korf's fabled pdb lmao
- std::vector<uint8_t> pdb(permuts/2);
- q.push(Node{start, 0});
- uint32_t nodeCount = 0;
- //int iterCount = 0;
- //std::array<uint32_t, 27> invalidRots;
- while (!q.empty()) {
- Node current = q.front();
- q.pop();
- //int rotIdx = 0;
- for (T neighbor : current.elem.getNeighbors()) {
- uint32_t idx = neighbor.getIdx();
- if (!(pdb[idx/2] & ((idx%2) ? 0xf0 : 0x0f))) {
- q.push(Node{neighbor, static_cast<uint8_t>(current.depth + 1)});
- nodeCount++;
- pdb[idx/2] |= (static_cast<uint8_t>(current.depth+1) << ((idx%2) ? 4 : 0));
- }
- }
- //iterCount++;
- }
- std::cout << "Node count: " << nodeCount << std::endl;
- pdb[start.getIdx()/2] &= ~((0b1111 << (start.getIdx()%2 ? 4 : 0)));
- return pdb;
- }
- template<typename T> uint8_t PDB<T>::getDist(int idx) const {
- return (data[idx/2] >> ((idx%2) ? 4 : 0))&0b1111;
- }
- template<typename T> PDB<T>::PDB(std::string filename) {
- std::ifstream is(filename, std::ios::binary);
- //I prefer !good instead of bad because
- //I try to stay positive
- if (!is.good()) { throw std::exception(); }
- uint32_t size;
- is >> size;
- //saw this one on SO
- std::vector<uint8_t> bytes(
- (std::istreambuf_iterator<char>(is)),
- (std::istreambuf_iterator<char>()));
- if (!is.good()) { throw std::exception(); }
- is.close();
- data = bytes;
- }
- template<typename T> PDB<T>::serialize(std::string filename) {
- std::ofstream os(filename, std::ios::binary);
- if (!is.good()) { throw std::exception(); }
- std::copy(data.begin(), data.end(), std::ostreambuf_iterator<uint8_t>(os));
- if (!os.good()) { throw std::exception(); }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement