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