mxnet
container_ext.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 // Acknowledgement: This file originates from dgl
25 #ifndef MXNET_RUNTIME_CONTAINER_EXT_H_
26 #define MXNET_RUNTIME_CONTAINER_EXT_H_
27 #include <dmlc/logging.h>
28 #include <mxnet/runtime/memory.h>
29 #include <mxnet/runtime/object.h>
30 
31 #include <string_view>
32 #include <string>
33 #include <initializer_list>
34 #include <type_traits>
35 #include <utility>
36 #include <vector>
37 #include <unordered_map>
38 
39 namespace mxnet {
40 namespace runtime {
41 
42 // Forward declare MXNetArgValue
43 class MXNetArgValue;
44 
46 struct ObjectRefHash {
52  size_t operator()(const ObjectRef& a) const;
53 };
54 
63  bool operator()(const ObjectRef& a, const ObjectRef& b) const;
64 };
65 
67 class MapObj : public Object {
68  public:
74  using ContainerType = std::unordered_map<ObjectRef, ObjectRef, ObjectRefHash, ObjectRefEqual>;
76  using iterator = ContainerType::iterator;
78  using const_iterator = ContainerType::const_iterator;
80  using KVType = ContainerType::value_type;
81 
82  static_assert(std::is_standard_layout<KVType>::value, "KVType is not standard layout");
83  static_assert(sizeof(KVType) == 16 || sizeof(KVType) == 8, "sizeof(KVType) incorrect");
84 
85  static constexpr const uint32_t _type_index = runtime::TypeIndex::kMXNetMap;
86  static constexpr const char* _type_key = "MXNet.Map";
88 
93  size_t size() const {
94  return data_.size();
95  }
101  size_t count(const key_type& key) const {
102  return data_.count(key);
103  }
109  const mapped_type& at(const key_type& key) const {
110  return data_.at(key);
111  }
117  mapped_type& at(const key_type& key) {
118  return data_.at(key);
119  }
122  return data_.begin();
123  }
126  return data_.begin();
127  }
130  return data_.end();
131  }
133  const_iterator end() const {
134  return data_.end();
135  }
141  const_iterator find(const key_type& key) const {
142  return data_.find(key);
143  }
149  iterator find(const key_type& key) {
150  return data_.find(key);
151  }
156  void erase(const iterator& position) {
157  data_.erase(position);
158  }
163  void erase(const key_type& key) {
164  data_.erase(key);
165  }
171  return make_object<MapObj>();
172  }
173 
174  protected:
182  template <typename IterType>
183  static ObjectPtr<Object> CreateFromRange(IterType first, IterType last) {
184  ObjectPtr<MapObj> p = make_object<MapObj>();
185  p->data_ = ContainerType(first, last);
186  return p;
187  }
193  static void InsertMaybeReHash(const KVType& kv, ObjectPtr<Object>* map) {
194  MapObj* map_node = static_cast<MapObj*>(map->get());
195  map_node->data_[kv.first] = kv.second;
196  }
203  ObjectPtr<MapObj> p = make_object<MapObj>();
204  p->data_ = ContainerType(from->data_.begin(), from->data_.end());
205  return p;
206  }
209  template <typename, typename, typename, typename>
210  friend class Map;
211 };
212 
222 template <typename K,
223  typename V,
224  typename = typename std::enable_if<std::is_base_of<ObjectRef, K>::value>::type,
225  typename = typename std::enable_if<std::is_base_of<ObjectRef, V>::value>::type>
226 class Map : public ObjectRef {
227  public:
228  using key_type = K;
229  using mapped_type = V;
230  class iterator;
234  Map() {
235  data_ = MapObj::Empty();
236  }
241  Map(Map<K, V>&& other) {
242  data_ = std::move(other.data_);
243  }
248  Map(const Map<K, V>& other) : ObjectRef(other.data_) {}
255  data_ = std::move(other.data_);
256  return *this;
257  }
263  Map<K, V>& operator=(const Map<K, V>& other) {
264  data_ = other.data_;
265  return *this;
266  }
271  explicit Map(ObjectPtr<Object> n) : ObjectRef(n) {}
278  template <typename IterType>
279  Map(IterType begin, IterType end) {
281  }
286  Map(std::initializer_list<std::pair<K, V>> init) {
287  data_ = MapObj::CreateFromRange(init.begin(), init.end());
288  }
293  template <typename Hash, typename Equal>
294  Map(const std::unordered_map<K, V, Hash, Equal>& init) { // NOLINT(*)
295  data_ = MapObj::CreateFromRange(init.begin(), init.end());
296  }
302  const V at(const K& key) const {
303  return DowncastNoCheck<V>(GetMapObj()->at(key));
304  }
310  const V operator[](const K& key) const {
311  return this->at(key);
312  }
314  size_t size() const {
315  MapObj* n = GetMapObj();
316  return n == nullptr ? 0 : n->size();
317  }
319  size_t count(const K& key) const {
320  MapObj* n = GetMapObj();
321  return n == nullptr ? 0 : GetMapObj()->count(key);
322  }
324  bool empty() const {
325  return size() == 0;
326  }
332  void Set(const K& key, const V& value) {
333  CopyOnWrite();
335  }
337  iterator begin() const {
338  return iterator(GetMapObj()->begin());
339  }
341  iterator end() const {
342  return iterator(GetMapObj()->end());
343  }
345  iterator find(const K& key) const {
346  return iterator(GetMapObj()->find(key));
347  }
348 
349  void erase(const K& key) {
350  CopyOnWrite()->erase(key);
351  }
352 
362  if (data_.get() == nullptr) {
363  data_ = MapObj::Empty();
364  } else if (!data_.unique()) {
365  data_ = MapObj::CopyFrom(GetMapObj());
366  }
367  return GetMapObj();
368  }
371 
373  class iterator {
374  public:
375  using iterator_category = std::bidirectional_iterator_tag;
376  using difference_type = int64_t;
377  using value_type = const std::pair<K, V>;
378  using pointer = value_type*;
380 
381  iterator() : itr() {}
382 
384  bool operator==(const iterator& other) const {
385  return itr == other.itr;
386  }
388  bool operator!=(const iterator& other) const {
389  return itr != other.itr;
390  }
392  pointer operator->() const = delete;
395  auto& kv = *itr;
396  return std::make_pair(DowncastNoCheck<K>(kv.first), DowncastNoCheck<V>(kv.second));
397  }
400  ++itr;
401  return *this;
402  }
405  iterator copy = *this;
406  ++(*this);
407  return copy;
408  }
409 
410  private:
411  iterator(const MapObj::iterator& itr) // NOLINT(*)
412  : itr(itr) {}
413 
414  template <typename, typename, typename, typename>
415  friend class Map;
416 
417  MapObj::iterator itr;
418  };
419 
420  private:
422  MapObj* GetMapObj() const {
423  return static_cast<MapObj*>(data_.get());
424  }
425 };
426 
433 template <typename K,
434  typename V,
435  typename = typename std::enable_if<std::is_base_of<ObjectRef, K>::value>::type,
436  typename = typename std::enable_if<std::is_base_of<ObjectRef, V>::value>::type>
437 inline Map<K, V> Merge(Map<K, V> lhs, const Map<K, V>& rhs) {
438  for (const auto& p : rhs) {
439  lhs.Set(p.first, p.second);
440  }
441  return std::move(lhs);
442 }
443 
445 class StringObj : public Object {
446  public:
448  const char* data;
449 
451  uint64_t size;
452 
453  static constexpr const uint32_t _type_index = TypeIndex::kMXNetString;
454  static constexpr const char* _type_key = "MXNet.String";
456 
457  private:
459  class FromStd;
460 
461  friend class String;
462 };
463 
490 class String : public ObjectRef {
491  public:
495  String() : String(std::string()) {}
504  String(std::string other); // NOLINT(*)
505 
511  String(const char* other) // NOLINT(*)
512  : String(std::string(other)) {}
513 
520  inline String& operator=(std::string other);
521 
527  inline String& operator=(const char* other);
528 
537  int compare(const String& other) const {
538  return memncmp(data(), other.data(), size(), other.size());
539  }
540 
549  int compare(const std::string& other) const {
550  return memncmp(data(), other.data(), size(), other.size());
551  }
552 
561  int compare(const char* other) const {
562  return memncmp(data(), other, size(), std::stold(other));
563  }
564 
570  const char* c_str() const {
571  return get()->data;
572  }
573 
579  size_t size() const {
580  const auto* ptr = get();
581  return ptr->size;
582  }
583 
589  size_t length() const {
590  return size();
591  }
592 
598  bool empty() const {
599  return size() == 0;
600  }
601 
607  const char* data() const {
608  return get()->data;
609  }
610 
616  operator std::string() const {
617  return std::string{get()->data, size()};
618  }
619 
625  inline static bool CanConvertFrom(const MXNetArgValue& val);
626 
633  static size_t HashBytes(const char* data, size_t size) {
634  // This function falls back to string copy with c++11 compiler and is
635  // recommended to be compiled with c++14
636  return std::hash<std::string_view>()(std::string_view(data, size));
637  }
638 
640 
641  private:
652  static int memncmp(const char* lhs, const char* rhs, size_t lhs_count, size_t rhs_count);
653 
664  static String Concat(const char* lhs, size_t lhs_size, const char* rhs, size_t rhs_size) {
665  std::string ret(lhs, lhs_size);
666  ret.append(rhs, rhs_size);
667  return String(ret);
668  }
669 
670  // Overload + operator
671  friend String operator+(const String& lhs, const String& rhs);
672  friend String operator+(const String& lhs, const std::string& rhs);
673  friend String operator+(const std::string& lhs, const String& rhs);
674  friend String operator+(const String& lhs, const char* rhs);
675  friend String operator+(const char* lhs, const String& rhs);
676 
678 };
679 
682  public:
691  explicit FromStd(std::string other) : data_container{other} {}
692 
693  private:
695  std::string data_container;
696 
697  friend class String;
698 };
699 
700 inline String::String(std::string other) {
701  auto ptr = make_object<StringObj::FromStd>(std::move(other));
702  ptr->size = ptr->data_container.size();
703  ptr->data = ptr->data_container.data();
704  data_ = std::move(ptr);
705 }
706 
707 inline String& String::operator=(std::string other) {
708  String replace{std::move(other)};
709  data_.swap(replace.data_);
710  return *this;
711 }
712 
713 inline String& String::operator=(const char* other) {
714  return operator=(std::string(other));
715 }
716 
717 inline String operator+(const String& lhs, const String& rhs) {
718  size_t lhs_size = lhs.size();
719  size_t rhs_size = rhs.size();
720  return String::Concat(lhs.data(), lhs_size, rhs.data(), rhs_size);
721 }
722 
723 inline String operator+(const String& lhs, const std::string& rhs) {
724  size_t lhs_size = lhs.size();
725  size_t rhs_size = rhs.size();
726  return String::Concat(lhs.data(), lhs_size, rhs.data(), rhs_size);
727 }
728 
729 inline String operator+(const std::string& lhs, const String& rhs) {
730  size_t lhs_size = lhs.size();
731  size_t rhs_size = rhs.size();
732  return String::Concat(lhs.data(), lhs_size, rhs.data(), rhs_size);
733 }
734 
735 inline String operator+(const char* lhs, const String& rhs) {
736  size_t lhs_size = std::stold(lhs);
737  size_t rhs_size = rhs.size();
738  return String::Concat(lhs, lhs_size, rhs.data(), rhs_size);
739 }
740 
741 inline String operator+(const String& lhs, const char* rhs) {
742  size_t lhs_size = lhs.size();
743  size_t rhs_size = std::stold(rhs);
744  return String::Concat(lhs.data(), lhs_size, rhs, rhs_size);
745 }
746 
747 // Overload < operator
748 inline bool operator<(const String& lhs, const std::string& rhs) {
749  return lhs.compare(rhs) < 0;
750 }
751 
752 inline bool operator<(const std::string& lhs, const String& rhs) {
753  return rhs.compare(lhs) > 0;
754 }
755 
756 inline bool operator<(const String& lhs, const String& rhs) {
757  return lhs.compare(rhs) < 0;
758 }
759 
760 inline bool operator<(const String& lhs, const char* rhs) {
761  return lhs.compare(rhs) < 0;
762 }
763 
764 inline bool operator<(const char* lhs, const String& rhs) {
765  return rhs.compare(lhs) > 0;
766 }
767 
768 // Overload > operator
769 inline bool operator>(const String& lhs, const std::string& rhs) {
770  return lhs.compare(rhs) > 0;
771 }
772 
773 inline bool operator>(const std::string& lhs, const String& rhs) {
774  return rhs.compare(lhs) < 0;
775 }
776 
777 inline bool operator>(const String& lhs, const String& rhs) {
778  return lhs.compare(rhs) > 0;
779 }
780 
781 inline bool operator>(const String& lhs, const char* rhs) {
782  return lhs.compare(rhs) > 0;
783 }
784 
785 inline bool operator>(const char* lhs, const String& rhs) {
786  return rhs.compare(lhs) < 0;
787 }
788 
789 // Overload <= operator
790 inline bool operator<=(const String& lhs, const std::string& rhs) {
791  return lhs.compare(rhs) <= 0;
792 }
793 
794 inline bool operator<=(const std::string& lhs, const String& rhs) {
795  return rhs.compare(lhs) >= 0;
796 }
797 
798 inline bool operator<=(const String& lhs, const String& rhs) {
799  return lhs.compare(rhs) <= 0;
800 }
801 
802 inline bool operator<=(const String& lhs, const char* rhs) {
803  return lhs.compare(rhs) <= 0;
804 }
805 
806 inline bool operator<=(const char* lhs, const String& rhs) {
807  return rhs.compare(lhs) >= 0;
808 }
809 
810 // Overload >= operator
811 inline bool operator>=(const String& lhs, const std::string& rhs) {
812  return lhs.compare(rhs) >= 0;
813 }
814 
815 inline bool operator>=(const std::string& lhs, const String& rhs) {
816  return rhs.compare(lhs) <= 0;
817 }
818 
819 inline bool operator>=(const String& lhs, const String& rhs) {
820  return lhs.compare(rhs) >= 0;
821 }
822 
823 inline bool operator>=(const String& lhs, const char* rhs) {
824  return lhs.compare(rhs) >= 0;
825 }
826 
827 inline bool operator>=(const char* lhs, const String& rhs) {
828  return rhs.compare(rhs) <= 0;
829 }
830 
831 // Overload == operator
832 inline bool operator==(const String& lhs, const std::string& rhs) {
833  return lhs.compare(rhs) == 0;
834 }
835 
836 inline bool operator==(const std::string& lhs, const String& rhs) {
837  return rhs.compare(lhs) == 0;
838 }
839 
840 inline bool operator==(const String& lhs, const String& rhs) {
841  return lhs.compare(rhs) == 0;
842 }
843 
844 inline bool operator==(const String& lhs, const char* rhs) {
845  return lhs.compare(rhs) == 0;
846 }
847 
848 inline bool operator==(const char* lhs, const String& rhs) {
849  return rhs.compare(lhs) == 0;
850 }
851 
852 // Overload != operator
853 inline bool operator!=(const String& lhs, const std::string& rhs) {
854  return lhs.compare(rhs) != 0;
855 }
856 
857 inline bool operator!=(const std::string& lhs, const String& rhs) {
858  return rhs.compare(lhs) != 0;
859 }
860 
861 inline bool operator!=(const String& lhs, const String& rhs) {
862  return lhs.compare(rhs) != 0;
863 }
864 
865 inline bool operator!=(const String& lhs, const char* rhs) {
866  return lhs.compare(rhs) != 0;
867 }
868 
869 inline bool operator!=(const char* lhs, const String& rhs) {
870  return rhs.compare(lhs) != 0;
871 }
872 
873 inline std::ostream& operator<<(std::ostream& out, const String& input) {
874  out.write(input.data(), input.size());
875  return out;
876 }
877 
878 inline int String::memncmp(const char* lhs, const char* rhs, size_t lhs_count, size_t rhs_count) {
879  if (lhs == rhs && lhs_count == rhs_count)
880  return 0;
881 
882  for (size_t i = 0; i < lhs_count && i < rhs_count; ++i) {
883  if (lhs[i] < rhs[i])
884  return -1;
885  if (lhs[i] > rhs[i])
886  return 1;
887  }
888  if (lhs_count < rhs_count) {
889  return -1;
890  } else if (lhs_count > rhs_count) {
891  return 1;
892  } else {
893  return 0;
894  }
895 }
896 
897 inline size_t ObjectRefHash::operator()(const ObjectRef& a) const {
898  if (const auto* str = a.as<StringObj>()) {
899  return String::HashBytes(str->data, str->size);
900  }
901  return ObjectHash()(a);
902 }
903 
904 inline bool ObjectRefEqual::operator()(const ObjectRef& a, const ObjectRef& b) const {
905  if (a.same_as(b)) {
906  return true;
907  }
908  if (const auto* str_a = a.as<StringObj>()) {
909  if (const auto* str_b = b.as<StringObj>()) {
910  return String::memncmp(str_a->data, str_b->data, str_a->size, str_b->size) == 0;
911  }
912  }
913  return false;
914 }
915 
916 } // namespace runtime
917 } // namespace mxnet
918 
919 #endif // MXNET_RUNTIME_CONTAINER_EXT_H_
mxnet::runtime::Map::at
const V at(const K &key) const
Read element from map.
Definition: container_ext.h:302
mxnet::runtime::MapObj::data_
ContainerType data_
The real container storing data.
Definition: container_ext.h:208
mxnet
namespace of mxnet
Definition: api_registry.h:33
mxnet::runtime::MapObj::end
const_iterator end() const
Definition: container_ext.h:133
mxnet::runtime::Map::count
size_t count(const K &key) const
Definition: container_ext.h:319
mxnet::runtime::ObjectRef::get
const Object * get() const
Definition: object.h:543
mxnet::runtime::Object
base class of all object containers.
Definition: object.h:151
mxnet::runtime::Map::end
iterator end() const
Definition: container_ext.h:341
mxnet::runtime::StringObj::size
uint64_t size
The length of the string object.
Definition: container_ext.h:451
mxnet::runtime::Map::Map
Map(IterType begin, IterType end)
constructor from iterator
Definition: container_ext.h:279
mxnet::runtime::String::compare
int compare(const char *other) const
Compares this to other.
Definition: container_ext.h:561
mxnet::runtime::Map::begin
iterator begin() const
Definition: container_ext.h:337
mxnet::runtime::ObjectRefHash::operator()
size_t operator()(const ObjectRef &a) const
Calculate the hash code of an ObjectRef.
Definition: container_ext.h:897
mxnet::runtime::Map::iterator::value_type
const std::pair< K, V > value_type
Definition: container_ext.h:377
mxnet::runtime::Map::mapped_type
V mapped_type
Definition: container_ext.h:229
mxnet::runtime::String::compare
int compare(const std::string &other) const
Compares this String object to other.
Definition: container_ext.h:549
mxnet::runtime::ObjectPtr
A custom smart pointer for Object.
Definition: object.h:346
mxnet::runtime::Map::Map
Map(std::initializer_list< std::pair< K, V >> init)
constructor from initializer list
Definition: container_ext.h:286
mxnet::runtime::MapObj::iterator
ContainerType::iterator iterator
Iterator class.
Definition: container_ext.h:76
mxnet::runtime::MXNetArgValue
A single argument value to PackedFunc. Containing both type_code and MXNetValue.
Definition: packed_func.h:480
mxnet::runtime::String::size
size_t size() const
Return the length of the string.
Definition: container_ext.h:579
mxnet::runtime::Map::erase
void erase(const K &key)
Definition: container_ext.h:349
mxnet::runtime::operator+
String operator+(const String &lhs, const String &rhs)
Definition: container_ext.h:717
mxnet::runtime::MapObj::at
mapped_type & at(const key_type &key)
Index value associated with a key, throw exception if the key does not exist.
Definition: container_ext.h:117
mxnet::runtime::MapObj::begin
const_iterator begin() const
Definition: container_ext.h:125
mxnet::runtime::StringObj::_type_key
static constexpr const char * _type_key
Definition: container_ext.h:454
mxnet::runtime::Map::size
size_t size() const
Definition: container_ext.h:314
mxnet::runtime::MapObj::ContainerType
std::unordered_map< ObjectRef, ObjectRef, ObjectRefHash, ObjectRefEqual > ContainerType
Type of the actual underlying container.
Definition: container_ext.h:74
mxnet::runtime::String::String
String(const char *other)
Construct a new String object.
Definition: container_ext.h:511
mxnet::runtime::Map
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: container_ext.h:226
mxnet::runtime::Merge
Map< K, V > Merge(Map< K, V > lhs, const Map< K, V > &rhs)
Merge two Maps.
Definition: container_ext.h:437
mxnet::runtime::Map::iterator::iterator
iterator()
Definition: container_ext.h:381
mxnet::runtime::Map::iterator::operator!=
bool operator!=(const iterator &other) const
Compare iterators.
Definition: container_ext.h:388
mxnet::runtime::Map::find
iterator find(const K &key) const
Definition: container_ext.h:345
mxnet::runtime::Map::iterator::operator++
iterator operator++(int)
Suffix self increment.
Definition: container_ext.h:404
mxnet::runtime::MapObj::const_iterator
ContainerType::const_iterator const_iterator
Iterator class.
Definition: container_ext.h:78
mxnet::runtime::ObjectPtr::get
T * get() const
Definition: object.h:401
mxnet::runtime::Map::Map
Map(Map< K, V > &&other)
move constructor
Definition: container_ext.h:241
mxnet::runtime::ObjectRef::same_as
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:511
mxnet::runtime::Map::Map
Map()
default constructor
Definition: container_ext.h:234
mxnet::runtime::MapObj::KVType
ContainerType::value_type KVType
Type of value stored in the hash map.
Definition: container_ext.h:80
mxnet::runtime::MapObj::end
iterator end()
Definition: container_ext.h:129
mxnet::runtime::operator!=
bool operator!=(const String &lhs, const std::string &rhs)
Definition: container_ext.h:853
mxnet::runtime::String::data
const char * data() const
Return the data pointer.
Definition: container_ext.h:607
mxnet::runtime::Map::iterator::reference
value_type reference
Definition: container_ext.h:379
mxnet::runtime::Map::Map
Map(ObjectPtr< Object > n)
constructor from pointer
Definition: container_ext.h:271
mxnet::runtime::ObjectRefEqual
String-aware ObjectRef equal functor.
Definition: container_ext.h:56
mxnet::runtime::Map::empty
bool empty() const
Definition: container_ext.h:324
mxnet::runtime::MapObj::_type_key
static constexpr const char * _type_key
Definition: container_ext.h:86
mxnet::runtime::MapObj::InsertMaybeReHash
static void InsertMaybeReHash(const KVType &kv, ObjectPtr< Object > *map)
InsertMaybeReHash an entry into the given hash map.
Definition: container_ext.h:193
mxnet::runtime::MapObj::begin
iterator begin()
Definition: container_ext.h:121
mxnet::runtime::String::compare
int compare(const String &other) const
Compares this String object to other.
Definition: container_ext.h:537
mxnet::runtime::MapObj::find
iterator find(const key_type &key)
Index value associated with a key.
Definition: container_ext.h:149
mxnet::runtime::Map::operator=
Map< K, V > & operator=(Map< K, V > &&other)
copy assign operator
Definition: container_ext.h:254
mxnet::runtime::StringObj::FromStd
An object representing string moved from std::string.
Definition: container_ext.h:681
mxnet::runtime::ObjectRef::as
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:804
mxnet::runtime::Map::iterator::operator++
iterator & operator++()
Prefix self increment, e.g. ++iter.
Definition: container_ext.h:399
mxnet::runtime::MapObj::erase
void erase(const iterator &position)
Erase the entry associated with the iterator.
Definition: container_ext.h:156
mxnet::runtime::MapObj::MXNET_DECLARE_FINAL_OBJECT_INFO
MXNET_DECLARE_FINAL_OBJECT_INFO(MapObj, Object)
mxnet::runtime::String::operator=
String & operator=(std::string other)
Change the value the reference object points to.
Definition: container_ext.h:707
memory.h
Runtime memory management.
mxnet::runtime::StringObj::FromStd::FromStd
FromStd(std::string other)
Construct a new FromStd object.
Definition: container_ext.h:691
mxnet::runtime::Map::iterator::operator*
reference operator*() const
De-reference iterators.
Definition: container_ext.h:394
mxnet::runtime::ObjectRef::data_
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:575
mxnet::runtime::Map::iterator::difference_type
int64_t difference_type
Definition: container_ext.h:376
mxnet::runtime::ObjectHash
ObjectRef hash functor.
Definition: object.h:620
mxnet::runtime::operator<
bool operator<(const String &lhs, const std::string &rhs)
Definition: container_ext.h:748
mxnet::runtime::Map::iterator
Iterator of the hash map.
Definition: container_ext.h:373
mxnet::runtime::operator>
bool operator>(const String &lhs, const std::string &rhs)
Definition: container_ext.h:769
mxnet::runtime::MapObj
Shared content of all specializations of hash map.
Definition: container_ext.h:67
mxnet::runtime::String::c_str
const char * c_str() const
Returns a pointer to the char array in the string.
Definition: container_ext.h:570
mxnet::runtime::MapObj::Empty
static ObjectPtr< MapObj > Empty()
Create an empty container.
Definition: container_ext.h:170
mxnet::runtime::StringObj
An object representing string. It's POD type.
Definition: container_ext.h:445
mxnet::runtime::kMXNetMap
@ kMXNetMap
Definition: object.h:57
mxnet::runtime::String::CanConvertFrom
static bool CanConvertFrom(const MXNetArgValue &val)
Check if a MXNetArgValue can be converted to String, i.e. it can be std::string or String.
Definition: packed_func.h:1289
mxnet::runtime::ObjectRef
Base class of all object reference.
Definition: object.h:500
std
Definition: optional.h:251
mxnet::runtime::Map::operator=
Map< K, V > & operator=(const Map< K, V > &other)
move assign operator
Definition: container_ext.h:263
mxnet::runtime::String::String
String()
Construct an empty string.
Definition: container_ext.h:495
mxnet::runtime::operator==
bool operator==(const String &lhs, const std::string &rhs)
Definition: container_ext.h:832
mxnet::runtime::MapObj::CreateFromRange
static ObjectPtr< Object > CreateFromRange(IterType first, IterType last)
Create the map using contents from the given iterators.
Definition: container_ext.h:183
mxnet::runtime::operator<=
bool operator<=(const String &lhs, const std::string &rhs)
Definition: container_ext.h:790
mxnet::runtime::String::HashBytes
static size_t HashBytes(const char *data, size_t size)
Hash the binary bytes.
Definition: container_ext.h:633
mxnet::runtime::Map::iterator::operator->
pointer operator->() const =delete
De-reference iterators is not allowed.
mxnet::runtime::StringObj::data
const char * data
The pointer to string data.
Definition: container_ext.h:448
mxnet::runtime::String::empty
bool empty() const
Retun if the string is empty.
Definition: container_ext.h:598
mxnet::runtime::Map::iterator::pointer
value_type * pointer
Definition: container_ext.h:378
mxnet::runtime::Map::Map
Map(const std::unordered_map< K, V, Hash, Equal > &init)
constructor from unordered_map
Definition: container_ext.h:294
mxnet::runtime::ObjectRefEqual::operator()
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Check if the two ObjectRef are equal.
Definition: container_ext.h:904
mxnet::runtime::operator<<
std::ostream & operator<<(std::ostream &out, const String &input)
Definition: container_ext.h:873
mxnet::runtime::MapObj::size
size_t size() const
Number of elements in the MapObj.
Definition: container_ext.h:93
mxnet::runtime::StringObj::MXNET_DECLARE_FINAL_OBJECT_INFO
MXNET_DECLARE_FINAL_OBJECT_INFO(StringObj, Object)
mxnet::runtime::MapObj::count
size_t count(const key_type &key) const
Count the number of times a key exists in the hash map.
Definition: container_ext.h:101
mxnet::runtime::String::operator+
friend String operator+(const String &lhs, const String &rhs)
Definition: container_ext.h:717
mxnet::runtime::MapObj::at
const mapped_type & at(const key_type &key) const
Index value associated with a key, throw exception if the key does not exist.
Definition: container_ext.h:109
mxnet::runtime::StringObj::_type_index
static constexpr const uint32_t _type_index
Definition: container_ext.h:453
mxnet::runtime::String
Reference to string objects.
Definition: container_ext.h:490
mxnet::runtime::MapObj::erase
void erase(const key_type &key)
Erase the entry associated with the key, do nothing if not exists.
Definition: container_ext.h:163
mxnet::runtime::String::MXNET_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS
MXNET_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(String, ObjectRef, StringObj)
mxnet::runtime::MapObj::find
const_iterator find(const key_type &key) const
Index value associated with a key.
Definition: container_ext.h:141
mxnet::runtime::Map::CopyOnWrite
MapObj * CopyOnWrite()
copy on write semantics Do nothing if current handle is the unique copy of the array....
Definition: container_ext.h:361
mxnet::runtime::operator>=
bool operator>=(const String &lhs, const std::string &rhs)
Definition: container_ext.h:811
mxnet::runtime::Map::iterator::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition: container_ext.h:375
mxnet::runtime::Map::iterator::operator==
bool operator==(const iterator &other) const
Compare iterators.
Definition: container_ext.h:384
mxnet::runtime::Map::Set
void Set(const K &key, const V &value)
set the Map.
Definition: container_ext.h:332
object.h
A managed object in MXNet runtime.
mxnet::runtime::MapObj::CopyFrom
static ObjectPtr< MapObj > CopyFrom(MapObj *from)
Create an empty container with elements copying from another MapObj.
Definition: container_ext.h:202
mxnet::runtime::String::length
size_t length() const
Return the length of the string.
Definition: container_ext.h:589
mxnet::runtime::kMXNetString
@ kMXNetString
Definition: object.h:58
mxnet::runtime::ObjectRefHash
String-aware ObjectRef hash functor.
Definition: container_ext.h:46
mxnet::runtime::Map::operator[]
const V operator[](const K &key) const
Read element from map.
Definition: container_ext.h:310
mxnet::runtime::Map::key_type
K key_type
Definition: container_ext.h:228
mxnet::runtime::MapObj::_type_index
static constexpr const uint32_t _type_index
Definition: container_ext.h:85
mxnet::runtime::Map::Map
Map(const Map< K, V > &other)
copy constructor
Definition: container_ext.h:248