namespace stl {
template <typename _Ty>
class allocator {
public:
static_assert(sizeof(_Ty) > 0, "The C++ Standard forbids containers of const elements "
"because allocator<const T> is ill-formed.");
typedef _Ty value_type;
typedef _Ty* pointer;
typedef const _Ty* const_pointer;
typedef _Ty& reference;
typedef const _Ty& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template<typename _Tp1>
struct rebind {
typedef allocator<_Tp1> other;
};
public:
template<typename _Tp1>
inline constexpr allocator(allocator<_Tp1> const&) noexcept {}
inline constexpr allocator() noexcept = default;
inline constexpr allocator(allocator const&) noexcept = default;
inline ~allocator() noexcept = default;
public:
inline constexpr pointer address(reference r) noexcept {
return (pointer)&reinterpret_cast<const char&>(r);
}
inline constexpr const_pointer address(const_reference r) noexcept {
return (const_pointer)&reinterpret_cast<const char&>(r);
}
public:
inline pointer allocate(size_type n, const void* = 0) noexcept {
return (_Ty*)Malloc(n * sizeof(_Ty));
}
inline void deallocate(pointer p, size_type n) noexcept {
Mfree(p);
}
public:
inline constexpr size_type max_size() const noexcept {
return static_cast<size_type>(-1) / sizeof(_Ty); // std::numeric_limits<size_type>::max();
}
public:
template <typename... Args>
inline void construct(pointer p, Args&&... args) noexcept {
new (p) _Ty(std::forward<Args>(args)...);
}
inline void destroy(pointer p) noexcept {
p->~_Ty();
}
public:
inline bool operator==(allocator const& right) noexcept {
const allocator* left = this;
return left == (allocator*)&reinterpret_cast<const char&>(right);
}
inline bool operator!=(allocator const& right) noexcept {
const allocator* left = this;
return left != (allocator*)&reinterpret_cast<const char&>(right);
}
};
template<typename _Ty, typename _Alloc = stl::allocator<_Ty> >
using basic_string = std::basic_string<_Ty, std::char_traits<_Ty>, _Alloc >;
using string = stl::basic_string<char>;
using wstring = stl::basic_string<wchar_t>; // GCC -finput-charset
using stringstream = std::basic_stringstream<char, std::char_traits<char>, stl::allocator<char> >;
template <typename _Ty>
using vector = std::vector<_Ty, stl::allocator<_Ty> >;
template <typename _Ty>
using list = std::list<_Ty, stl::allocator<_Ty> >;
template <typename _Ty>
using set = std::set<_Ty, stl::allocator<_Ty> >;
template <typename _Kty, typename _Ty, typename _Pr = std::less<_Kty> >
using map = std::map<_Kty, _Ty, _Pr, stl::allocator<std::pair<const _Kty, _Ty> > >;
template <class _Kty, class _Hasher = std::hash<_Kty>, class _Keyeq = std::equal_to<_Kty> >
using unordered_set = std::unordered_set<_Kty, _Hasher, _Keyeq, stl::allocator<_Kty> >;
template <class _Kty, class _Ty, class _Hasher = std::hash<_Kty>, class _Keyeq = std::equal_to<_Kty> >
using unordered_map = std::unordered_map<_Kty, _Ty, _Hasher, _Keyeq, stl::allocator<std::pair<const _Kty, _Ty> > >;
}
#ifndef _WIN32
namespace std {
template<>
struct hash<stl::string> : public std::__hash_base<size_t, stl::string> {
inline size_t operator()(const stl::string& s) const noexcept {
const char* p = s.data();
return GetHashCode(p, s.size());
}
};
template<>
struct hash<stl::wstring> : public std::__hash_base<size_t, stl::wstring> {
inline size_t operator()(const stl::wstring& s) const noexcept {
const char* p = (char*)s.data();
return GetHashCode(p, s.size() * sizeof(wchar_t));
}
};
}
#endif
inline std::string stdstr(const stl::string& v) noexcept {
return std::string(v.data(), v.size());
}
inline stl::string stlstr(const std::string& v) noexcept {
return stl::string(v.data(), v.size());
}
关于STL模板类自定义内存分配器
最新推荐文章于 2023-12-04 19:43:12 发布