erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
1.9 KiB

пре 5 година
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string.h>
  4. class Binary {
  5. public:
  6. unsigned char *bin;
  7. size_t size;
  8. bool allocated;
  9. Binary() : bin(NULL), size(0), allocated(false) { }
  10. Binary(const char *data) {
  11. bin = (unsigned char *) data;
  12. size = strlen(data);
  13. allocated = false;
  14. }
  15. Binary(const Binary &b) {
  16. bin = b.bin;
  17. size = b.size;
  18. allocated = false;
  19. }
  20. ~Binary() {
  21. if (allocated) {
  22. delete bin;
  23. }
  24. }
  25. operator std::string() {
  26. return (const char *) bin;
  27. }
  28. friend std::ostream & operator<<(std::ostream & str, Binary const &b) {
  29. return str << b.bin;
  30. }
  31. bool operator<(const Binary &b) {
  32. if(size < b.size) {
  33. return true;
  34. } else if (size > b.size) {
  35. return false;
  36. } else {
  37. return memcmp(bin,b.bin,size) < 0;
  38. }
  39. }
  40. bool operator<(Binary &b) {
  41. if(size < b.size) {
  42. return true;
  43. } else if (size > b.size) {
  44. return false;
  45. } else {
  46. return memcmp(bin,b.bin,size) < 0;
  47. }
  48. }
  49. bool operator>(const Binary &b) {
  50. if(size > b.size) {
  51. return true;
  52. } else if (size < b.size) {
  53. return false;
  54. } else {
  55. return memcmp(bin,b.bin,size) > 0;
  56. }
  57. }
  58. bool operator== (const Binary &b) {
  59. if (size == b.size ) {
  60. return memcmp(bin,b.bin, std::min(size, b.size)) == 0;
  61. } else {
  62. return false;
  63. }
  64. }
  65. operator std::string() const {
  66. return (const char*) bin;
  67. }
  68. Binary& set_data(const char *data) {
  69. bin = (unsigned char *) data;
  70. size = strlen(data);
  71. return *this;
  72. }
  73. void copy(char *inbin, size_t insize) {
  74. bin = (unsigned char *) operator new(insize);
  75. allocated = true;
  76. size = insize;
  77. memcpy(bin, inbin, size);
  78. }
  79. };
  80. inline bool operator < (const Binary &a, const Binary &b) {
  81. if(a.size < b.size) {
  82. return true;
  83. } else if (a.size > b.size) {
  84. return false;
  85. } else {
  86. return memcmp(a.bin,b.bin, std::min(a.size, b.size)) < 0;
  87. }
  88. }