Changeset 9f77d98 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:19Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e65c9285
Parents:
82fd245
git-author:
Dzejrou <dzejrou@…> (2018-01-07 00:40:04)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:19)
Message:

cpp: implemented std::hash for primitive types

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/functional.hpp

    r82fd245 r9f77d98  
    3232#include <type_traits>
    3333
     34extern "C" {
     35#include <adt/hash.h>
     36}
     37
    3438namespace std
    3539{
     
    707711
    708712    template<class T>
    709     struct hash;
    710 
    711     template<>
    712     struct hash<bool>;
    713 
    714     template<>
    715     struct hash<char>;
    716 
    717     template<>
    718     struct hash<signed char>;
    719 
    720     template<>
    721     struct hash<unsigned char>;
    722 
    723     template<>
    724     struct hash<char16_t>;
    725 
    726     template<>
    727     struct hash<char32_t>;
    728 
    729     template<>
    730     struct hash<wchar_t>;
    731 
    732     template<>
    733     struct hash<short>;
    734 
    735     template<>
    736     struct hash<unsigned short>;
    737 
    738     template<>
    739     struct hash<int>;
    740 
    741     template<>
    742     struct hash<unsigned int>;
    743 
    744     template<>
    745     struct hash<long>;
    746 
    747     template<>
    748     struct hash<long long>;
    749 
    750     template<>
    751     struct hash<unsigned long>;
    752 
    753     template<>
    754     struct hash<unsigned long long>;
    755 
    756     template<>
    757     struct hash<float>;
     713    struct hash
     714    { /* DUMMY BODY */ };
     715
     716    template<>
     717    struct hash<bool>
     718    {
     719        size_t operator()(bool x) const noexcept
     720        {
     721            return hash_mix(static_cast<size_t>(x));
     722        }
     723
     724        using argument_type = bool;
     725        using result_type   = size_t;
     726    };
     727
     728    template<>
     729    struct hash<char>
     730    {
     731        size_t operator()(char x) const noexcept
     732        {
     733            return hash_mix(static_cast<size_t>(x));
     734        }
     735
     736        using argument_type = char;
     737        using result_type   = size_t;
     738    };
     739
     740    template<>
     741    struct hash<signed char>
     742    {
     743        size_t operator()(signed char x) const noexcept
     744        {
     745            return hash_mix(static_cast<size_t>(x));
     746        }
     747
     748        using argument_type = signed char;
     749        using result_type   = size_t;
     750    };
     751
     752    template<>
     753    struct hash<unsigned char>
     754    {
     755        size_t operator()(unsigned char x) const noexcept
     756        {
     757            return hash_mix(static_cast<size_t>(x));
     758        }
     759
     760        using argument_type = unsigned char;
     761        using result_type   = size_t;
     762    };
     763
     764    template<>
     765    struct hash<char16_t>
     766    {
     767        size_t operator()(char16_t x) const noexcept
     768        {
     769            return hash_mix(static_cast<size_t>(x));
     770        }
     771
     772        using argument_type = char16_t;
     773        using result_type   = size_t;
     774    };
     775
     776    template<>
     777    struct hash<char32_t>
     778    {
     779        size_t operator()(char32_t x) const noexcept
     780        {
     781            return hash_mix(static_cast<size_t>(x));
     782        }
     783
     784        using argument_type = char32_t;
     785        using result_type   = size_t;
     786    };
     787
     788    template<>
     789    struct hash<wchar_t>
     790    {
     791        size_t operator()(wchar_t x) const noexcept
     792        {
     793            return hash_mix(static_cast<size_t>(x));
     794        }
     795
     796        using argument_type = wchar_t;
     797        using result_type   = size_t;
     798    };
     799
     800    template<>
     801    struct hash<short>
     802    {
     803        size_t operator()(short x) const noexcept
     804        {
     805            return hash_mix(static_cast<size_t>(x));
     806        }
     807
     808        using argument_type = short;
     809        using result_type   = size_t;
     810    };
     811
     812    template<>
     813    struct hash<unsigned short>
     814    {
     815        size_t operator()(unsigned short x) const noexcept
     816        {
     817            return hash_mix(static_cast<size_t>(x));
     818        }
     819
     820        using argument_type = unsigned short;
     821        using result_type   = size_t;
     822    };
     823
     824    template<>
     825    struct hash<int>
     826    {
     827        size_t operator()(int x) const noexcept
     828        {
     829            return hash_mix(static_cast<size_t>(x));
     830        }
     831
     832        using argument_type = int;
     833        using result_type   = size_t;
     834    };
     835
     836    template<>
     837    struct hash<unsigned int>
     838    {
     839        size_t operator()(unsigned int x) const noexcept
     840        {
     841            return hash_mix(static_cast<size_t>(x));
     842        }
     843
     844        using argument_type = unsigned int;
     845        using result_type   = size_t;
     846    };
     847
     848    template<>
     849    struct hash<long>
     850    {
     851        size_t operator()(long x) const noexcept
     852        {
     853            return hash_mix(static_cast<size_t>(x));
     854        }
     855
     856        using argument_type = long;
     857        using result_type   = size_t;
     858    };
     859
     860    template<>
     861    struct hash<long long>
     862    {
     863        size_t operator()(long long x) const noexcept
     864        {
     865            return hash_mix(static_cast<size_t>(x));
     866        }
     867
     868        using argument_type = long long;
     869        using result_type   = size_t;
     870    };
     871
     872    template<>
     873    struct hash<unsigned long>
     874    {
     875        size_t operator()(unsigned long x) const noexcept
     876        {
     877            return hash_mix(static_cast<size_t>(x));
     878        }
     879
     880        using argument_type = unsigned long;
     881        using result_type   = size_t;
     882    };
     883
     884    template<>
     885    struct hash<unsigned long long>
     886    {
     887        size_t operator()(unsigned long long x) const noexcept
     888        {
     889            return hash_mix(static_cast<size_t>(x));
     890        }
     891
     892        using argument_type = unsigned long long;
     893        using result_type   = size_t;
     894    };
     895
     896    template<>
     897    struct hash<float>
     898    {
     899        size_t operator()(float x) const noexcept
     900        {
     901            return hash_mix(*(size_t*)(&x));
     902        }
     903
     904        using argument_type = float;
     905        using result_type   = size_t;
     906    };
    758907
    759908    template<>
    760909    struct hash<double>;
     910    {
     911        size_t operator()(double x) const noexcept
     912        {
     913            return hash_mix(*(size_t*)(&x));
     914        }
     915
     916        using argument_type = double;
     917        using result_type   = size_t;
     918    };
    761919
    762920    template<>
    763921    struct hash<long double>;
    764 
    765     template<class T>
    766     struct hash<T*>;
     922    {
     923        size_t operator()(long double x) const noexcept
     924        {
     925            return hash_mix(*(size_t*)(&x));
     926        }
     927
     928        using argument_type = long double;
     929        using result_type   = size_t;
     930    };
     931
     932    template<class T>
     933    struct hash<T*>
     934    {
     935        size_t operator()(T* x) const noexcept
     936        {
     937            return hash_mix(static_cast<size_t>(x));
     938        }
     939
     940        using argument_type = T*;
     941        using result_type   = size_t;
     942    };
    767943}
    768944
Note: See TracChangeset for help on using the changeset viewer.