You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vector.h 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_LITE_INTERNAL_INCLUDE_VECTOR_H
  17. #define MINDSPORE_LITE_INTERNAL_INCLUDE_VECTOR_H
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stddef.h>
  22. #define DEFAULT_CAPACITY 4
  23. struct MSTensor;
  24. struct Node;
  25. template <typename T>
  26. class Vector {
  27. public:
  28. Vector();
  29. explicit Vector(size_t size);
  30. Vector(size_t size, const T &value);
  31. Vector(const Vector<T> &vector);
  32. ~Vector();
  33. void clear();
  34. void push_back(const T &elem);
  35. void push_back(T &&);
  36. void pop_back();
  37. void insert(const T &elem, size_t index);
  38. T *begin();
  39. const T *begin() const;
  40. T *end();
  41. const T *end() const;
  42. T &front();
  43. const T &front() const;
  44. T &back();
  45. const T &back() const;
  46. T &at(size_t index);
  47. const T &at(size_t index) const;
  48. T &operator[](size_t index);
  49. const T &operator[](size_t index) const;
  50. T *data();
  51. const T *data() const;
  52. size_t size() const;
  53. size_t capacity() const;
  54. bool empty() const;
  55. void erase(size_t index);
  56. void resize(size_t size);
  57. void reserve(size_t capacity);
  58. Vector<T> &operator=(const Vector<T> &v);
  59. private:
  60. size_t size_;
  61. size_t elem_size_;
  62. size_t capacity_;
  63. T *data_;
  64. };
  65. template <typename T>
  66. bool operator==(const Vector<T> &lhs, const Vector<T> &rhs) {
  67. if (lhs.size() != rhs.size()) {
  68. return false;
  69. }
  70. for (int i = 0; i < lhs.size(); ++i) {
  71. if (lhs[i] != rhs[i]) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. template <typename T>
  78. bool operator!=(const Vector<T> &lhs, const Vector<T> &rhs) {
  79. return !(lhs == rhs);
  80. }
  81. #endif // MINDSPORE_LITE_INTERNAL_INCLUDE_VECTOR_H