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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
//
// NamePool.cpp
//
// Library: XML
// Package: XML
// Module: NamePool
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/XML/NamePool.h"
#include "Poco/Exception.h"
#include "Poco/Random.h"
namespace Poco {
namespace XML {
class NamePoolItem
{
public:
NamePoolItem(): _used(false)
{
}
~NamePoolItem()
{
}
bool set(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName)
{
if (!_used)
{
_name.assign(qname, namespaceURI, localName);
_used = true;
return true;
}
else return _name.equals(qname, namespaceURI, localName);
}
const Name& get() const
{
return _name;
}
bool used() const
{
return _used;
}
private:
Name _name;
bool _used;
};
NamePool::NamePool(unsigned long size):
_size(size),
_salt(0),
_rc(1)
{
poco_assert (size > 1);
_pItems = new NamePoolItem[size];
Poco::Random rnd;
rnd.seed();
_salt = rnd.next();
}
NamePool::~NamePool()
{
delete [] _pItems;
}
void NamePool::duplicate()
{
++_rc;
}
void NamePool::release()
{
if (--_rc == 0)
delete this;
}
const Name& NamePool::insert(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName)
{
unsigned long i = 0;
unsigned long n = (hash(qname, namespaceURI, localName) ^ _salt) % _size;
while (!_pItems[n].set(qname, namespaceURI, localName) && i++ < _size)
n = (n + 1) % _size;
if (i > _size) throw Poco::PoolOverflowException("XML name pool");
return _pItems[n].get();
}
const Name& NamePool::insert(const Name& name)
{
return insert(name.qname(), name.namespaceURI(), name.localName());
}
unsigned long NamePool::hash(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName)
{
unsigned long h = 0;
XMLString::const_iterator it = qname.begin();
XMLString::const_iterator end = qname.end();
while (it != end) h = (h << 5) + h + (unsigned long) *it++;
it = namespaceURI.begin();
end = namespaceURI.end();
while (it != end) h = (h << 5) + h + (unsigned long) *it++;
it = localName.begin();
end = localName.end();
while (it != end) h = (h << 5) + h + (unsigned long) *it++;
return h;
}
} } // namespace Poco::XML
|