geo_hash.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "stdafx.h"
  2. #include "ProcessRemodule.h"
  3. #include "geo_hash.h"
  4. NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
  5. void geo_list::find_near(std::vector<std::string>&ret,int x,int y,unsigned h,int dist2,unsigned mask,const std::string & card_no)
  6. {
  7. for(auto it=geo2card.lower_bound(h);it!=geo2card.end();++it)
  8. {
  9. if((it->first&mask)!=h)
  10. break;
  11. auto pos=ghash::decode(it->first);
  12. int xi=std::get<0>(pos);
  13. int yi=std::get<1>(pos);
  14. unsigned sn = (xi-x)*(xi-x)+(yi-y)*(yi-y);
  15. if(sn > dist2)
  16. continue;
  17. if (card_no.compare(it->second) == 0)
  18. continue;
  19. //std::stringstream ss;
  20. //ss<<"-----LemonHash---findnear---cardid"<<card_no<<"x:"<<x<<"y:"<<y<<"dist2:"<<dist2<<" cardid:"<<it->second<<"x:"<<xi<<"y:"<<yi<<"sn"<<sn;
  21. //debug_print_syslog(0,"%s",ss.str().c_str());
  22. ret.push_back(it->second);
  23. }
  24. }
  25. std::vector<std::string> geo_list::find_near(int x,int y,int dist,const std::string & card_no)
  26. {
  27. int t=1;
  28. while(t<dist) t<<=1;
  29. int x0=x&~(t-1) , y0=y&~(t-1);
  30. std::vector<std::string> r;
  31. const int dist2=dist*dist;
  32. unsigned mask=~(t*t-1);
  33. find_near(r,x,y,ghash::encode(x0-t,y0-t),dist2,mask,card_no);
  34. find_near(r,x,y,ghash::encode(x0,y0-t),dist2,mask,card_no);
  35. find_near(r,x,y,ghash::encode(x0+t,y0-t),dist2,mask,card_no);
  36. find_near(r,x,y,ghash::encode(x0-t,y0),dist2,mask,card_no);
  37. find_near(r,x,y,ghash::encode(x0,y0),dist2,mask,card_no);
  38. find_near(r,x,y,ghash::encode(x0+t,y0),dist2,mask,card_no);
  39. find_near(r,x,y,ghash::encode(x0-t,y0+t),dist2,mask,card_no);
  40. find_near(r,x,y,ghash::encode(x0,y0+t),dist2,mask,card_no);
  41. find_near(r,x,y,ghash::encode(x0+t,y0+t),dist2,mask,card_no);
  42. return std::move(r);
  43. }
  44. //std::vector<std::string> find_near(const char*card_no,int dist)
  45. std::vector<std::string> geo_list::find_near(const std::string& card_no,int dist)
  46. {
  47. std::vector<std::string> r;
  48. auto it=card2geo.find(card_no);
  49. if(it==card2geo.end())
  50. return std::move(r);
  51. auto h=ghash::decode(it->second);
  52. return find_near(std::get<0>(h),std::get<1>(h),dist,card_no);
  53. }
  54. //void update(int x,int y,const char*card_no)
  55. void geo_list::update(int x,int y,const std::string card_no)
  56. {
  57. //std::stringstream ss;
  58. unsigned h=ghash::encode(x,y);
  59. //ss<<"-----LemonHash--update--"<<card_no<<"x:"<<x<<"y:"<<y<<"h:"<<h;
  60. //debug_print_syslog(0,"%s",ss.str().c_str());
  61. auto it=card2geo.find(card_no);
  62. if(it==card2geo.end())
  63. {
  64. card2geo.insert(std::make_pair(card_no,h));
  65. geo2card.insert(std::make_pair(h,card_no));
  66. }
  67. else
  68. {
  69. it->second=h;
  70. for(auto it1=geo2card.begin();it1!=geo2card.end();++it1)
  71. {
  72. if(it1->second.compare(card_no)==0)
  73. {
  74. geo2card.erase(it1);
  75. break;
  76. }
  77. }
  78. geo2card.insert(std::make_pair(h,card_no));
  79. }
  80. }
  81. NAMESPACE_POINT_END(NAMESPACE_POINT)