Re: [PR] [Picojson] Let the key of objects in json be ordered by default [tvm]

2024-04-10 Thread via GitHub


tqchen merged PR #16863:
URL: https://github.com/apache/tvm/pull/16863


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



Re: [PR] [Picojson] Let the key of objects in json be ordered by default [tvm]

2024-04-09 Thread via GitHub


tqchen commented on code in PR #16863:
URL: https://github.com/apache/tvm/pull/16863#discussion_r1558112101


##
3rdparty/picojson/picojson.h:
##
@@ -137,10 +146,177 @@ enum { INDENT_WIDTH = 2 };
 
 struct null {};
 
+// The ordered version of hashmap. When iterating through the hashmap,
+// the elements maintain the order in which they were inserted.
+// Its API is the same as std::unordered_map.
+template 
+class ordered_hashmap : private std::unordered_map {
+ public:
+  using value_type = std::pair;
+
+ private:
+  template 
+  struct iterator_base {
+   public:
+using pointer = std::conditional_t;
+using reference = std::conditional_t;
+
+iterator_base(const iterator_base& other)
+: order_index(other.order_index), obj_ptr(other.obj_ptr) {}
+
+template >
+iterator_base(const iterator_base& other)
+: order_index(other.order_index), obj_ptr(other.obj_ptr) {
+  static_assert(_IsConst, "This constructor should only be used for const 
iterators.");
+}
+
+iterator_base& operator++() {
+  ++order_index;
+  return *this;
+}
+iterator_base operator++(int) {
+  auto tmp = *this;
+  ++order_index;
+  return tmp;
+}
+pointer operator->() const {
+  assert(order_index >= 0 && order_index < obj_ptr->order.size());
+  return obj_ptr->std::unordered_map::find(obj_ptr->order[order_index]).operator->();
+}
+reference operator*() const { return *operator->(); }
+bool operator==(const iterator_base& other) const {
+  return order_index == other.order_index && obj_ptr == other.obj_ptr;
+}
+bool operator!=(const iterator_base& other) const { return 
!(*this == other); }
+
+friend class ordered_hashmap;
+
+   private:
+using obj_pointer_type = std::conditional_t;
+iterator_base(int order_index, obj_pointer_type obj_ptr)
+: order_index(order_index), obj_ptr(obj_ptr) {}
+
+int order_index;
+obj_pointer_type obj_ptr;
+  };
+
+ public:
+  using iterator = iterator_base;
+  using const_iterator = iterator_base;
+
+  ordered_hashmap() = default;
+  ordered_hashmap(const ordered_hashmap&) = default;
+  ordered_hashmap(ordered_hashmap&&) = default;
+  ordered_hashmap(std::initializer_list init) : 
std::unordered_map(init) {
+for (const auto& pair : init) {
+  order.push_back(pair.first);
+}
+  }
+  ordered_hashmap& operator=(const ordered_hashmap&) = default;
+  ordered_hashmap& operator=(ordered_hashmap&&) = default;
+
+  iterator begin() { return {0, this}; }
+  iterator end() { return {static_cast(order.size()), this}; }
+  const_iterator begin() const { return {0, this}; }
+  const_iterator end() const { return {static_cast(order.size()), this}; }
+  const_iterator cbegin() const { return {0, this}; }
+  const_iterator cend() const { return {static_cast(order.size()), this}; 
}
+
+  using std::unordered_map::empty;
+  using std::unordered_map::size;
+  using std::unordered_map::at;
+
+  T& operator[](const Key& key) {
+if (count(key) == 0) {

Review Comment:
   use `std::unordered_map`, so you only do lookup once



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



Re: [PR] [Picojson] Let the key of objects in json be ordered by default [tvm]

2024-04-09 Thread via GitHub


tqchen commented on code in PR #16863:
URL: https://github.com/apache/tvm/pull/16863#discussion_r1558112101


##
3rdparty/picojson/picojson.h:
##
@@ -137,10 +146,177 @@ enum { INDENT_WIDTH = 2 };
 
 struct null {};
 
+// The ordered version of hashmap. When iterating through the hashmap,
+// the elements maintain the order in which they were inserted.
+// Its API is the same as std::unordered_map.
+template 
+class ordered_hashmap : private std::unordered_map {
+ public:
+  using value_type = std::pair;
+
+ private:
+  template 
+  struct iterator_base {
+   public:
+using pointer = std::conditional_t;
+using reference = std::conditional_t;
+
+iterator_base(const iterator_base& other)
+: order_index(other.order_index), obj_ptr(other.obj_ptr) {}
+
+template >
+iterator_base(const iterator_base& other)
+: order_index(other.order_index), obj_ptr(other.obj_ptr) {
+  static_assert(_IsConst, "This constructor should only be used for const 
iterators.");
+}
+
+iterator_base& operator++() {
+  ++order_index;
+  return *this;
+}
+iterator_base operator++(int) {
+  auto tmp = *this;
+  ++order_index;
+  return tmp;
+}
+pointer operator->() const {
+  assert(order_index >= 0 && order_index < obj_ptr->order.size());
+  return obj_ptr->std::unordered_map::find(obj_ptr->order[order_index]).operator->();
+}
+reference operator*() const { return *operator->(); }
+bool operator==(const iterator_base& other) const {
+  return order_index == other.order_index && obj_ptr == other.obj_ptr;
+}
+bool operator!=(const iterator_base& other) const { return 
!(*this == other); }
+
+friend class ordered_hashmap;
+
+   private:
+using obj_pointer_type = std::conditional_t;
+iterator_base(int order_index, obj_pointer_type obj_ptr)
+: order_index(order_index), obj_ptr(obj_ptr) {}
+
+int order_index;
+obj_pointer_type obj_ptr;
+  };
+
+ public:
+  using iterator = iterator_base;
+  using const_iterator = iterator_base;
+
+  ordered_hashmap() = default;
+  ordered_hashmap(const ordered_hashmap&) = default;
+  ordered_hashmap(ordered_hashmap&&) = default;
+  ordered_hashmap(std::initializer_list init) : 
std::unordered_map(init) {
+for (const auto& pair : init) {
+  order.push_back(pair.first);
+}
+  }
+  ordered_hashmap& operator=(const ordered_hashmap&) = default;
+  ordered_hashmap& operator=(ordered_hashmap&&) = default;
+
+  iterator begin() { return {0, this}; }
+  iterator end() { return {static_cast(order.size()), this}; }
+  const_iterator begin() const { return {0, this}; }
+  const_iterator end() const { return {static_cast(order.size()), this}; }
+  const_iterator cbegin() const { return {0, this}; }
+  const_iterator cend() const { return {static_cast(order.size()), this}; 
}
+
+  using std::unordered_map::empty;
+  using std::unordered_map::size;
+  using std::unordered_map::at;
+
+  T& operator[](const Key& key) {
+if (count(key) == 0) {

Review Comment:
   use find, so you only do lookup once



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org