Skip to content

Commit 19f045b

Browse files
committed
DoublyLinkedLists split into two implementations absolute and relative.
Absolute implementation means there is a single global list. Relative implementation means there are many lists and each is relative to some element.
1 parent 2761349 commit 19f045b

14 files changed

+672
-42
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
namespace Platform::Collections::Methods::Lists
2+
{
3+
template <typename ...> class AbsoluteCircularDoublyLinkedListMethods;
4+
template <typename TElement> class AbsoluteCircularDoublyLinkedListMethods<TElement> : public AbsoluteDoublyLinkedListMethodsBase<TElement>
5+
{
6+
public: void AttachBefore(TElement baseElement, TElement newElement)
7+
{
8+
auto baseElementPrevious = this->GetPrevious(baseElement);
9+
this->SetPrevious(newElement, baseElementPrevious);
10+
this->SetNext(newElement, baseElement);
11+
if (baseElement == this->GetFirst())
12+
{
13+
this->SetFirst(newElement);
14+
}
15+
this->SetNext(baseElementPrevious, newElement);
16+
this->SetPrevious(baseElement, newElement);
17+
this->IncrementSize();
18+
}
19+
20+
public: void AttachAfter(TElement baseElement, TElement newElement)
21+
{
22+
auto baseElementNext = this->GetNext(baseElement);
23+
this->SetPrevious(newElement, baseElement);
24+
this->SetNext(newElement, baseElementNext);
25+
if (baseElement == this->GetLast())
26+
{
27+
this->SetLast(newElement);
28+
}
29+
this->SetPrevious(baseElementNext, newElement);
30+
this->SetNext(baseElement, newElement);
31+
this->IncrementSize();
32+
}
33+
34+
public: void AttachAsFirst(TElement element)
35+
{
36+
auto first = this->GetFirst();
37+
if (first == 0)
38+
{
39+
this->SetFirst(element);
40+
this->SetLast(element);
41+
this->SetPrevious(element, element);
42+
this->SetNext(element, element);
43+
this->IncrementSize();
44+
}
45+
else
46+
{
47+
this->AttachBefore(first, element);
48+
}
49+
}
50+
51+
public: void AttachAsLast(TElement element)
52+
{
53+
auto last = this->GetLast();
54+
if (last == 0)
55+
{
56+
this->AttachAsFirst(element);
57+
}
58+
else
59+
{
60+
this->AttachAfter(last, element);
61+
}
62+
}
63+
64+
public: void Detach(TElement element)
65+
{
66+
auto elementPrevious = this->GetPrevious(element);
67+
auto elementNext = this->GetNext(element);
68+
if (elementNext == element)
69+
{
70+
this->SetFirst(0);
71+
this->SetLast(0);
72+
}
73+
else
74+
{
75+
this->SetNext(elementPrevious, elementNext);
76+
this->SetPrevious(elementNext, elementPrevious);
77+
if (element == this->GetFirst())
78+
{
79+
this->SetFirst(elementNext);
80+
}
81+
if (element == this->GetLast())
82+
{
83+
this->SetLast(elementPrevious);
84+
}
85+
}
86+
this->SetPrevious(element, 0);
87+
this->SetNext(element, 0);
88+
this->DecrementSize();
89+
}
90+
};
91+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Platform::Collections::Methods::Lists
2+
{
3+
template <typename ...> class AbsoluteDoublyLinkedListMethodsBase;
4+
template <typename TElement> class AbsoluteDoublyLinkedListMethodsBase<TElement> : public DoublyLinkedListMethodsBase<TElement>
5+
{
6+
protected: virtual TElement GetFirst() = 0;
7+
8+
protected: virtual TElement GetLast() = 0;
9+
10+
protected: virtual TElement GetSize() = 0;
11+
12+
protected: virtual void SetFirst(TElement element) = 0;
13+
14+
protected: virtual void SetLast(TElement element) = 0;
15+
16+
protected: virtual void SetSize(TElement size) = 0;
17+
18+
protected: void IncrementSize() { this->SetSize(this->GetSize() + 1); }
19+
20+
protected: void DecrementSize() { this->SetSize(this->GetSize() - 1); }
21+
};
22+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace Platform::Collections::Methods::Lists
2+
{
3+
template <typename ...> class AbsoluteOpenDoublyLinkedListMethods;
4+
template <typename TElement> class AbsoluteOpenDoublyLinkedListMethods<TElement> : public AbsoluteDoublyLinkedListMethodsBase<TElement>
5+
{
6+
public: void AttachBefore(TElement baseElement, TElement newElement)
7+
{
8+
auto baseElementPrevious = this->GetPrevious(baseElement);
9+
this->SetPrevious(newElement, baseElementPrevious);
10+
this->SetNext(newElement, baseElement);
11+
if (baseElementPrevious == 0)
12+
{
13+
this->SetFirst(newElement);
14+
}
15+
else
16+
{
17+
this->SetNext(baseElementPrevious, newElement);
18+
}
19+
this->SetPrevious(baseElement, newElement);
20+
this->IncrementSize();
21+
}
22+
23+
public: void AttachAfter(TElement baseElement, TElement newElement)
24+
{
25+
auto baseElementNext = this->GetNext(baseElement);
26+
this->SetPrevious(newElement, baseElement);
27+
this->SetNext(newElement, baseElementNext);
28+
if (baseElementNext == 0)
29+
{
30+
this->SetLast(newElement);
31+
}
32+
else
33+
{
34+
this->SetPrevious(baseElementNext, newElement);
35+
}
36+
this->SetNext(baseElement, newElement);
37+
this->IncrementSize();
38+
}
39+
40+
public: void AttachAsFirst(TElement element)
41+
{
42+
auto first = this->GetFirst();
43+
if (first == 0)
44+
{
45+
this->SetFirst(element);
46+
this->SetLast(element);
47+
this->SetPrevious(element, 0);
48+
this->SetNext(element, 0);
49+
this->IncrementSize();
50+
}
51+
else
52+
{
53+
this->AttachBefore(first, element);
54+
}
55+
}
56+
57+
public: void AttachAsLast(TElement element)
58+
{
59+
auto last = this->GetLast();
60+
if (last == 0)
61+
{
62+
this->AttachAsFirst(element);
63+
}
64+
else
65+
{
66+
this->AttachAfter(last, element);
67+
}
68+
}
69+
70+
public: void Detach(TElement element)
71+
{
72+
auto elementPrevious = this->GetPrevious(element);
73+
auto elementNext = this->GetNext(element);
74+
if (elementPrevious == 0)
75+
{
76+
this->SetFirst(elementNext);
77+
}
78+
else
79+
{
80+
this->SetNext(elementPrevious, elementNext);
81+
}
82+
if (elementNext == 0)
83+
{
84+
this->SetLast(elementPrevious);
85+
}
86+
else
87+
{
88+
this->SetPrevious(elementNext, elementPrevious);
89+
}
90+
this->SetPrevious(element, 0);
91+
this->SetNext(element, 0);
92+
this->DecrementSize();
93+
}
94+
};
95+
}

cpp/Platform.Collections.Methods/Lists/DoublyLinkedListMethodsBase.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,12 @@
33
template <typename ...> class DoublyLinkedListMethodsBase;
44
template <typename TElement> class DoublyLinkedListMethodsBase<TElement> : public GenericCollectionMethodsBase<TElement>
55
{
6-
protected: virtual TElement GetFirst() = 0;
7-
8-
protected: virtual TElement GetLast() = 0;
9-
106
protected: virtual TElement GetPrevious(TElement element) = 0;
117

128
protected: virtual TElement GetNext(TElement element) = 0;
139

14-
protected: virtual TElement GetSize() = 0;
15-
16-
protected: virtual void SetFirst(TElement element) = 0;
17-
18-
protected: virtual void SetLast(TElement element) = 0;
19-
2010
protected: virtual void SetPrevious(TElement element, TElement previous) = 0;
2111

2212
protected: virtual void SetNext(TElement element, TElement next) = 0;
23-
24-
protected: virtual void SetSize(TElement size) = 0;
25-
26-
protected: void IncrementSize() { this->SetSize(this->GetSize() + 1); }
27-
28-
protected: void DecrementSize() { this->SetSize(this->GetSize() - 1); }
2913
};
3014
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
namespace Platform::Collections::Methods::Lists
2+
{
3+
template <typename ...> class RelativeCircularDoublyLinkedListMethods;
4+
template <typename TElement> class RelativeCircularDoublyLinkedListMethods<TElement> : public RelativeDoublyLinkedListMethodsBase<TElement>
5+
{
6+
public: void AttachBefore(TElement headElement, TElement baseElement, TElement newElement)
7+
{
8+
auto baseElementPrevious = this->GetPrevious(baseElement);
9+
this->SetPrevious(newElement, baseElementPrevious);
10+
this->SetNext(newElement, baseElement);
11+
if (baseElement == this->GetFirst(headElement))
12+
{
13+
this->SetFirst(headElement, newElement);
14+
}
15+
this->SetNext(baseElementPrevious, newElement);
16+
this->SetPrevious(baseElement, newElement);
17+
this->IncrementSize(headElement);
18+
}
19+
20+
public: void AttachAfter(TElement headElement, TElement baseElement, TElement newElement)
21+
{
22+
auto baseElementNext = this->GetNext(baseElement);
23+
this->SetPrevious(newElement, baseElement);
24+
this->SetNext(newElement, baseElementNext);
25+
if (baseElement == this->GetLast(headElement))
26+
{
27+
this->SetLast(headElement, newElement);
28+
}
29+
this->SetPrevious(baseElementNext, newElement);
30+
this->SetNext(baseElement, newElement);
31+
this->IncrementSize(headElement);
32+
}
33+
34+
public: void AttachAsFirst(TElement headElement, TElement element)
35+
{
36+
auto first = this->GetFirst(headElement);
37+
if (first == 0)
38+
{
39+
this->SetFirst(headElement, element);
40+
this->SetLast(headElement, element);
41+
this->SetPrevious(element, element);
42+
this->SetNext(element, element);
43+
this->IncrementSize(headElement);
44+
}
45+
else
46+
{
47+
this->AttachBefore(headElement, first, element);
48+
}
49+
}
50+
51+
public: void AttachAsLast(TElement headElement, TElement element)
52+
{
53+
auto last = this->GetLast(headElement);
54+
if (last == 0)
55+
{
56+
this->AttachAsFirst(headElement, element);
57+
}
58+
else
59+
{
60+
this->AttachAfter(headElement, last, element);
61+
}
62+
}
63+
64+
public: void Detach(TElement headElement, TElement element)
65+
{
66+
auto elementPrevious = this->GetPrevious(element);
67+
auto elementNext = this->GetNext(element);
68+
if (elementNext == element)
69+
{
70+
this->SetFirst(headElement, 0);
71+
this->SetLast(headElement, 0);
72+
}
73+
else
74+
{
75+
this->SetNext(elementPrevious, elementNext);
76+
this->SetPrevious(elementNext, elementPrevious);
77+
if (element == this->GetFirst(headElement))
78+
{
79+
this->SetFirst(headElement, elementNext);
80+
}
81+
if (element == this->GetLast(headElement))
82+
{
83+
this->SetLast(headElement, elementPrevious);
84+
}
85+
}
86+
this->SetPrevious(element, 0);
87+
this->SetNext(element, 0);
88+
this->DecrementSize(headElement);
89+
}
90+
};
91+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Platform::Collections::Methods::Lists
2+
{
3+
template <typename ...> class RelativeDoublyLinkedListMethodsBase;
4+
template <typename TElement> class RelativeDoublyLinkedListMethodsBase<TElement> : public DoublyLinkedListMethodsBase<TElement>
5+
{
6+
protected: virtual TElement GetFirst(TElement headElement) = 0;
7+
8+
protected: virtual TElement GetLast(TElement headElement) = 0;
9+
10+
protected: virtual TElement GetSize(TElement headElement) = 0;
11+
12+
protected: virtual void SetFirst(TElement headElement, TElement element) = 0;
13+
14+
protected: virtual void SetLast(TElement headElement, TElement element) = 0;
15+
16+
protected: virtual void SetSize(TElement headElement, TElement size) = 0;
17+
18+
protected: void IncrementSize(TElement headElement) { this->SetSize(headElement, this->GetSize(headElement) + 1); }
19+
20+
protected: void DecrementSize(TElement headElement) { this->SetSize(headElement, this->GetSize(headElement) - 1); }
21+
};
22+
}

0 commit comments

Comments
 (0)