File: string_pool.hpp

package info (click to toggle)
liborcus 0.7.0%2Bdfsg-9
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,416 kB
  • ctags: 3,153
  • sloc: cpp: 18,133; sh: 11,224; xml: 2,836; makefile: 928; python: 351
file content (90 lines) | stat: -rw-r--r-- 2,171 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef __ORCUS_STRING_POOL_HPP__
#define __ORCUS_STRING_POOL_HPP__

#include <string>
#include <boost/unordered_set.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/functional/hash.hpp>

#include "env.hpp"
#include "pstring.hpp"

namespace orcus {

/**
 * Implements string hash map.
 */
class ORCUS_DLLPUBLIC string_pool
{
    struct string_hash
    {
        size_t operator() (const std::string* p) const;
    private:
        boost::hash<std::string> m_hash;
    };

    struct string_equal_to
    {
        bool operator() (const std::string* p1, const std::string* p2) const;
    private:
        std::equal_to<std::string> m_equal_to;
    };

    typedef boost::unordered_set<pstring, pstring::hash> string_set_type;
    typedef boost::ptr_vector<std::string> string_store_type;

public:
    string_pool();
    ~string_pool();

    /**
     * Intern a string.
     *
     * @param str string to intern.  It must be null-terminated.
     *
     * @return pair whose first value is the interned string, and the second
     *         value specifies whether it is a newly created instance (true)
     *         or a reuse of an existing instance (false).
     */
    std::pair<pstring, bool> intern(const char* str);

    /**
     * Intern a string.
     *
     *
     * @param str string to intern.  It doesn't need to be null-terminated.
     * @param n length of the string.
     *
     * @return see above.
     */
    std::pair<pstring, bool> intern(const char* str, size_t n);

    /**
     * Intern a string.
     *
     * @param str string to intern.
     *
     * @return see above.
     */
    std::pair<pstring, bool> intern(const pstring& str);

    void dump() const;
    void clear();
    size_t size() const;

private:
    string_set_type m_set;
    string_store_type m_store;
};

}

#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */