Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving linked list handling of Page into a separate class
- Loading branch information
1 parent
97d778e
commit a94da54
Showing
12 changed files
with
332 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright (C) 2005-2015 Christoph Rupp (chris@crupp.de). | ||
* All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* See the file COPYING for License information. | ||
*/ | ||
|
||
/* | ||
* An intrusive list | ||
* | ||
* usage: | ||
* | ||
* struct P { | ||
* IntrusiveListNode<P> list_node; // don't change that name! | ||
* }; | ||
* | ||
* IntrusiveList<P> list; | ||
* | ||
* P p; | ||
* list.put(&p); | ||
* list.del(&p); | ||
* assert(list.has(&p)); | ||
* | ||
* If |P| should be a member in multiple lists: | ||
* | ||
* struct P { | ||
* IntrusiveListNode<P, 3> list_node; // for 3 lists | ||
* }; | ||
* | ||
* IntrusiveList<P, 0> list1; | ||
* IntrusiveList<P, 1> list2; | ||
* IntrusiveList<P, 2> list3; | ||
*/ | ||
|
||
#ifndef UPS_INTRUSIVE_LIST_H | ||
#define UPS_INTRUSIVE_LIST_H | ||
|
||
#include "0root/root.h" | ||
#include "1base/uncopyable.h" | ||
|
||
// Always verify that a file of level N does not include headers > N! | ||
|
||
#ifndef UPS_ROOT_H | ||
# error "root.h was not included" | ||
#endif | ||
|
||
namespace upscaledb { | ||
|
||
template<typename T, int I = 1> | ||
struct IntrusiveListNode : public Uncopyable { | ||
IntrusiveListNode() { | ||
for (int i = 0; i < I; i++) | ||
previous[i] = next[i] = 0; | ||
} | ||
|
||
T *previous[I]; | ||
T *next[I]; | ||
}; | ||
|
||
template<typename T, int I = 0> | ||
struct IntrusiveList { | ||
IntrusiveList() | ||
: head_(0), tail_(0), size_(0) { | ||
} | ||
|
||
T *head() const { | ||
return head_; | ||
} | ||
|
||
T *tail() const { | ||
return tail_; | ||
} | ||
|
||
bool is_empty() const { | ||
return size_ == 0; | ||
} | ||
|
||
size_t size() const { | ||
return size_; | ||
} | ||
|
||
void put(T *t) { | ||
t->list_node.previous[I] = 0; | ||
t->list_node.next[I] = 0; | ||
if (head_) { | ||
t->list_node.next[I] = head_; | ||
head_->list_node.previous[I] = t; | ||
} | ||
head_ = t; | ||
if (!tail_) | ||
tail_ = t; | ||
size_++; | ||
} | ||
|
||
void del(T *t) { | ||
assert(has(t)); | ||
|
||
if (t == tail_) | ||
tail_ = t->list_node.previous[I]; | ||
if (t == head_) { | ||
T *next = head_->list_node.next[I]; | ||
if (next) | ||
next->list_node.previous[I] = 0; | ||
head_ = next; | ||
} | ||
else { | ||
T *next = t->list_node.next[I]; | ||
T *prev = t->list_node.previous[I]; | ||
if (prev) | ||
prev->list_node.next[I] = next; | ||
if (next) | ||
next->list_node.previous[I] = prev; | ||
} | ||
t->list_node.next[I] = 0; | ||
t->list_node.previous[I] = 0; | ||
size_--; | ||
} | ||
|
||
bool has(const T *t) const { | ||
return t->list_node.previous[I] != 0 | ||
|| t->list_node.next[I] != 0 | ||
|| t == head_; | ||
} | ||
|
||
T *head_; | ||
T *tail_; | ||
size_t size_; | ||
}; | ||
|
||
} // namespace upscaledb | ||
|
||
#endif // UPS_INTRUSIVE_LIST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2005-2015 Christoph Rupp (chris@crupp.de). | ||
* All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* See the file COPYING for License information. | ||
*/ | ||
|
||
#ifndef UPS_UNCOPYABLE_H | ||
#define UPS_UNCOPYABLE_H | ||
|
||
#include "0root/root.h" | ||
|
||
// Always verify that a file of level N does not include headers > N! | ||
|
||
#ifndef UPS_ROOT_H | ||
# error "root.h was not included" | ||
#endif | ||
|
||
namespace upscaledb { | ||
|
||
class Uncopyable { | ||
public: | ||
Uncopyable() { | ||
} | ||
|
||
private: | ||
Uncopyable(const Uncopyable&); | ||
Uncopyable& operator=(const Uncopyable&); | ||
}; | ||
|
||
} // namespace upscaledb | ||
|
||
#endif // UPS_UNCOPYABLE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.