source: mainline/uspace/lib/cpp/include/internal/hash_table_bucket.hpp@ 614b07e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 614b07e was 9751280, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: divided the hash table sources to multiple headers for better readability and shareability with rbtree in the case of key extractors

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_INTERNAL_HASH_TABLE_BUCKET
30#define LIBCPP_INTERNAL_HASH_TABLE_BUCKET
31
32#include <internal/list.hpp>
33
34namespace std::aux
35{
36 template<class Value, class Size>
37 struct hash_table_bucket
38 {
39 /**
40 * Note: We use a doubly linked list because
41 * we need to use hints, which point to the
42 * element after the hinted spot.
43 */
44
45 list_node<Value>* head;
46
47 hash_table_bucket()
48 : head{}
49 { /* DUMMY BODY */ }
50
51 Size size() const noexcept
52 {
53 auto current = head;
54 Size res{};
55
56 do
57 {
58 ++res;
59 current = current->next;
60 }
61 while (current != head);
62
63 return res;
64 }
65
66 void append(list_node<Value>* node)
67 {
68 if (!head)
69 head = node;
70 else
71 head->append(node);
72 }
73
74 void prepend(list_node<Value>* node)
75 {
76 if (!head)
77 head = node;
78 else
79 head->prepend(node);
80 }
81
82 void clear()
83 {
84 if (!head)
85 return;
86
87 auto current = head;
88 do
89 {
90 auto tmp = current;
91 current = current->next;
92 delete tmp;
93 }
94 while (current != head);
95
96 head = nullptr;
97 }
98
99 ~hash_table_bucket()
100 {
101 clear();
102 }
103 };
104}
105
106#endif
Note: See TracBrowser for help on using the repository browser.