Advertisement
theultraman20

alg_impl.hpp

Oct 8th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iterator>
  4. #include <cassert>
  5. #include <iterator>
  6. #include <queue>
  7. #include <type_traits>
  8. #include <vector>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <fstream>
  12.  
  13. //class PDB;
  14.  
  15. template<typename T> std::vector<uint8_t> PDB<T>::genPdb(T start, int const permuts) {
  16.  
  17.     struct Node {
  18.         T elem;
  19.         uint8_t depth;
  20.     };
  21.  
  22.     std::queue<Node> q;
  23.  
  24.     //this single line is literally the entirety of korf's fabled pdb lmao
  25.     std::vector<uint8_t> pdb(permuts/2);
  26.  
  27.     q.push(Node{start, 0});
  28.     uint32_t nodeCount = 0;
  29.     //int iterCount = 0;
  30.     //std::array<uint32_t, 27> invalidRots;
  31.  
  32.     while (!q.empty()) {
  33.  
  34.         Node current = q.front();
  35.         q.pop();
  36.  
  37.         //int rotIdx = 0;
  38.  
  39.  
  40.         for (T neighbor : current.elem.getNeighbors()) {
  41.             uint32_t idx = neighbor.getIdx();
  42.  
  43.             if (!(pdb[idx/2] & ((idx%2) ? 0xf0 : 0x0f))) {
  44.                 q.push(Node{neighbor, static_cast<uint8_t>(current.depth + 1)});
  45.                 nodeCount++;
  46.  
  47.                 pdb[idx/2] |= (static_cast<uint8_t>(current.depth+1) << ((idx%2) ? 4 : 0));
  48.             }
  49.         }
  50.  
  51.         //iterCount++;
  52.     }
  53.  
  54.  
  55.     std::cout << "Node count: " << nodeCount << std::endl;
  56.  
  57.     pdb[start.getIdx()/2] &= ~((0b1111 << (start.getIdx()%2 ? 4 : 0)));
  58.  
  59.     return pdb;
  60. }
  61.  
  62. template<typename T> uint8_t PDB<T>::getDist(int idx) const {
  63.     return (data[idx/2] >> ((idx%2) ? 4 : 0))&0b1111;
  64. }
  65.  
  66.  
  67.  
  68. template<typename T> PDB<T>::PDB(std::string filename) {
  69.     std::ifstream is(filename, std::ios::binary);
  70.     //I prefer !good instead of bad because
  71.     //I try to stay positive
  72.     if (!is.good()) { throw std::exception(); }
  73.  
  74.     uint32_t size;
  75.     is >> size;
  76.  
  77.     //saw this one on SO
  78.     std::vector<uint8_t> bytes(
  79.           (std::istreambuf_iterator<char>(is)),
  80.           (std::istreambuf_iterator<char>()));
  81.  
  82.     if (!is.good()) { throw std::exception(); }
  83.     is.close();
  84.  
  85.     data = bytes;
  86. }
  87.  
  88. template<typename T> PDB<T>::serialize(std::string filename) {
  89.     std::ofstream os(filename, std::ios::binary);
  90.     if (!is.good()) { throw std::exception(); }
  91.  
  92.     std::copy(data.begin(), data.end(), std::ostreambuf_iterator<uint8_t>(os));
  93.     if (!os.good()) { throw std::exception(); }
  94. };
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement