int main() { char *key_one = generateHashKey(); puts(key_one); unsigned int retTime = time(0) + 10; while (time(0) < retTime); char *key_two = generateHashKey(); puts(key_two); if(strcmp(key_one, key_two) == 0) { puts("Two Identical Keys are generated!"); return 1; } return 0; }
/* * Extension: Checks and adds to hash table using Zobrist hash function */ void generateUniqueBoardHash(BoardNode currentBoard, int rowMove, int colMove, int currRow, int currCol) { int hashKey; BoardNode generatedBoard; generatedBoard = makeMove(copyParentToChild(currentBoard,createBoard(currentBoard)),rowMove,colMove,currRow,currCol,DELETE); hashKey = generateHashKey(generatedBoard); if(hashBoard(hashKey,generatedBoard)) { addToQueue(generatedBoard); checkTarget(generatedBoard); } }
void pcl::Permutohedral::init (const std::vector<float> &feature, const int feature_dimension, const int N) { N_ = N; d_ = feature_dimension; // Create hash table std::vector<std::vector<short> > keys; keys.reserve ((d_+1) * N_); std::multimap<size_t, int> hash_table; // reserve class memory if (offset_.size () > 0) offset_.clear (); offset_.resize ((d_ + 1) * N_); if (barycentric_.size () > 0) barycentric_.clear (); barycentric_.resize ((d_ + 1) * N_); // create vectors and matrices Eigen::VectorXf scale_factor = Eigen::VectorXf::Zero (d_); Eigen::VectorXf elevated = Eigen::VectorXf::Zero (d_ + 1); Eigen::VectorXf rem0 = Eigen::VectorXf::Zero (d_+1); Eigen::VectorXf barycentric = Eigen::VectorXf::Zero (d_+2); Eigen::VectorXi rank = Eigen::VectorXi::Zero (d_+1); Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> canonical; canonical = Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic>::Zero (d_+1, d_+1); //short * key = new short[d_+1]; std::vector<short> key (d_+1); // Compute the canonical simple for (int i = 0; i <= d_; i++) { for (int j = 0; j <= (d_ - i); j++) canonical (j, i) = i; for (int j = (d_ - i + 1); j <= d_; j++) canonical (j, i) = i - (d_ + 1); } // Expected standard deviation of our filter (p.6 in [Adams etal 2010]) float inv_std_dev = std::sqrt (2.0f / 3.0f) * static_cast<float> (d_ + 1); // Compute the diagonal part of E (p.5 in [Adams etal 2010]) for (int i = 0; i < d_; i++) scale_factor (i) = 1.0f / std::sqrt (static_cast<float> (i + 2) * static_cast<float> (i + 1)) * inv_std_dev; // Compute the simplex each feature lies in for (int k = 0; k < N_; k++) //for (int k = 0; k < 5; k++) { // Elevate the feature (y = Ep, see p.5 in [Adams etal 2010]) int index = k * feature_dimension; // sm contains the sum of 1..n of our faeture vector float sm = 0; for (int j = d_; j > 0; j--) { float cf = feature[index + j-1] * scale_factor (j-1); elevated (j) = sm - static_cast<float> (j) * cf; sm += cf; } elevated (0) = sm; // Find the closest 0-colored simplex through rounding float down_factor = 1.0f / static_cast<float>(d_+1); float up_factor = static_cast<float>(d_+1); int sum = 0; for (int j = 0; j <= d_; j++){ float rd = floorf (0.5f + (down_factor * elevated (j))) ; rem0 (j) = rd * up_factor; sum += static_cast<int> (rd); } // rank differential to find the permutation between this simplex and the canonical one. // (See pg. 3-4 in paper.) rank.setZero (); Eigen::VectorXf tmp = elevated - rem0; for (int i = 0; i < d_; i++){ for (int j = i+1; j <= d_; j++) if (tmp (i) < tmp (j)) rank (i)++; else rank (j)++; } // If the point doesn't lie on the plane (sum != 0) bring it back for (int j = 0; j <= d_; j++){ rank (j) += sum; if (rank (j) < 0){ rank (j) += d_+1; rem0 (j) += static_cast<float> (d_ + 1); } else if (rank (j) > d_){ rank (j) -= d_+1; rem0 (j) -= static_cast<float> (d_ + 1); } } // Compute the barycentric coordinates (p.10 in [Adams etal 2010]) barycentric.setZero (); Eigen::VectorXf v = (elevated - rem0) * down_factor; for (int j = 0; j <= d_; j++){ barycentric (d_ - rank (j) ) += v (j); barycentric (d_ + 1 - rank (j)) -= v (j); } // Wrap around barycentric (0) += 1.0f + barycentric (d_+1); // Compute all vertices and their offset for (int remainder = 0; remainder <= d_; remainder++) { for (int j = 0; j < d_; j++) key[j] = static_cast<short> (rem0 (j) + static_cast<float> (canonical ( rank (j), remainder))); // insert key in hash table size_t hash_key = generateHashKey (key); auto it = hash_table.find (hash_key); int key_index = -1; if (it != hash_table.end ()) { key_index = it->second; // check if key is the right one int tmp_key_index = -1; //for (int ii = key_index; ii < keys.size (); ii++) for (; it != hash_table.end (); ++it) { int ii = it->second; bool same = true; std::vector<short> k = keys[ii]; for (size_t i_k = 0; i_k < k.size (); i_k++) { if (key[i_k] != k[i_k]) { same = false; break; } } if (same) { tmp_key_index = ii; break; } } if (tmp_key_index == -1) { key_index = static_cast<int> (keys.size ()); keys.push_back (key); hash_table.insert (std::pair<size_t, int> (hash_key, key_index)); } else key_index = tmp_key_index; } else { key_index = static_cast<int> (keys.size ()); keys.push_back (key); hash_table.insert (std::pair<size_t, int> (hash_key, key_index)); } offset_[ k * (d_ + 1) + remainder ] = static_cast<float> (key_index); barycentric_[ k * (d_ + 1) + remainder ] = barycentric (remainder); } } // Find the Neighbors of each lattice point // Get the number of vertices in the lattice M_ = static_cast<int> (hash_table.size()); // Create the neighborhood structure if (blur_neighbors_.size () > 0) blur_neighbors_.clear (); blur_neighbors_.resize ((d_+1)*M_); std::vector<short> n1 (d_+1); std::vector<short> n2 (d_+1); // For each of d+1 axes, for (int j = 0; j <= d_; j++) { for (int i = 0; i < M_; i++) { std::vector<short> key = keys[i]; for (int k=0; k<d_; k++){ n1[k] = static_cast<short> (key[k] - 1); n2[k] = static_cast<short> (key[k] + 1); } n1[j] = static_cast<short> (key[j] + d_); n2[j] = static_cast<short> (key[j] - d_); std::multimap<size_t ,int>::iterator it; size_t hash_key; int key_index = -1; hash_key = generateHashKey (n1); it = hash_table.find (hash_key); if (it != hash_table.end ()) key_index = it->second; blur_neighbors_[j*M_+i].n1 = key_index; key_index = -1; hash_key = generateHashKey (n2); it = hash_table.find (hash_key); if (it != hash_table.end ()) key_index = it->second; blur_neighbors_[j*M_+i].n2 = key_index; } } }