1+ using System . Runtime . CompilerServices ;
2+ using Platform . Numbers ;
3+ using static System . Runtime . CompilerServices . Unsafe ;
4+
5+ #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
6+
7+ namespace Platform . Data . Doublets . ResizableDirectMemory
8+ {
9+ public unsafe class LinksSourcesAVLBalancedTreeMethods < TLink > : LinksAVLBalancedTreeMethodsBase < TLink > , ILinksTreeMethods < TLink >
10+ {
11+ public LinksSourcesAVLBalancedTreeMethods ( ResizableDirectMemoryLinks < TLink > memory , byte * links , byte * header ) : base ( memory , links , header ) { }
12+
13+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
14+ protected unsafe override ref TLink GetLeftReference ( TLink node ) => ref AsRef < TLink > ( ( void * ) ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . LeftAsSourceOffset ) ) ;
15+
16+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
17+ protected unsafe override ref TLink GetRightReference ( TLink node ) => ref AsRef < TLink > ( ( void * ) ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . RightAsSourceOffset ) ) ;
18+
19+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
20+ protected override TLink GetLeft ( TLink node ) => Read < TLink > ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . LeftAsSourceOffset ) ;
21+
22+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
23+ protected override TLink GetRight ( TLink node ) => Read < TLink > ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . RightAsSourceOffset ) ;
24+
25+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
26+ protected override TLink GetSize ( TLink node )
27+ {
28+ var previousValue = Read < TLink > ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ) ;
29+ return Bit < TLink > . PartialRead ( previousValue , 5 , - 5 ) ;
30+ }
31+
32+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
33+ protected override void SetLeft ( TLink node , TLink left ) => Write ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . LeftAsSourceOffset , left ) ;
34+
35+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
36+ protected override void SetRight ( TLink node , TLink right ) => Write ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . RightAsSourceOffset , right ) ;
37+
38+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
39+ protected override void SetSize ( TLink node , TLink size )
40+ {
41+ var linkSizeAsSourceOffset = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ;
42+ var previousValue = Read < TLink > ( linkSizeAsSourceOffset ) ;
43+ Write ( linkSizeAsSourceOffset , Bit < TLink > . PartialWrite ( previousValue , size , 5 , - 5 ) ) ;
44+ }
45+
46+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
47+ protected override bool GetLeftIsChild ( TLink node )
48+ {
49+ var previousValue = Read < TLink > ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ) ;
50+ //return (Integer<TLink>)Bit<TLink>.PartialRead(previousValue, 4, 1);
51+ return ! EqualityComparer . Equals ( Bit < TLink > . PartialRead ( previousValue , 4 , 1 ) , default ) ;
52+ }
53+
54+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
55+ protected override void SetLeftIsChild ( TLink node , bool value )
56+ {
57+ var linkSizeAsSourceOffset = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ;
58+ var previousValue = Read < TLink > ( linkSizeAsSourceOffset ) ;
59+ var modified = Bit < TLink > . PartialWrite ( previousValue , ( Integer < TLink > ) value , 4 , 1 ) ;
60+ Write ( linkSizeAsSourceOffset , modified ) ;
61+ }
62+
63+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
64+ protected override bool GetRightIsChild ( TLink node )
65+ {
66+ var previousValue = Read < TLink > ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ) ;
67+ //return (Integer<TLink>)Bit<TLink>.PartialRead(previousValue, 3, 1);
68+ return ! EqualityComparer . Equals ( Bit < TLink > . PartialRead ( previousValue , 3 , 1 ) , default ) ;
69+ }
70+
71+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
72+ protected override void SetRightIsChild ( TLink node , bool value )
73+ {
74+ var linkSizeAsSourceOffset = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ;
75+ var previousValue = Read < TLink > ( linkSizeAsSourceOffset ) ;
76+ var modified = Bit < TLink > . PartialWrite ( previousValue , ( Integer < TLink > ) value , 3 , 1 ) ;
77+ Write ( linkSizeAsSourceOffset , modified ) ;
78+ }
79+
80+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
81+ protected override sbyte GetBalance ( TLink node )
82+ {
83+ unchecked
84+ {
85+ var previousValue = Read < TLink > ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ) ;
86+ var value = ( int ) ( Integer < TLink > ) Bit < TLink > . PartialRead ( previousValue , 0 , 3 ) ;
87+ value |= 0xF8 * ( ( value & 4 ) >> 2 ) ; // if negative, then continue ones to the end of sbyte
88+ return ( sbyte ) value ;
89+ }
90+ }
91+
92+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
93+ protected override void SetBalance ( TLink node , sbyte value )
94+ {
95+ var linkSizeAsSourceOffset = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node + RawLink < TLink > . SizeAsSourceOffset ;
96+ var previousValue = Read < TLink > ( linkSizeAsSourceOffset ) ;
97+ var packagedValue = ( TLink ) ( Integer < TLink > ) ( ( ( ( byte ) value >> 5 ) & 4 ) | value & 3 ) ;
98+ var modified = Bit < TLink > . PartialWrite ( previousValue , packagedValue , 0 , 3 ) ;
99+ Write ( linkSizeAsSourceOffset , modified ) ;
100+ }
101+
102+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
103+ protected override bool FirstIsToTheLeftOfSecond ( TLink first , TLink second )
104+ {
105+ var firstLink = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) first ;
106+ var secondLink = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) second ;
107+ var firstSource = Read < TLink > ( firstLink + RawLink < TLink > . SourceOffset ) ;
108+ var secondSource = Read < TLink > ( secondLink + RawLink < TLink > . SourceOffset ) ;
109+ return LessThan ( firstSource , secondSource ) ||
110+ ( IsEquals ( firstSource , secondSource ) && LessThan ( Read < TLink > ( firstLink + RawLink < TLink > . TargetOffset ) , Read < TLink > ( secondLink + RawLink < TLink > . TargetOffset ) ) ) ;
111+ }
112+
113+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
114+ protected override bool FirstIsToTheRightOfSecond ( TLink first , TLink second )
115+ {
116+ var firstLink = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) first ;
117+ var secondLink = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) second ;
118+ var firstSource = Read < TLink > ( firstLink + RawLink < TLink > . SourceOffset ) ;
119+ var secondSource = Read < TLink > ( secondLink + RawLink < TLink > . SourceOffset ) ;
120+ return GreaterThan ( firstSource , secondSource ) ||
121+ ( IsEquals ( firstSource , secondSource ) && GreaterThan ( Read < TLink > ( firstLink + RawLink < TLink > . TargetOffset ) , Read < TLink > ( secondLink + RawLink < TLink > . TargetOffset ) ) ) ;
122+ }
123+
124+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
125+ protected override TLink GetTreeRoot ( ) => Read < TLink > ( Header + LinksHeader < TLink > . FirstAsSourceOffset ) ;
126+
127+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
128+ protected override TLink GetBasePartValue ( TLink link ) => Read < TLink > ( Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) link + RawLink < TLink > . SourceOffset ) ;
129+
130+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
131+ protected override bool FirstIsToTheLeftOfSecond ( TLink firstSource , TLink firstTarget , TLink secondSource , TLink secondTarget ) => LessThan ( firstSource , secondSource ) || ( IsEquals ( firstSource , secondSource ) && LessThan ( firstTarget , secondTarget ) ) ;
132+
133+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
134+ protected override bool FirstIsToTheRightOfSecond ( TLink firstSource , TLink firstTarget , TLink secondSource , TLink secondTarget ) => GreaterThan ( firstSource , secondSource ) || ( IsEquals ( firstSource , secondSource ) && GreaterThan ( firstTarget , secondTarget ) ) ;
135+
136+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
137+ protected override void ClearNode ( TLink node )
138+ {
139+ byte * link = Links + RawLink < TLink > . SizeInBytes * ( Integer < TLink > ) node ;
140+ Write ( link + RawLink < TLink > . LeftAsSourceOffset , Zero ) ;
141+ Write ( link + RawLink < TLink > . RightAsSourceOffset , Zero ) ;
142+ Write ( link + RawLink < TLink > . SizeAsSourceOffset , Zero ) ;
143+ }
144+ }
145+ }
0 commit comments