/*
 * Written by Masayuki Takigahira
 * Copyright 2012 Kyoto University and Honda Motor Co.,Ltd.
 * All rights reserved.
 * HARK was developed by researchers in Okuno Laboratory
 * at the Kyoto University and Honda Research Institute Japan Co.,Ltd.
 */

#ifndef HARK_UUID_BASE_HPP
#define HARK_UUID_BASE_HPP

#include <string>
#include <random>
#include <cstdint>

namespace HARK_UUID {
  class uuid {
  private:
    const std::string HEXDECSTR = "0123456789abcdef";
    std::mt19937_64 mt;
  public:
    uuid():mt(std::random_device()()){ }
    uuid(int seed):mt(seed){ }

    std::string v4(){
      std::string id = std::string(36,' ');
      uint_fast8_t  val[32];
      uint_fast64_t rnd;
      int i = 0, j = 0;
      rnd = mt(); while(i<16){ val[i] = rnd & 0xf; rnd >>= 4; i++; };
      rnd = mt(); while(i<32){ val[i] = rnd & 0xf; rnd >>= 4; i++; };

      i = 0;
      while(j<36){
        switch(j){
        case 8:
        case 13:
        case 18:
        case 23:
          id[j] = '-'; j++;
          break;
        case 14:
          id[j] = '4'; j++;
          break;
        default:
          id[j] = HEXDECSTR[(j == 19) ? ((val[i] & 0x3) | 0x8) : val[i]]; j++; i++;
          break;
        }
      }
      return id;
    }
  };
} // namespace HARK_UUID end

#endif // HARK_UUID_BASE_HPP

