2 * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef KERNEL_TEAM_THREAD_TABLES_H
6 #define KERNEL_TEAM_THREAD_TABLES_H
9 #include <thread_types.h>
15 template<typename Element
>
16 struct TeamThreadTable
{
18 typedef typename
Element::id_type id_type
;
19 typedef typename
Element::iterator_type IteratorEntry
;
28 Iterator(IteratorEntry
* nextEntry
)
40 Element
* result
= fNext
;
42 _SetNext(result
->GetDoublyLinkedListLink()->next
);
48 void _SetNext(IteratorEntry
* entry
)
50 while (entry
!= NULL
) {
52 fNext
= static_cast<Element
*>(entry
);
56 entry
= entry
->GetDoublyLinkedListLink()->next
;
73 status_t
Init(size_t initialSize
)
75 return fTable
.Init(initialSize
);
78 void Insert(Element
* element
)
80 element
->serial_number
= fNextSerialNumber
++;
81 fTable
.InsertUnchecked(element
);
85 void Remove(Element
* element
)
87 fTable
.RemoveUnchecked(element
);
88 fList
.Remove(element
);
91 Element
* Lookup(id_type id
, bool visibleOnly
= true) const
93 Element
* element
= fTable
.Lookup(id
);
94 return element
!= NULL
&& (!visibleOnly
|| element
->visible
)
99 The iterator iterates through all, including invisible, entries!
101 Iterator
GetIterator()
103 return Iterator(fList
.Head());
106 void InsertIteratorEntry(IteratorEntry
* entry
)
110 entry
->visible
= false;
111 fList
.Add(entry
, false);
114 void RemoveIteratorEntry(IteratorEntry
* entry
)
119 Element
* NextElement(IteratorEntry
* entry
, bool visibleOnly
= true)
121 if (entry
== fList
.Tail())
124 IteratorEntry
* nextEntry
= entry
;
127 nextEntry
= nextEntry
->GetDoublyLinkedListLink()->next
;
128 if (nextEntry
== NULL
) {
129 // end of list -- requeue entry at the end and return NULL
135 if (nextEntry
->id
>= 0 && (!visibleOnly
|| nextEntry
->visible
)) {
136 // found an element -- requeue entry after element
137 Element
* element
= static_cast<Element
*>(nextEntry
);
139 fList
.InsertAfter(nextEntry
, entry
);
146 struct HashDefinition
{
147 typedef id_type KeyType
;
148 typedef Element ValueType
;
150 size_t HashKey(id_type key
) const
155 size_t Hash(Element
* value
) const
157 return HashKey(value
->id
);
160 bool Compare(id_type key
, Element
* value
) const
162 return value
->id
== key
;
165 Element
*& GetLink(Element
* value
) const
167 return value
->hash_next
;
171 typedef BOpenHashTable
<HashDefinition
> ElementTable
;
172 typedef DoublyLinkedList
<IteratorEntry
> List
;
177 int64 fNextSerialNumber
;
181 } // namespace BKernel
184 #endif // KERNEL_TEAM_THREAD_TABLES_H