Go to the documentation of this file.
25 #ifndef MXNET_RUNTIME_CONTAINER_EXT_H_
26 #define MXNET_RUNTIME_CONTAINER_EXT_H_
27 #include <dmlc/logging.h>
31 #include <string_view>
33 #include <initializer_list>
34 #include <type_traits>
37 #include <unordered_map>
74 using ContainerType = std::unordered_map<ObjectRef, ObjectRef, ObjectRefHash, ObjectRefEqual>;
80 using KVType = ContainerType::value_type;
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");
86 static constexpr
const char*
_type_key =
"MXNet.Map";
102 return data_.count(key);
110 return data_.at(key);
118 return data_.at(key);
122 return data_.begin();
126 return data_.begin();
142 return data_.find(key);
150 return data_.find(key);
157 data_.erase(position);
171 return make_object<MapObj>();
182 template <
typename IterType>
195 map_node->
data_[kv.first] = kv.second;
209 template <
typename,
typename,
typename,
typename>
222 template <
typename K,
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>
242 data_ = std::move(other.data_);
255 data_ = std::move(other.data_);
278 template <
typename IterType>
286 Map(std::initializer_list<std::pair<K, V>> init) {
293 template <
typename Hash,
typename Equal>
294 Map(
const std::unordered_map<K, V, Hash, Equal>& init) {
302 const V
at(
const K& key)
const {
303 return DowncastNoCheck<V>(GetMapObj()->
at(key));
311 return this->
at(key);
316 return n ==
nullptr ? 0 : n->
size();
321 return n ==
nullptr ? 0 : GetMapObj()->
count(key);
332 void Set(
const K& key,
const V& value) {
362 if (
data_.get() ==
nullptr) {
364 }
else if (!
data_.unique()) {
385 return itr == other.itr;
389 return itr != other.itr;
396 return std::make_pair(DowncastNoCheck<K>(kv.first), DowncastNoCheck<V>(kv.second));
414 template <
typename,
typename,
typename,
typename>
422 MapObj* GetMapObj()
const {
433 template <
typename K,
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>
438 for (
const auto& p : rhs) {
439 lhs.
Set(p.first, p.second);
441 return std::move(lhs);
454 static constexpr
const char*
_type_key =
"MXNet.String";
504 String(std::string other);
550 return memncmp(
data(), other.data(),
size(), other.size());
562 return memncmp(
data(), other,
size(), std::stold(other));
580 const auto* ptr =
get();
616 operator std::string()
const {
617 return std::string{
get()->data,
size()};
636 return std::hash<std::string_view>()(std::string_view(
data,
size));
652 static int memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count);
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);
691 explicit FromStd(std::string other) : data_container{other} {}
695 std::string data_container;
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);
708 String replace{std::move(other)};
709 data_.swap(replace.data_);
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);
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);
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);
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);
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);
874 out.write(input.
data(), input.
size());
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)
882 for (
size_t i = 0; i < lhs_count && i < rhs_count; ++i) {
888 if (lhs_count < rhs_count) {
890 }
else if (lhs_count > rhs_count) {
910 return String::memncmp(str_a->data, str_b->data, str_a->size, str_b->size) == 0;
919 #endif // MXNET_RUNTIME_CONTAINER_EXT_H_
const V at(const K &key) const
Read element from map.
Definition: container_ext.h:302
ContainerType data_
The real container storing data.
Definition: container_ext.h:208
namespace of mxnet
Definition: api_registry.h:33
const_iterator end() const
Definition: container_ext.h:133
size_t count(const K &key) const
Definition: container_ext.h:319
const Object * get() const
Definition: object.h:543
base class of all object containers.
Definition: object.h:151
iterator end() const
Definition: container_ext.h:341
uint64_t size
The length of the string object.
Definition: container_ext.h:451
Map(IterType begin, IterType end)
constructor from iterator
Definition: container_ext.h:279
int compare(const char *other) const
Compares this to other.
Definition: container_ext.h:561
iterator begin() const
Definition: container_ext.h:337
size_t operator()(const ObjectRef &a) const
Calculate the hash code of an ObjectRef.
Definition: container_ext.h:897
const std::pair< K, V > value_type
Definition: container_ext.h:377
V mapped_type
Definition: container_ext.h:229
int compare(const std::string &other) const
Compares this String object to other.
Definition: container_ext.h:549
A custom smart pointer for Object.
Definition: object.h:346
Map(std::initializer_list< std::pair< K, V >> init)
constructor from initializer list
Definition: container_ext.h:286
ContainerType::iterator iterator
Iterator class.
Definition: container_ext.h:76
A single argument value to PackedFunc. Containing both type_code and MXNetValue.
Definition: packed_func.h:480
size_t size() const
Return the length of the string.
Definition: container_ext.h:579
void erase(const K &key)
Definition: container_ext.h:349
String operator+(const String &lhs, const String &rhs)
Definition: container_ext.h:717
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
const_iterator begin() const
Definition: container_ext.h:125
static constexpr const char * _type_key
Definition: container_ext.h:454
size_t size() const
Definition: container_ext.h:314
std::unordered_map< ObjectRef, ObjectRef, ObjectRefHash, ObjectRefEqual > ContainerType
Type of the actual underlying container.
Definition: container_ext.h:74
String(const char *other)
Construct a new String object.
Definition: container_ext.h:511
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: container_ext.h:226
Map< K, V > Merge(Map< K, V > lhs, const Map< K, V > &rhs)
Merge two Maps.
Definition: container_ext.h:437
iterator()
Definition: container_ext.h:381
bool operator!=(const iterator &other) const
Compare iterators.
Definition: container_ext.h:388
iterator find(const K &key) const
Definition: container_ext.h:345
iterator operator++(int)
Suffix self increment.
Definition: container_ext.h:404
ContainerType::const_iterator const_iterator
Iterator class.
Definition: container_ext.h:78
T * get() const
Definition: object.h:401
Map(Map< K, V > &&other)
move constructor
Definition: container_ext.h:241
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:511
Map()
default constructor
Definition: container_ext.h:234
ContainerType::value_type KVType
Type of value stored in the hash map.
Definition: container_ext.h:80
iterator end()
Definition: container_ext.h:129
bool operator!=(const String &lhs, const std::string &rhs)
Definition: container_ext.h:853
const char * data() const
Return the data pointer.
Definition: container_ext.h:607
value_type reference
Definition: container_ext.h:379
Map(ObjectPtr< Object > n)
constructor from pointer
Definition: container_ext.h:271
String-aware ObjectRef equal functor.
Definition: container_ext.h:56
bool empty() const
Definition: container_ext.h:324
static constexpr const char * _type_key
Definition: container_ext.h:86
static void InsertMaybeReHash(const KVType &kv, ObjectPtr< Object > *map)
InsertMaybeReHash an entry into the given hash map.
Definition: container_ext.h:193
iterator begin()
Definition: container_ext.h:121
int compare(const String &other) const
Compares this String object to other.
Definition: container_ext.h:537
iterator find(const key_type &key)
Index value associated with a key.
Definition: container_ext.h:149
Map< K, V > & operator=(Map< K, V > &&other)
copy assign operator
Definition: container_ext.h:254
An object representing string moved from std::string.
Definition: container_ext.h:681
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:804
iterator & operator++()
Prefix self increment, e.g. ++iter.
Definition: container_ext.h:399
void erase(const iterator &position)
Erase the entry associated with the iterator.
Definition: container_ext.h:156
MXNET_DECLARE_FINAL_OBJECT_INFO(MapObj, Object)
String & operator=(std::string other)
Change the value the reference object points to.
Definition: container_ext.h:707
Runtime memory management.
FromStd(std::string other)
Construct a new FromStd object.
Definition: container_ext.h:691
reference operator*() const
De-reference iterators.
Definition: container_ext.h:394
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:575
int64_t difference_type
Definition: container_ext.h:376
ObjectRef hash functor.
Definition: object.h:620
bool operator<(const String &lhs, const std::string &rhs)
Definition: container_ext.h:748
Iterator of the hash map.
Definition: container_ext.h:373
bool operator>(const String &lhs, const std::string &rhs)
Definition: container_ext.h:769
Shared content of all specializations of hash map.
Definition: container_ext.h:67
const char * c_str() const
Returns a pointer to the char array in the string.
Definition: container_ext.h:570
static ObjectPtr< MapObj > Empty()
Create an empty container.
Definition: container_ext.h:170
An object representing string. It's POD type.
Definition: container_ext.h:445
@ kMXNetMap
Definition: object.h:57
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
Base class of all object reference.
Definition: object.h:500
Definition: optional.h:251
Map< K, V > & operator=(const Map< K, V > &other)
move assign operator
Definition: container_ext.h:263
String()
Construct an empty string.
Definition: container_ext.h:495
bool operator==(const String &lhs, const std::string &rhs)
Definition: container_ext.h:832
static ObjectPtr< Object > CreateFromRange(IterType first, IterType last)
Create the map using contents from the given iterators.
Definition: container_ext.h:183
bool operator<=(const String &lhs, const std::string &rhs)
Definition: container_ext.h:790
static size_t HashBytes(const char *data, size_t size)
Hash the binary bytes.
Definition: container_ext.h:633
pointer operator->() const =delete
De-reference iterators is not allowed.
const char * data
The pointer to string data.
Definition: container_ext.h:448
bool empty() const
Retun if the string is empty.
Definition: container_ext.h:598
value_type * pointer
Definition: container_ext.h:378
Map(const std::unordered_map< K, V, Hash, Equal > &init)
constructor from unordered_map
Definition: container_ext.h:294
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Check if the two ObjectRef are equal.
Definition: container_ext.h:904
std::ostream & operator<<(std::ostream &out, const String &input)
Definition: container_ext.h:873
size_t size() const
Number of elements in the MapObj.
Definition: container_ext.h:93
MXNET_DECLARE_FINAL_OBJECT_INFO(StringObj, Object)
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
friend String operator+(const String &lhs, const String &rhs)
Definition: container_ext.h:717
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
static constexpr const uint32_t _type_index
Definition: container_ext.h:453
Reference to string objects.
Definition: container_ext.h:490
void erase(const key_type &key)
Erase the entry associated with the key, do nothing if not exists.
Definition: container_ext.h:163
MXNET_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(String, ObjectRef, StringObj)
const_iterator find(const key_type &key) const
Index value associated with a key.
Definition: container_ext.h:141
MapObj * CopyOnWrite()
copy on write semantics Do nothing if current handle is the unique copy of the array....
Definition: container_ext.h:361
bool operator>=(const String &lhs, const std::string &rhs)
Definition: container_ext.h:811
std::bidirectional_iterator_tag iterator_category
Definition: container_ext.h:375
bool operator==(const iterator &other) const
Compare iterators.
Definition: container_ext.h:384
void Set(const K &key, const V &value)
set the Map.
Definition: container_ext.h:332
A managed object in MXNet runtime.
static ObjectPtr< MapObj > CopyFrom(MapObj *from)
Create an empty container with elements copying from another MapObj.
Definition: container_ext.h:202
size_t length() const
Return the length of the string.
Definition: container_ext.h:589
@ kMXNetString
Definition: object.h:58
String-aware ObjectRef hash functor.
Definition: container_ext.h:46
const V operator[](const K &key) const
Read element from map.
Definition: container_ext.h:310
K key_type
Definition: container_ext.h:228
static constexpr const uint32_t _type_index
Definition: container_ext.h:85
Map(const Map< K, V > &other)
copy constructor
Definition: container_ext.h:248