1 | // |
2 | // NamePool.cpp |
3 | // |
4 | // Library: XML |
5 | // Package: XML |
6 | // Module: NamePool |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/XML/NamePool.h" |
16 | #include "Poco/Exception.h" |
17 | #include "Poco/Random.h" |
18 | |
19 | |
20 | namespace Poco { |
21 | namespace XML { |
22 | |
23 | |
24 | class NamePoolItem |
25 | { |
26 | public: |
27 | NamePoolItem(): _used(false) |
28 | { |
29 | } |
30 | |
31 | ~NamePoolItem() |
32 | { |
33 | } |
34 | |
35 | bool set(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) |
36 | { |
37 | if (!_used) |
38 | { |
39 | _name.assign(qname, namespaceURI, localName); |
40 | _used = true; |
41 | return true; |
42 | } |
43 | else return _name.equals(qname, namespaceURI, localName); |
44 | } |
45 | |
46 | const Name& get() const |
47 | { |
48 | return _name; |
49 | } |
50 | |
51 | bool used() const |
52 | { |
53 | return _used; |
54 | } |
55 | |
56 | private: |
57 | Name _name; |
58 | bool _used; |
59 | }; |
60 | |
61 | |
62 | NamePool::NamePool(unsigned long size): |
63 | _size(size), |
64 | _salt(0), |
65 | _rc(1) |
66 | { |
67 | poco_assert (size > 1); |
68 | |
69 | _pItems = new NamePoolItem[size]; |
70 | |
71 | Poco::Random rnd; |
72 | rnd.seed(); |
73 | _salt = rnd.next(); |
74 | } |
75 | |
76 | |
77 | NamePool::~NamePool() |
78 | { |
79 | delete [] _pItems; |
80 | } |
81 | |
82 | |
83 | void NamePool::duplicate() |
84 | { |
85 | ++_rc; |
86 | } |
87 | |
88 | |
89 | void NamePool::release() |
90 | { |
91 | if (--_rc == 0) |
92 | delete this; |
93 | } |
94 | |
95 | |
96 | const Name& NamePool::insert(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) |
97 | { |
98 | unsigned long i = 0; |
99 | unsigned long n = (hash(qname, namespaceURI, localName) ^ _salt) % _size; |
100 | |
101 | while (!_pItems[n].set(qname, namespaceURI, localName) && i++ < _size) |
102 | n = (n + 1) % _size; |
103 | |
104 | if (i > _size) throw Poco::PoolOverflowException("XML name pool" ); |
105 | |
106 | return _pItems[n].get(); |
107 | } |
108 | |
109 | |
110 | const Name& NamePool::insert(const Name& name) |
111 | { |
112 | return insert(name.qname(), name.namespaceURI(), name.localName()); |
113 | } |
114 | |
115 | |
116 | unsigned long NamePool::hash(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) |
117 | { |
118 | unsigned long h = 0; |
119 | XMLString::const_iterator it = qname.begin(); |
120 | XMLString::const_iterator end = qname.end(); |
121 | while (it != end) h = (h << 5) + h + (unsigned long) *it++; |
122 | it = namespaceURI.begin(); |
123 | end = namespaceURI.end(); |
124 | while (it != end) h = (h << 5) + h + (unsigned long) *it++; |
125 | it = localName.begin(); |
126 | end = localName.end(); |
127 | while (it != end) h = (h << 5) + h + (unsigned long) *it++; |
128 | return h; |
129 | } |
130 | |
131 | |
132 | } } // namespace Poco::XML |
133 | |