ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/include/ermyth/shstr.h
Revision: 1.3
Committed: Sun Sep 16 18:54:42 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +5 -5 lines
Log Message:
#defines to enum

File Contents

# Content
1 /**
2 * shstr.h: Shared string class.
3 *
4 * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5 * Rights to this code are as documented in doc/pod/gplicense.pod.
6 *
7 * $Id: shstr.h,v 1.2 2007-09-05 11:23:13 pippijn Exp $
8 */
9
10 #ifndef SHSTR_H
11 #define SHSTR_H
12
13 #include <string>
14 #include <vector>
15 #include <iostream>
16
17 #ifdef WANT_STRLEN
18 # define SHSTRLEN(str) strlen (str)
19 #else
20 # define SHSTRLEN(str) 0
21 #endif
22
23 struct shentry
24 {
25 shentry (char const * const str, const size_t hash)
26 : m_refcnt (0), m_str (str), m_len (SHSTRLEN (str)), m_hash (hash)
27 {
28 }
29
30 mutable int m_refcnt;
31 char const * const m_str;
32 size_t m_len;
33 size_t m_hash;
34 };
35
36 class shstr
37 {
38 protected:
39 static size_t m_cnt;
40
41 static size_t m_bucketcnt;
42
43 typedef std::vector<shentry *> pairs;
44
45 static pairs *m_buckets;
46
47 static shentry *prepare (char const * const str);
48 static bool unref (char const * const str);
49 static const shentry *get (char const * const str);
50
51 static pairs::iterator find (char const * const str, size_t n);
52
53 static size_t hash (char const * const str);
54
55 public:
56 static char const * const insert (char const * const str, size_t *size);
57
58 static void erase (char const * const str);
59
60 static size_t bucketcnt ()
61 {
62 return m_bucketcnt;
63 }
64
65 static size_t refs (char const * const str);
66 static size_t strcnt ();
67
68 static size_t memused ();
69
70 static void initbuckets (pairs *buckets)
71 {
72 m_buckets = buckets;
73 }
74
75 static void cleanup ()
76 {
77 delete[] m_buckets;
78 }
79
80 public:
81 typedef size_t size_type;
82
83 shstr (char const * const str = "");
84 shstr (const shstr &rhs);
85 ~shstr ();
86
87 char const * const c_str () const;
88 size_type length () const;
89 shstr &operator = (char const * const rhs);
90 shstr &operator = (const shstr &rhs);
91 bool operator == (const shstr &rhs) const;
92 bool operator != (const shstr &rhs) const;
93
94 friend std::ostream &operator << (std::ostream &os, const shstr &str)
95 {
96 os << str.m_str;
97 return os;
98 }
99
100 protected:
101 char const *m_str;
102 size_type m_len;
103
104 void erase ()
105 {
106 erase (m_str);
107 m_str = NULL;
108 }
109
110 void insert (char const * const newstr)
111 {
112 m_str = insert (newstr, &m_len);
113 }
114 };
115
116 #endif // SHSTR_H