1 | #include "rf_word.ih" |
---|
2 | |
---|
3 | namespace rftype |
---|
4 | { |
---|
5 | |
---|
6 | using namespace rfrt ; |
---|
7 | |
---|
8 | uintptr_t Word::ctor (uintptr_t _data) |
---|
9 | { |
---|
10 | Header* h = reinterpret_cast<Header*>(_data); |
---|
11 | h->ref_count++; |
---|
12 | return _data; |
---|
13 | } |
---|
14 | |
---|
15 | void Word::dtor (uintptr_t _data) |
---|
16 | { |
---|
17 | Header* h = reinterpret_cast<Header*>(_data); |
---|
18 | if (--(h->ref_count) == 0) allocator.deallocate(h); |
---|
19 | } |
---|
20 | |
---|
21 | bool Word::eq (uintptr_t _data1, uintptr_t _data2) |
---|
22 | { |
---|
23 | Header* h1 = reinterpret_cast<Header*>(_data1); |
---|
24 | Header* h2 = reinterpret_cast<Header*>(_data2); |
---|
25 | if (h1 == h2) return true; |
---|
26 | else { |
---|
27 | if (hash(_data1) != hash(_data2)) return false; |
---|
28 | else if (h1->length != h2->length) return false; |
---|
29 | else if (wmemcmp(h1->content, h2->content, h1->length) == 0) return true; |
---|
30 | else return false; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | int Word::compare (uintptr_t _data1, uintptr_t _data2) |
---|
35 | { |
---|
36 | Header* h1 = reinterpret_cast<Header*>(_data1); |
---|
37 | Header* h2 = reinterpret_cast<Header*>(_data2); |
---|
38 | if (h1 == h2) return 0; |
---|
39 | else { |
---|
40 | int r = wmemcmp(h1->content, h2->content, pxx_min(h1->length, h2->length)); |
---|
41 | if (r != 0) return r; |
---|
42 | else { |
---|
43 | if (h1->length == h2->length) return 0; |
---|
44 | else return h1->length > h2->length ? 1 : -1; |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | uint32_t Word::hash (uintptr_t _data) |
---|
50 | { |
---|
51 | Header* h = reinterpret_cast<Header*>(_data); |
---|
52 | if (h->hash == 0xffffffff) { |
---|
53 | // Should never occur, because we always compute hash for words |
---|
54 | h->hash = compute_hash(h->content, h->length * sizeof(wchar_t)); |
---|
55 | } |
---|
56 | return h->hash; |
---|
57 | } |
---|
58 | |
---|
59 | short_type_funcs_t Word::funcs = { |
---|
60 | Word::ctor, |
---|
61 | Word::dtor, |
---|
62 | Word::eq, |
---|
63 | Word::compare, |
---|
64 | Word::hash, |
---|
65 | Word::to_string |
---|
66 | }; |
---|
67 | |
---|
68 | } |
---|