source: doc/proposals/interned_string.h @ a032992

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a032992 was 5a89a2b, checked in by Aaron Moss <a3moss@…>, 5 years ago

Add interned_string code to proposals

  • Property mode set to 100644
File size: 1.6 KB
Line 
1#pragma once
2
3// Copyright (c) 2015 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in
6// the file "LICENCE" distributed with this repository.
7
8#include <functional>
9#include <string>
10#include <unordered_set>
11#include <utility>
12
13/// Keeps canonical copies of a std::string for quicker comparisons
14class interned_string {
15        /// Shared map of canonical string representations
16        static std::unordered_set< std::string > canonical;
17
18        /// Canonical representation of empty string
19        static const std::string* empty_string() {
20                static const std::string* mt = [](){
21                        return &*canonical.emplace( "" ).first;
22                }();
23                return mt;
24        }
25
26        /// Canonicalize string
27        template<typename S>
28        static const std::string* intern( S&& s ) {
29                return &*canonical.emplace( std::forward<S>(s) ).first;
30        }
31
32        /// Pointer to stored string
33        const std::string* s;
34       
35public:
36        interned_string() : s{empty_string()} {}
37        interned_string(const char* cs) : s{intern(cs)} {}
38        interned_string(const std::string& ss) : s{intern(ss)} {}
39
40        operator const std::string& () const { return *s; }
41
42        bool operator== (const interned_string& o) const { return s == o.s; }
43        bool operator!= (const interned_string& o) const { return s != o.s; }
44        bool operator< (const interned_string& o) const { return *s < *o.s; }
45};
46
47inline std::ostream& operator<< (std::ostream& out, const interned_string& s) {
48        return out << (const std::string&)s;
49}
50
51namespace std {
52        template<> struct hash<interned_string> {
53                std::size_t operator() (const interned_string& s) const {
54                        return std::hash<const std::string*>{}( &(const std::string&)s );
55                }
56        };
57}
Note: See TracBrowser for help on using the repository browser.