vector.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <vector>
  3. #include <functional>
  4. namespace ylib
  5. {
  6. template<typename T>
  7. class vector:public std::vector<T>
  8. {
  9. public:
  10. vector(const std::vector<T>& value):std::vector<T>()
  11. {
  12. operator=(value);
  13. }
  14. vector() : std::vector<T>()
  15. {
  16. }
  17. ~vector()
  18. {
  19. }
  20. void operator=(const std::vector<T>& value)
  21. {
  22. *((std::vector<T>*)this) = value;
  23. }
  24. bool find(std::function<bool(T& value, size_t idx)> callback)
  25. {
  26. for (size_t i = 0; i < this->size(); i++)
  27. {
  28. if (callback((*this)[i], i))
  29. return true;
  30. }
  31. return false;
  32. }
  33. bool find(const T& value)
  34. {
  35. for (size_t i = 0; i < this->size(); i++)
  36. {
  37. if(value == this->at(i))
  38. return true;
  39. }
  40. return false;
  41. }
  42. bool find(std::function<bool(T& value, size_t idx)> callback,T& value)
  43. {
  44. for (size_t i = 0; i < this->size(); i++)
  45. {
  46. if (callback((*this)[i], i))
  47. {
  48. value =this->at(i);
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. void loop(std::function<void(const T& value)> callback)
  55. {
  56. for (size_t i = 0; i < this->size(); i++)
  57. callback(this->at(i));
  58. }
  59. void loop_fl(std::function<void(const T& value,bool first,bool last)> callback)
  60. {
  61. for (size_t i = 0; i < this->size(); i++)
  62. callback(this->at(i),i==0,i==this->size()-1);
  63. }
  64. void rloop(std::function<void(const T& value)> callback)
  65. {
  66. size_t size = this->size();
  67. for (size_t i = 0; i < this->size(); i++)
  68. callback(this->at(size-i-1));
  69. }
  70. void rloop_fl(std::function<void(const T& value, bool first, bool last)> callback)
  71. {
  72. size_t size = this->size();
  73. for (size_t i = 0; i < this->size(); i++)
  74. callback(this->at(size - i - 1), i == size - 1, i == 0);
  75. }
  76. };
  77. }