├── .gitignore ├── README.md └── src ├── FNSeq ├── FNSeq.cs ├── FNSeq.csproj └── Properties │ └── AssemblyInfo.cs ├── FingerTree.UnitTests ├── App.config ├── FingerTree.UnitTests.csproj ├── OrderedSequenceUnitTests.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── FingerTree.sln ├── FingerTree ├── FingerTree.cs ├── FingerTree.csproj ├── FingerTreeM.cs ├── FingerTreeSplits.cs ├── OrderedSequence.cs ├── PriorityQueue.cs ├── Properties │ └── AssemblyInfo.cs └── RandAccessSequence.cs └── TestDotNetCollection ├── Properties └── AssemblyInfo.cs ├── TestCollAndString.cs ├── TestCollAndString.csproj └── app.config /.gitignore: -------------------------------------------------------------------------------- 1 | **.user 2 | **/obj 3 | **/bin 4 | **/res 5 | **.tlb 6 | **.tlh 7 | **.olb 8 | **.dll 9 | **.obj 10 | **.exe 11 | **.ncb 12 | **.suo 13 | **.bak 14 | **.log 15 | **.sdf 16 | **.opensdf 17 | **.ipch 18 | /vs 19 | /src/FingerTree/bin 20 | /src/FingerTree/obj 21 | /src/FingerTree.UnitTests/bin 22 | /src/FingerTree.UnitTests/obj 23 | /src/FNSeq/bin 24 | /src/FNSeq/obj 25 | /src/TestDotNetCollection/bin 26 | /src/TestDotNetCollection/obj -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CSharpFingerTree 2 | AppVeyor Status 3 | 4 | C# Implementation of Finger Tree 5 | 6 | Based on the work of Dimitre Novatchev [here](https://dnovatchev.wordpress.com/2008/07/20/the-swiss-army-knife-of-data-structures-in-c/) which in turn is based on this [research paper](http://www.staff.city.ac.uk/~ross/papers/FingerTree.html). 7 | -------------------------------------------------------------------------------- /src/FNSeq/FNSeq.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FingerTree; 6 | namespace FSeq 7 | { 8 | public class FString 9 | { 10 | private Seq theSeq = null; 11 | 12 | public FString() 13 | { 14 | theSeq = new Seq(new List()); 15 | } 16 | 17 | public FString(string aString) 18 | { 19 | theSeq = new Seq(aString.ToCharArray()); 20 | } 21 | 22 | protected FString(Seq aSeq) 23 | { 24 | theSeq = aSeq; 25 | } 26 | 27 | public uint Length() 28 | { 29 | return this.theSeq.length; 30 | } 31 | 32 | public FString Merge(FString string2) 33 | { 34 | return new FString(new Seq(theSeq.Merge(string2.theSeq.treeRep))); 35 | } 36 | 37 | public FString concat(FString afString) 38 | { 39 | return this.Merge(afString); 40 | } 41 | 42 | public static FString 43 | stringJoin(FString[] stringList, 44 | FString strSeparator) 45 | { 46 | FString fStr = new FString(); 47 | 48 | int numStrings = stringList.Length; 49 | 50 | if (numStrings == 0) 51 | return fStr; 52 | //else 53 | int i = 0; 54 | for (i = 0; i < numStrings - 1; i++) 55 | { 56 | fStr = fStr.Merge(stringList[i]); 57 | fStr = fStr.Merge(strSeparator); 58 | } 59 | fStr = fStr.Merge(stringList[i]); 60 | 61 | return fStr; 62 | } 63 | 64 | public FString insert(int startInd, FString fstr2) 65 | { 66 | Pair, uint>, FTreeM, uint>> theSplit = 67 | theSeq.SeqSplit(new MPredicate 68 | (FP.Curry(theLTMethod, (uint)startInd))); 69 | 70 | return new FString( 71 | new Seq 72 | ( 73 | ( 74 | theSplit.first.Merge(fstr2.theSeq.treeRep) 75 | .Merge(((Seq)(theSplit.second)).treeRep) 76 | ) 77 | ) 78 | ); 79 | 80 | } 81 | 82 | public FString remove(int startInd, int subLength) 83 | { 84 | uint theLength = theSeq.length; 85 | 86 | if (theLength == 0 || subLength <= 0) 87 | return this; 88 | //else 89 | if (startInd < 1) 90 | startInd = 0; 91 | 92 | if (startInd + subLength > theLength) 93 | subLength = (int)(theLength - startInd); 94 | 95 | // Now ready to do the real work 96 | Pair, uint>, FTreeM, uint>> split1 = 97 | theSeq.SeqSplit 98 | ( 99 | new MPredicate 100 | (FP.Curry(theLTMethod, (uint)startInd)) 101 | ); 102 | 103 | Pair, uint>, FTreeM, uint>> split2 = 104 | split1.second.SeqSplit 105 | ( 106 | new MPredicate 107 | (FP.Curry(theLTMethod, (uint)subLength)) 108 | ); 109 | 110 | FString fsResult = 111 | new FString( 112 | new Seq 113 | ( 114 | split1.first.Merge(((Seq)(split2.second)).treeRep) 115 | ) 116 | ); 117 | return fsResult; 118 | } 119 | 120 | public FString substring(int startInd, int subLength) 121 | { 122 | uint theLength = theSeq.length; 123 | 124 | if (theLength == 0 || subLength <= 0) 125 | return this; 126 | //else 127 | if (startInd < 1) 128 | startInd = 0; 129 | 130 | if (startInd + subLength > theLength) 131 | subLength = (int)(theLength - startInd); 132 | 133 | // Now ready to do the real work 134 | FString fsResult = 135 | new FString( 136 | new Seq 137 | ( 138 | theSeq.SeqSplit 139 | ( 140 | new MPredicate 141 | (FP.Curry(theLTMethod, (uint)startInd)) 142 | ).second 143 | .SeqSplit 144 | (new MPredicate 145 | (FP.Curry(theLTMethod, (uint)subLength)) 146 | ).first 147 | ) 148 | ); 149 | return fsResult; 150 | } 151 | 152 | public char itemAt(int ind) 153 | { 154 | if (ind < 0 || ind >= Length()) 155 | throw new ArgumentOutOfRangeException(); 156 | //else 157 | return theSeq.ElemAt(((uint)ind)); 158 | } 159 | 160 | 161 | 162 | bool theLTMethod(uint i1, uint i2) 163 | { 164 | return i1 < i2; 165 | } 166 | 167 | } 168 | 169 | 170 | public class FNSeq //where T : IMeasured 171 | { 172 | private Seq theSeq = null; 173 | 174 | public FNSeq() 175 | { 176 | theSeq = new Seq(new List()); 177 | } 178 | 179 | public FNSeq(IEnumerable seqIterator) 180 | { 181 | theSeq = new Seq(new List()); 182 | 183 | foreach(T t in seqIterator) 184 | theSeq = (Seq)(theSeq.Push_Back(new SizedElem(t))); 185 | } 186 | 187 | protected FNSeq(Seq aSeq) 188 | { 189 | theSeq = aSeq; 190 | } 191 | 192 | public uint Length() 193 | { 194 | return this.theSeq.length; 195 | } 196 | 197 | public List ToSequence() 198 | { 199 | List lstResult = new List(); 200 | 201 | foreach (SizedElem elem in theSeq.ToSequence()) 202 | lstResult.Add(elem.Element); 203 | 204 | return lstResult;//.ToArray(); 205 | } 206 | 207 | public T itemAt(int ind) 208 | { 209 | if (ind < 0 || ind >= Length()) 210 | throw new ArgumentOutOfRangeException(); 211 | //else 212 | return theSeq.ElemAt(((uint)ind)); 213 | } 214 | 215 | public FNSeq reverse() 216 | { 217 | return new FNSeq((Seq)(theSeq.Reverse())); 218 | } 219 | 220 | public FNSeq Merge(FNSeq seq2) 221 | { 222 | return new FNSeq(new Seq(theSeq.Merge(seq2.theSeq.treeRep))); 223 | } 224 | 225 | public FNSeq skip(int length) 226 | { 227 | return new FNSeq 228 | ( 229 | new Seq 230 | ( 231 | this.theSeq.dropUntil(new MPredicate 232 | (FP.Curry(theLTMethod, (uint)length)) 233 | ) 234 | ) 235 | ); 236 | } 237 | 238 | public FNSeq take(int length) 239 | { 240 | return new FNSeq 241 | ( 242 | new Seq 243 | ( 244 | this.theSeq.takeUntil(new MPredicate 245 | (FP.Curry(theLTMethod, (uint)length)) 246 | ) 247 | ) 248 | ); 249 | } 250 | 251 | public FNSeq subsequence(int startInd, int subLength) 252 | { 253 | uint theLength = theSeq.length; 254 | 255 | if (theLength == 0 || subLength <= 0) 256 | return this; 257 | //else 258 | if (startInd < 0) 259 | startInd = 0; 260 | 261 | if (startInd + subLength > theLength) 262 | subLength = (int)(theLength - startInd); 263 | 264 | // Now ready to do the real work 265 | FNSeq fsResult = 266 | new FNSeq( 267 | (Seq) 268 | ( 269 | ((Seq) 270 | (theSeq.SeqSplit 271 | ( 272 | new MPredicate 273 | (FP.Curry(theLTMethod, (uint)startInd)) 274 | ).second 275 | ) 276 | ).SeqSplit 277 | (new MPredicate 278 | (FP.Curry(theLTMethod, (uint)subLength)) 279 | ).first 280 | ) 281 | ); 282 | return fsResult; 283 | } 284 | 285 | public FNSeq remove(int ind) 286 | { 287 | if (ind < 0 || ind >= Length()) 288 | throw new ArgumentOutOfRangeException(); 289 | //else 290 | return new FNSeq(theSeq.RemoveAt((uint)(ind))); 291 | } 292 | 293 | // this inserts a whole sequence, so we cannot just use Seq.snsertAt() 294 | public FNSeq insert_before(int ind, FNSeq fSeq2) 295 | { 296 | if (ind < 0 || ind >= this.Length()) 297 | throw new ArgumentOutOfRangeException(); 298 | //else 299 | Pair, uint>, FTreeM, uint>> theSplit = 300 | theSeq.SeqSplit 301 | (new MPredicate 302 | ( 303 | FP.Curry(theLTMethod, (uint)ind - 1) 304 | ) 305 | ); 306 | 307 | FNSeq fs1 = new FNSeq((Seq)(theSplit.first)); 308 | FNSeq fs3 = new FNSeq((Seq)(theSplit.second)); 309 | 310 | return fs1.Merge(fSeq2).Merge(fs3); 311 | } 312 | 313 | bool theLTMethod(uint i1, uint i2) 314 | { 315 | return i1 < i2; 316 | } 317 | 318 | } 319 | } 320 | 321 | -------------------------------------------------------------------------------- /src/FNSeq/FNSeq.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {9CDD93A8-3350-4F13-A001-F640BF28B96D} 9 | Library 10 | Properties 11 | FNSeq 12 | FNSeq 13 | v4.6.1 14 | 512 15 | 16 | 17 | 18 | 19 | 3.5 20 | 21 | 22 | 23 | true 24 | full 25 | false 26 | bin\Debug\ 27 | DEBUG;TRACE 28 | prompt 29 | 4 30 | false 31 | 32 | 33 | pdbonly 34 | true 35 | bin\Release\ 36 | TRACE 37 | prompt 38 | 4 39 | false 40 | 41 | 42 | 43 | 44 | 3.5 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | {51807875-B25D-4F87-8B8D-5100E5F26BAD} 55 | FingerTree 56 | 57 | 58 | 59 | 66 | -------------------------------------------------------------------------------- /src/FNSeq/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("FNSeq")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("At Home")] 12 | [assembly: AssemblyProduct("FNSeq")] 13 | [assembly: AssemblyCopyright("Copyright © At Home 2008")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("501764b5-1b81-408a-9970-96051f5d1daf")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/FingerTree.UnitTests/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/FingerTree.UnitTests/FingerTree.UnitTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {78137E75-6466-4EDA-AACF-0EC4405741CA} 8 | Exe 9 | Properties 10 | FingerTree.UnitTests 11 | FingerTree.UnitTests 12 | v4.6.1 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | {51807875-b25d-4f87-8b8d-5100e5f26bad} 57 | FingerTree 58 | 59 | 60 | 61 | 68 | -------------------------------------------------------------------------------- /src/FingerTree.UnitTests/OrderedSequenceUnitTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace FingerTree.UnitTests 8 | { 9 | public class OrderedSequenceUnitTests 10 | { 11 | public void TestCharOrderedSequence() 12 | { 13 | var os = new OrderedSequence( 14 | new Key(uint.MinValue, (c) => { return c; })); 15 | 16 | var src = new List 17 | { 18 | 'z', 19 | 'b', 20 | 'h', 21 | 'a', 22 | 'a', 23 | 'c', 24 | 'e', 25 | 'j' 26 | }; 27 | 28 | foreach (var s in src) 29 | os = os.Insert(s); 30 | 31 | var expected = new List 32 | { 33 | 'a', 34 | 'a', 35 | 'b', 36 | 'c', 37 | 'e', 38 | 'h', 39 | 'j', 40 | 'z', 41 | }; 42 | 43 | var tseq = os.ToSequence(); 44 | 45 | var tseqEnum = tseq.GetEnumerator(); 46 | tseqEnum.MoveNext(); 47 | 48 | foreach (var es in expected) 49 | { 50 | if (0 != es.CompareTo(tseqEnum.Current)) 51 | throw new Exception("TestCharOrderedSequence failed."); 52 | tseqEnum.MoveNext(); 53 | } 54 | } 55 | 56 | public void TestStringOrderedSequence() 57 | { 58 | var os = new OrderedSequence( 59 | new Key(string.Empty, (s) => { return s; })); 60 | 61 | var src = new List 62 | { 63 | "zaz", 64 | "zab", 65 | "zah", 66 | "aaz", 67 | "abz", 68 | "acd", 69 | "eeh", 70 | "heh" 71 | }; 72 | 73 | foreach (var s in src) 74 | os = os.Insert(s); 75 | 76 | var expected = new List 77 | { 78 | "aaz", 79 | "abz", 80 | "acd", 81 | "eeh", 82 | "heh", 83 | "zab", 84 | "zah", 85 | "zaz", 86 | }; 87 | 88 | var tseq = os.ToSequence(); 89 | 90 | var tseqEnum = tseq.GetEnumerator(); 91 | tseqEnum.MoveNext(); 92 | 93 | foreach (var es in expected) 94 | { 95 | if(0 != es.CompareTo(tseqEnum.Current)) 96 | throw new Exception("TestStringOrderedSequence failed."); 97 | tseqEnum.MoveNext(); 98 | } 99 | 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/FingerTree.UnitTests/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace FingerTree.UnitTests 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var t1 = new OrderedSequenceUnitTests(); 14 | t1.TestCharOrderedSequence(); 15 | t1.TestStringOrderedSequence(); 16 | 17 | Console.ReadKey(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/FingerTree.UnitTests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("FingerTree.UnitTests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("FingerTree.UnitTests")] 13 | [assembly: AssemblyCopyright("Copyright © 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("78137e75-6466-4eda-aacf-0ec4405741ca")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/FingerTree.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FingerTree", "FingerTree\FingerTree.csproj", "{51807875-B25D-4F87-8B8D-5100E5F26BAD}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCollAndString", "TestDotNetCollection\TestCollAndString.csproj", "{CFBD54BD-4EDD-4793-8913-4611B3D33682}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FNSeq", "FNSeq\FNSeq.csproj", "{9CDD93A8-3350-4F13-A001-F640BF28B96D}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FingerTree.UnitTests", "FingerTree.UnitTests\FingerTree.UnitTests.csproj", "{78137E75-6466-4EDA-AACF-0EC4405741CA}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {51807875-B25D-4F87-8B8D-5100E5F26BAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {51807875-B25D-4F87-8B8D-5100E5F26BAD}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {51807875-B25D-4F87-8B8D-5100E5F26BAD}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {51807875-B25D-4F87-8B8D-5100E5F26BAD}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {CFBD54BD-4EDD-4793-8913-4611B3D33682}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {CFBD54BD-4EDD-4793-8913-4611B3D33682}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {CFBD54BD-4EDD-4793-8913-4611B3D33682}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {CFBD54BD-4EDD-4793-8913-4611B3D33682}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {9CDD93A8-3350-4F13-A001-F640BF28B96D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {9CDD93A8-3350-4F13-A001-F640BF28B96D}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {9CDD93A8-3350-4F13-A001-F640BF28B96D}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {9CDD93A8-3350-4F13-A001-F640BF28B96D}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {78137E75-6466-4EDA-AACF-0EC4405741CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {78137E75-6466-4EDA-AACF-0EC4405741CA}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {78137E75-6466-4EDA-AACF-0EC4405741CA}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {78137E75-6466-4EDA-AACF-0EC4405741CA}.Release|Any CPU.Build.0 = Release|Any CPU 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | EndGlobal 41 | -------------------------------------------------------------------------------- /src/FingerTree/FingerTree.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace FingerTree 6 | { 7 | public abstract class FTree 8 | { 9 | public abstract FTree Push_Front(T t); 10 | public abstract FTree Push_Back(T t); 11 | 12 | public abstract IEnumerable ToSequence(); 13 | public abstract IEnumerable ToSequenceR(); 14 | 15 | public abstract ViewL LeftView(); 16 | public abstract ViewR RightView(); 17 | 18 | public abstract FTree Merge(FTree rightFT); 19 | 20 | public abstract FTree App2(List ts, FTree rightFT); 21 | 22 | 23 | public static FTree Create(List frontList, //may be empty! 24 | FTree> innerFT, 25 | Digit backDig 26 | ) 27 | { 28 | if (frontList.Count > 0) 29 | return new DeepFTree(new Digit(frontList), 30 | innerFT, 31 | backDig 32 | ); 33 | //else 34 | 35 | if (innerFT is EmptyFTree>) 36 | return FromSequence(backDig.digNodes); 37 | 38 | //else we must create a new intermediate tree 39 | var innerLeft = innerFT.LeftView(); 40 | 41 | List newlstFront = innerLeft.head.theNodes; 42 | 43 | DeepFTree theNewDeepTree = 44 | new DeepFTree(new Digit(newlstFront), 45 | innerLeft.ftTail, 46 | backDig 47 | ); 48 | 49 | return theNewDeepTree; 50 | } 51 | 52 | public static FTree CreateR(Digit frontDig, 53 | FTree> innerFT, 54 | List backList //may be empty! 55 | ) 56 | { 57 | if (backList.Count > 0) 58 | return new DeepFTree(frontDig, 59 | innerFT, 60 | new Digit(backList) 61 | ); 62 | //else 63 | 64 | if (innerFT is EmptyFTree>) 65 | return FromSequence(frontDig.digNodes); 66 | 67 | //else we must create a new intermediate tree 68 | var innerRight = innerFT.RightView(); 69 | 70 | List newlstBack = innerRight.last.theNodes; 71 | 72 | DeepFTree theNewDeepTree = 73 | new DeepFTree(frontDig, 74 | innerRight.ftInit, 75 | new Digit(newlstBack) 76 | ); 77 | 78 | return theNewDeepTree; 79 | } 80 | 81 | public static FTree FromSequence(IEnumerable sequence) 82 | { 83 | IEnumerator sequenceEnum = sequence.GetEnumerator(); 84 | 85 | FTree ftResult = new EmptyFTree(); 86 | 87 | while (sequenceEnum.MoveNext()) 88 | { 89 | ftResult = ftResult.Push_Back(sequenceEnum.Current); 90 | } 91 | 92 | return ftResult; 93 | } 94 | 95 | public static List> ListOfNodes(List tList) 96 | { 97 | List> resultNodeList = new List>(); 98 | 99 | Node nextNode = null; 100 | 101 | int tCount = tList.Count; 102 | 103 | if(tCount < 4) 104 | { 105 | nextNode = new Node(tList); 106 | 107 | resultNodeList.Add(nextNode); 108 | 109 | return resultNodeList; 110 | } 111 | 112 | //else 113 | List nextTList = new List(tList.GetRange(0,3)); 114 | //tList.CopyTo(0, nextTList, 0, 3); 115 | 116 | nextNode = new Node(nextTList); 117 | resultNodeList.Add(nextNode); 118 | 119 | resultNodeList.AddRange(ListOfNodes(tList.GetRange(3, tCount-3))); 120 | 121 | return resultNodeList; 122 | } 123 | 124 | public class ViewL 125 | { 126 | public X head; 127 | public FTree ftTail; 128 | 129 | public ViewL(X head, FTree ftTail) 130 | { 131 | this.head = head; 132 | this.ftTail = ftTail; 133 | } 134 | } 135 | 136 | public class ViewR 137 | { 138 | public X last; 139 | public FTree ftInit; 140 | 141 | public ViewR(FTree ftInit, X last) 142 | { 143 | this.ftInit = ftInit; 144 | this.last = last; 145 | } 146 | } 147 | 148 | public class Digit 149 | { 150 | public List digNodes = new List(); // At most four elements in this list 151 | 152 | public Digit(U u1) 153 | { 154 | digNodes.Add(u1); 155 | } 156 | 157 | public Digit(U u1, U u2) 158 | { 159 | digNodes.Add(u1); 160 | digNodes.Add(u2); 161 | } 162 | public Digit(U u1, U u2, U u3) 163 | { 164 | digNodes.Add(u1); 165 | digNodes.Add(u2); 166 | digNodes.Add(u3); 167 | } 168 | public Digit(U u1, U u2, U u3, U u4) 169 | { 170 | digNodes.Add(u1); 171 | digNodes.Add(u2); 172 | digNodes.Add(u3); 173 | digNodes.Add(u4); 174 | } 175 | 176 | public Digit(List listU) 177 | { 178 | digNodes = listU; 179 | } 180 | } 181 | 182 | 183 | public class Node 184 | { 185 | public List theNodes = new List(); // 2 or 3 elements in this list 186 | 187 | public Node(V v1, V v2) 188 | { 189 | theNodes.Add(v1); 190 | theNodes.Add(v2); 191 | } 192 | 193 | public Node(V v1, V v2, V v3) 194 | { 195 | theNodes.Add(v1); 196 | theNodes.Add(v2); 197 | theNodes.Add(v3); 198 | } 199 | 200 | public Node(List listV) 201 | { 202 | theNodes = listV; 203 | } 204 | } 205 | 206 | 207 | 208 | } 209 | 210 | public class EmptyFTree : FTree 211 | { 212 | public EmptyFTree() { } 213 | 214 | public override FTree Push_Front(T t) 215 | { 216 | return new SingleFTree(t); 217 | } 218 | 219 | public override FTree Push_Back(T t) 220 | { 221 | return new SingleFTree(t); 222 | } 223 | 224 | public override IEnumerable ToSequence() 225 | { 226 | return new List(); 227 | } 228 | 229 | public override IEnumerable ToSequenceR() 230 | { 231 | return new List(); 232 | } 233 | 234 | public override ViewL LeftView() 235 | { 236 | return null; 237 | } 238 | 239 | public override ViewR RightView() 240 | { 241 | return null; 242 | } 243 | 244 | public override FTree App2(List ts, FTree rightFT) 245 | { 246 | FTree resultFT = rightFT; 247 | 248 | for(int i = ts.Count -1; i >= 0; i--) 249 | { 250 | resultFT = resultFT.Push_Front(ts[i]); 251 | } 252 | 253 | return resultFT; 254 | } 255 | 256 | public override FTree Merge(FTree rightFT) 257 | { 258 | return rightFT; 259 | } 260 | 261 | 262 | } 263 | 264 | public class SingleFTree : FTree 265 | { 266 | protected T theSingle; 267 | public SingleFTree(T t) 268 | { 269 | theSingle = t; 270 | } 271 | 272 | public override FTree Push_Front(T t) 273 | { 274 | return new DeepFTree(new Digit(t), 275 | new EmptyFTree>(), 276 | new Digit(theSingle) 277 | ); 278 | } 279 | 280 | public override FTree Push_Back(T t) 281 | { 282 | return new DeepFTree(new Digit(theSingle), 283 | new EmptyFTree>(), 284 | new Digit(t) 285 | ); 286 | } 287 | 288 | public override IEnumerable ToSequence() 289 | { 290 | List newL = new List(); 291 | newL.Add(theSingle); 292 | return newL; 293 | } 294 | 295 | public override IEnumerable ToSequenceR() 296 | { 297 | List newR = new List(); 298 | newR.Add(theSingle); 299 | return newR; 300 | } 301 | 302 | public override ViewL LeftView() 303 | { 304 | return new ViewL(theSingle, new EmptyFTree()); 305 | } 306 | 307 | public override ViewR RightView() 308 | { 309 | return new ViewR(new EmptyFTree(), theSingle); 310 | } 311 | 312 | public override FTree App2(List ts, FTree rightFT) 313 | { 314 | FTree resultFT = rightFT; 315 | 316 | for (int i = ts.Count - 1; i >= 0; i--) 317 | { 318 | resultFT = resultFT.Push_Front(ts[i]); 319 | } 320 | 321 | return resultFT.Push_Front(theSingle); 322 | } 323 | 324 | public override FTree Merge(FTree rightFT) 325 | { 326 | return rightFT.Push_Front(theSingle); 327 | } 328 | 329 | 330 | } 331 | 332 | public class DeepFTree : FTree 333 | { 334 | protected Digit frontDig; 335 | protected FTree> innerFT; 336 | protected Digit backDig; 337 | 338 | public DeepFTree(Digit frontDig, FTree> innerFT, Digit backDig) 339 | { 340 | if (frontDig.digNodes.Count > 0) 341 | { 342 | this.frontDig = frontDig; 343 | this.innerFT = innerFT; 344 | this.backDig = backDig; 345 | } 346 | else 347 | { 348 | throw new Exception("The DeepFTree() constructor is passed an empty frontDig !"); 349 | } 350 | 351 | } 352 | 353 | public override ViewL LeftView() 354 | { 355 | T head = frontDig.digNodes[0]; 356 | 357 | List newFront = new List(frontDig.digNodes); 358 | newFront.RemoveAt(0); 359 | 360 | return new ViewL(head, 361 | FTree.Create(newFront, innerFT, backDig) 362 | //new DeepFTree(newDigs, innerFT, backDig) 363 | ); 364 | } 365 | 366 | public override ViewR RightView() 367 | { 368 | int lastIndex = backDig.digNodes.Count - 1; 369 | T last = backDig.digNodes[lastIndex]; 370 | 371 | List newBack = new List(backDig.digNodes); 372 | newBack.RemoveAt(lastIndex); 373 | 374 | return new ViewR(FTree.CreateR(frontDig, innerFT, newBack), 375 | last 376 | ); 377 | } 378 | 379 | public override FTree Push_Front(T t) 380 | { 381 | if (frontDig.digNodes.Count == 4) 382 | { 383 | List newFront = new List(frontDig.digNodes); 384 | newFront.RemoveAt(0); 385 | 386 | return new DeepFTree(new Digit(t, frontDig.digNodes[0]), 387 | innerFT.Push_Front(new Node(newFront)), 388 | backDig 389 | ); 390 | } 391 | else //less than three digits in front -- will accomodate one more 392 | { 393 | List newFront = new List(frontDig.digNodes); 394 | newFront.Insert(0, t); 395 | 396 | return new DeepFTree(new Digit(newFront), innerFT, backDig); 397 | } 398 | } 399 | 400 | public override FTree Push_Back(T t) 401 | { 402 | int cntbackDig = backDig.digNodes.Count; 403 | 404 | 405 | if (backDig.digNodes.Count == 4) 406 | { 407 | List newBack = new List(backDig.digNodes); 408 | newBack.RemoveAt(cntbackDig - 1); 409 | 410 | return new DeepFTree 411 | (frontDig, 412 | innerFT.Push_Back(new Node(newBack)), 413 | new Digit(backDig.digNodes[cntbackDig - 1], t) 414 | ); 415 | 416 | } 417 | else //less than three digits at the back -- will accomodate one more 418 | { 419 | List newBack = new List(backDig.digNodes); 420 | newBack.Add(t); 421 | 422 | return new DeepFTree(frontDig, innerFT, new Digit(newBack)); 423 | } 424 | 425 | } 426 | 427 | 428 | 429 | public override IEnumerable ToSequence() 430 | { 431 | ViewL lView = LeftView(); 432 | 433 | yield return lView.head; 434 | 435 | foreach (T t in lView.ftTail.ToSequence()) 436 | yield return t; 437 | } 438 | 439 | public override IEnumerable ToSequenceR() 440 | { 441 | ViewR rView = RightView(); 442 | 443 | yield return rView.last; 444 | 445 | foreach (T t in rView.ftInit.ToSequenceR()) 446 | yield return t; 447 | } 448 | 449 | public override FTree App2(List ts, FTree rightFT) 450 | { 451 | if (! (rightFT is DeepFTree)) 452 | { 453 | FTree resultFT = this; 454 | 455 | foreach (T t in ts) 456 | { 457 | resultFT = resultFT.Push_Back(t); 458 | } 459 | 460 | return (rightFT is EmptyFTree) 461 | ? resultFT 462 | : resultFT.Push_Back(rightFT.LeftView().head); 463 | } 464 | else // the right tree is also a deep tree 465 | { 466 | DeepFTree deepRight = rightFT as DeepFTree; 467 | 468 | List cmbList = new List(backDig.digNodes); 469 | 470 | cmbList.AddRange(ts); 471 | 472 | cmbList.AddRange(deepRight.frontDig.digNodes); 473 | 474 | FTree resultFT = 475 | new DeepFTree 476 | (frontDig, 477 | innerFT.App2(FTree.ListOfNodes(cmbList), deepRight.innerFT), 478 | deepRight.backDig 479 | ); 480 | 481 | return resultFT; 482 | } 483 | } 484 | 485 | public override FTree Merge(FTree rightFT) 486 | { 487 | List emptyList = new List(); 488 | 489 | return App2(emptyList, rightFT); 490 | } 491 | 492 | 493 | } 494 | 495 | } 496 | -------------------------------------------------------------------------------- /src/FingerTree/FingerTree.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {51807875-B25D-4F87-8B8D-5100E5F26BAD} 9 | Library 10 | Properties 11 | FingerTree 12 | FingerTree 13 | 14 | 15 | 16 | 17 | 3.5 18 | v4.6.1 19 | 20 | 21 | 22 | true 23 | full 24 | false 25 | bin\Debug\ 26 | DEBUG;TRACE 27 | prompt 28 | 4 29 | false 30 | 31 | 32 | pdbonly 33 | true 34 | bin\Release\ 35 | TRACE 36 | prompt 37 | 4 38 | false 39 | 40 | 41 | 42 | 43 | 3.5 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 65 | -------------------------------------------------------------------------------- /src/FingerTree/FingerTreeM.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace FingerTree 6 | { 7 | public class Monoid 8 | { 9 | T theZero; 10 | 11 | public delegate T monOp(T t1, T t2); 12 | 13 | public monOp theOp; 14 | 15 | public Monoid(T tZero, monOp aMonOp) 16 | { 17 | theZero = tZero; 18 | 19 | theOp = aMonOp; 20 | 21 | } 22 | 23 | public T zero 24 | { 25 | get 26 | { 27 | return theZero; 28 | } 29 | } 30 | 31 | } 32 | 33 | public interface IMeasured 34 | { 35 | M Measure(); 36 | } 37 | 38 | public interface ISplittable : IMeasured 39 | { 40 | IEnumerable ToSequence(); 41 | } 42 | 43 | 44 | // Types: 45 | // T -- the Node type for this tree, must be measurable 46 | // M -- the type for the values of the measures 47 | public abstract partial class FTreeM : ISplittable where T : IMeasured 48 | { 49 | public abstract Monoid treeMonoid { get; } 50 | 51 | public abstract M Measure(); 52 | 53 | public abstract FTreeM Push_Front(T t); 54 | public abstract FTreeM Push_Back(T t); 55 | 56 | public abstract IEnumerable ToSequence(); 57 | public abstract IEnumerable ToSequenceR(); 58 | 59 | public abstract ViewL LeftView(); 60 | public abstract ViewR RightView(); 61 | 62 | public abstract FTreeM Merge(FTreeM rightFT); 63 | 64 | public abstract FTreeM App2(List ts, FTreeM rightFT); 65 | 66 | public static FTreeM FromSequence(IEnumerable sequence, 67 | Monoid aMonoid) 68 | { 69 | IEnumerator sequenceEnum = sequence.GetEnumerator(); 70 | 71 | FTreeM ftResult = new EmptyFTreeM(aMonoid); 72 | 73 | while (sequenceEnum.MoveNext()) 74 | { 75 | ftResult = ftResult.Push_Back(sequenceEnum.Current); 76 | } 77 | 78 | return ftResult; 79 | } 80 | 81 | public static FTreeM Create(List frontList, //may be empty! 82 | FTreeM, M> innerFT, 83 | Digit backDig 84 | ) 85 | { 86 | Monoid theMonoid = backDig.theMonoid; 87 | 88 | if (frontList.Count > 0) 89 | return new DeepFTreeM 90 | (theMonoid, 91 | new Digit(theMonoid, 92 | frontList), 93 | innerFT, 94 | backDig 95 | ); 96 | //else 97 | 98 | if (innerFT is EmptyFTreeM, M>) 99 | return FromSequence(backDig.digNodes, theMonoid); 100 | 101 | //else we must create a new intermediate tree 102 | var innerLeft = innerFT.LeftView(); 103 | 104 | List newlstFront = innerLeft.head.theNodes; 105 | 106 | DeepFTreeM theNewDeepTree = 107 | new DeepFTreeM 108 | (theMonoid, 109 | new Digit(theMonoid, 110 | newlstFront), 111 | innerLeft.ftTail, 112 | backDig 113 | ); 114 | 115 | return theNewDeepTree; 116 | } 117 | 118 | 119 | public virtual FTreeM Reverse() 120 | { 121 | return this; 122 | } 123 | 124 | public static Digit ReverseDigit(Digit theDigit) 125 | { 126 | List newDigitList = new List(theDigit.digNodes); 127 | 128 | newDigitList.Reverse(); 129 | 130 | return new Digit(theDigit.theMonoid, newDigitList); 131 | } 132 | 133 | public static FTreeM CreateR(Digit frontDig, 134 | FTreeM, M> innerFT, 135 | List backList //may be empty! 136 | ) 137 | { 138 | Monoid theMonoid = frontDig.theMonoid; 139 | 140 | if (backList.Count > 0) 141 | return new DeepFTreeM 142 | (theMonoid, 143 | frontDig, 144 | innerFT, 145 | new Digit(theMonoid, backList) 146 | ); 147 | //else 148 | 149 | if (innerFT is EmptyFTreeM, M>) 150 | return FromSequence(frontDig.digNodes, theMonoid); 151 | 152 | //else we must create a new intermediate tree 153 | var innerRight = innerFT.RightView(); 154 | 155 | List newlstBack = innerRight.last.theNodes; 156 | 157 | DeepFTreeM theNewDeepTree = 158 | new DeepFTreeM 159 | (theMonoid, 160 | frontDig, 161 | innerRight.ftInit, 162 | new Digit(theMonoid, newlstBack) 163 | ); 164 | 165 | return theNewDeepTree; 166 | } 167 | 168 | public static List> 169 | ListOfNodes(Monoid aMonoid, List tList) 170 | { 171 | List> resultNodeList = new List>(); 172 | 173 | Node nextNode = null; 174 | 175 | int tCount = tList.Count; 176 | 177 | if (tCount < 4) 178 | { 179 | nextNode = new Node(aMonoid, tList); 180 | 181 | resultNodeList.Add(nextNode); 182 | 183 | return resultNodeList; 184 | } 185 | 186 | //else 187 | List nextTList = new List(tList.GetRange(0, 3)); 188 | 189 | nextNode = new Node(aMonoid,nextTList); 190 | resultNodeList.Add(nextNode); 191 | 192 | resultNodeList.AddRange 193 | (ListOfNodes(aMonoid, 194 | tList.GetRange(3, tCount - 3) 195 | ) 196 | ); 197 | 198 | return resultNodeList; 199 | } 200 | 201 | 202 | public partial class Digit : ISplittable 203 | where U : IMeasured 204 | { 205 | public Monoid theMonoid; 206 | 207 | public List digNodes = new List(); // At most four elements in this list 208 | 209 | public Digit(Monoid aMonoid, U u1) 210 | { 211 | theMonoid = aMonoid; 212 | 213 | digNodes.Add(u1); 214 | } 215 | 216 | public Digit(Monoid aMonoid, U u1, U u2) 217 | { 218 | theMonoid = aMonoid; 219 | 220 | digNodes.Add(u1); 221 | digNodes.Add(u2); 222 | } 223 | public Digit(Monoid aMonoid, U u1, U u2, U u3) 224 | { 225 | theMonoid = aMonoid; 226 | 227 | digNodes.Add(u1); 228 | digNodes.Add(u2); 229 | digNodes.Add(u3); 230 | } 231 | public Digit(Monoid aMonoid, U u1, U u2, U u3, U u4) 232 | { 233 | theMonoid = aMonoid; 234 | 235 | digNodes.Add(u1); 236 | digNodes.Add(u2); 237 | digNodes.Add(u3); 238 | digNodes.Add(u4); 239 | } 240 | 241 | public Digit(Monoid aMonoid, List listU) 242 | { 243 | theMonoid = aMonoid; 244 | 245 | digNodes = listU; 246 | } 247 | 248 | public V Measure() 249 | { 250 | V result = theMonoid.zero; 251 | 252 | foreach (U u in digNodes) 253 | result = theMonoid.theOp(result, u.Measure()); 254 | 255 | return result; 256 | } 257 | 258 | public IEnumerable ToSequence() 259 | { 260 | return digNodes; 261 | } 262 | } 263 | 264 | public class Node : IMeasured where U : IMeasured 265 | { 266 | public Monoid theMonoid; 267 | 268 | protected V PreCalcMeasure; 269 | 270 | public List theNodes = new List(); // 2 or 3 elements in this list 271 | 272 | public Node(Monoid aMonoid, U u1, U u2) 273 | { 274 | theMonoid = aMonoid; 275 | 276 | theNodes.Add(u1); 277 | theNodes.Add(u2); 278 | 279 | PreCalcMeasure = theMonoid.theOp(u1.Measure(), u2.Measure()); 280 | } 281 | 282 | public Node(Monoid aMonoid, U u1, U u2, U u3) 283 | { 284 | theMonoid = aMonoid; 285 | 286 | theNodes.Add(u1); 287 | theNodes.Add(u2); 288 | theNodes.Add(u3); 289 | 290 | PreCalcMeasure = theMonoid.zero; 291 | foreach(U u in theNodes) 292 | PreCalcMeasure = theMonoid.theOp(PreCalcMeasure, u.Measure()); 293 | } 294 | 295 | public Node(Monoid aMonoid, List listU) 296 | { 297 | theMonoid = aMonoid; 298 | 299 | theNodes = listU; 300 | 301 | PreCalcMeasure = theMonoid.zero; 302 | foreach (U u in theNodes) 303 | PreCalcMeasure = theMonoid.theOp(PreCalcMeasure, u.Measure()); 304 | } 305 | 306 | public V Measure() 307 | { 308 | return PreCalcMeasure; 309 | } 310 | } 311 | 312 | 313 | //public class ViewL where X : IMeasured 314 | //{ 315 | // public X head; 316 | // public FTreeM ftTail; 317 | 318 | // public ViewL(X head, FTreeM ftTail) 319 | // { 320 | // this.head = head; 321 | // this.ftTail = ftTail; 322 | // } 323 | //} 324 | 325 | //public class ViewR where X : IMeasured 326 | //{ 327 | // public X last; 328 | // public FTreeM ftInit; 329 | 330 | // public ViewR(FTreeM ftInit, X last) 331 | // { 332 | // this.ftInit = ftInit; 333 | // this.last = last; 334 | // } 335 | //} 336 | 337 | } 338 | 339 | public class ViewL where X : IMeasured 340 | { 341 | public X head; 342 | public FTreeM ftTail; 343 | 344 | public ViewL(X head, FTreeM ftTail) 345 | { 346 | this.head = head; 347 | this.ftTail = ftTail; 348 | } 349 | } 350 | 351 | public class ViewR where X : IMeasured 352 | { 353 | public X last; 354 | public FTreeM ftInit; 355 | 356 | public ViewR(FTreeM ftInit, X last) 357 | { 358 | this.ftInit = ftInit; 359 | this.last = last; 360 | } 361 | } 362 | 363 | 364 | public partial class EmptyFTreeM : FTreeM where T : IMeasured 365 | { 366 | Monoid theMonoid; 367 | 368 | public EmptyFTreeM(Monoid aMonoid) 369 | { 370 | theMonoid = aMonoid; 371 | } 372 | 373 | public override Monoid treeMonoid 374 | { 375 | get { return theMonoid; } 376 | } 377 | 378 | public override M Measure() 379 | { 380 | return theMonoid.zero; 381 | } 382 | 383 | public override FTreeM Push_Front(T t) 384 | { 385 | return new SingleFTreeM(theMonoid, t); 386 | } 387 | 388 | public override FTreeM Push_Back(T t) 389 | { 390 | return new SingleFTreeM(theMonoid, t); 391 | } 392 | 393 | public override IEnumerable ToSequence() 394 | { 395 | return new List(); 396 | } 397 | 398 | public override IEnumerable ToSequenceR() 399 | { 400 | return new List(); 401 | } 402 | 403 | public override ViewL LeftView() 404 | { 405 | return null; 406 | } 407 | 408 | public override ViewR RightView() 409 | { 410 | return null; 411 | } 412 | 413 | public override FTreeM App2(List ts, FTreeM rightFT) 414 | { 415 | FTreeM resultFT = rightFT; 416 | 417 | for(int i = ts.Count -1; i >= 0; i--) 418 | { 419 | resultFT = resultFT.Push_Front(ts[i]); 420 | } 421 | 422 | return resultFT; 423 | } 424 | 425 | public override FTreeM Merge(FTreeM rightFT) 426 | { 427 | return rightFT; 428 | } 429 | 430 | } 431 | 432 | public partial class SingleFTreeM : FTreeM where T : IMeasured 433 | { 434 | public Monoid theMonoid; 435 | 436 | protected T theSingle; 437 | 438 | public SingleFTreeM(Monoid aMonoid, T t) 439 | { 440 | theMonoid = aMonoid; 441 | 442 | theSingle = t; 443 | } 444 | 445 | public override Monoid treeMonoid 446 | { 447 | get { return theMonoid; } 448 | } 449 | 450 | public override M Measure() 451 | { 452 | return theSingle.Measure(); 453 | } 454 | 455 | public override FTreeM Push_Front(T t) 456 | { 457 | return new DeepFTreeM(theMonoid, 458 | new Digit(theMonoid, t), 459 | new EmptyFTreeM, M>(theMonoid), 460 | new Digit(theMonoid, theSingle) 461 | ); 462 | } 463 | 464 | public override FTreeM Push_Back(T t) 465 | { 466 | return new DeepFTreeM(theMonoid, 467 | new Digit(theMonoid, theSingle), 468 | new EmptyFTreeM, M>(theMonoid), 469 | new Digit(theMonoid, t)); 470 | } 471 | 472 | public override IEnumerable ToSequence() 473 | { 474 | List newL = new List(); 475 | newL.Add(theSingle); 476 | return newL; 477 | } 478 | 479 | public override IEnumerable ToSequenceR() 480 | { 481 | List newR = new List(); 482 | newR.Add(theSingle); 483 | return newR; 484 | } 485 | 486 | public override ViewL LeftView() 487 | { 488 | return new ViewL(theSingle, 489 | new EmptyFTreeM(theMonoid) 490 | ); 491 | } 492 | 493 | public override ViewR RightView() 494 | { 495 | return new ViewR(new EmptyFTreeM(theMonoid), 496 | theSingle 497 | ); 498 | } 499 | 500 | public override FTreeM App2(List ts, FTreeM rightFT) 501 | { 502 | FTreeM resultFT = rightFT; 503 | 504 | for (int i = ts.Count - 1; i >= 0; i--) 505 | { 506 | resultFT = resultFT.Push_Front(ts[i]); 507 | } 508 | 509 | return resultFT.Push_Front(theSingle); 510 | } 511 | 512 | public override FTreeM Merge(FTreeM rightFT) 513 | { 514 | return rightFT.Push_Front(theSingle); 515 | } 516 | 517 | } 518 | 519 | public partial class DeepFTreeM : FTreeM where T : IMeasured 520 | { 521 | public Monoid theMonoid; 522 | 523 | protected M PreCalcMeasure; 524 | 525 | protected Digit frontDig; 526 | protected FTreeM, M> innerFT; 527 | protected Digit backDig; 528 | 529 | public DeepFTreeM(Monoid aMonoid, 530 | 531 | Digit frontDig, 532 | FTreeM, M> innerFT, 533 | Digit backDig) 534 | { 535 | if (frontDig.digNodes.Count > 0) 536 | { 537 | theMonoid = aMonoid; 538 | 539 | this.frontDig = frontDig; 540 | this.innerFT = innerFT; 541 | this.backDig = backDig; 542 | 543 | PreCalcMeasure = theMonoid.zero; 544 | PreCalcMeasure = theMonoid.theOp(PreCalcMeasure, frontDig.Measure()); 545 | PreCalcMeasure = theMonoid.theOp(PreCalcMeasure, innerFT.Measure()); 546 | PreCalcMeasure = theMonoid.theOp(PreCalcMeasure, backDig.Measure()); 547 | } 548 | else 549 | { 550 | throw new Exception("The DeepFTree() constructor is passed an empty frontDig !"); 551 | } 552 | } 553 | 554 | public override Monoid treeMonoid 555 | { 556 | get { return theMonoid; } 557 | } 558 | 559 | public override M Measure() 560 | { 561 | return PreCalcMeasure; 562 | } 563 | 564 | public override FTreeM Reverse() 565 | { 566 | Digit newFrontDig = ReverseDigit(backDig); 567 | Digit newBackDig = ReverseDigit(frontDig); 568 | 569 | if (innerFT is EmptyFTreeM, M>) 570 | return new 571 | DeepFTreeM(theMonoid, newFrontDig, innerFT, newBackDig); 572 | //else 573 | if (innerFT is SingleFTreeM, M>) 574 | { 575 | return new DeepFTreeM 576 | (theMonoid, 577 | newFrontDig, 578 | new SingleFTreeM, M> 579 | (theMonoid, 580 | ReverseNode(innerFT.LeftView().head) 581 | ), 582 | newBackDig 583 | ); 584 | } 585 | //else innerFT is a Deep tree 586 | DeepFTreeM, M> revDeepInner = 587 | (DeepFTreeM, M>) 588 | ( 589 | ((DeepFTreeM, M>)innerFT).Reverse() 590 | ); 591 | 592 | List> newFrontNodes = new List>(); 593 | List> newBackNodes = new List>(); 594 | 595 | foreach (Node node in revDeepInner.frontDig.digNodes) 596 | newFrontNodes.Add(ReverseNode(node)); 597 | 598 | foreach (Node node in revDeepInner.backDig.digNodes) 599 | newBackNodes.Add(ReverseNode(node)); 600 | 601 | DeepFTreeM, M> reversedInner = 602 | new DeepFTreeM, M> 603 | (theMonoid, 604 | new DeepFTreeM, M>.Digit, M> 605 | (theMonoid, newFrontNodes), 606 | revDeepInner.innerFT, 607 | new DeepFTreeM, M>.Digit, M> 608 | (theMonoid, newBackNodes) 609 | ); 610 | 611 | return new DeepFTreeM 612 | (theMonoid, 613 | ReverseDigit(backDig), 614 | reversedInner, 615 | ReverseDigit(frontDig) 616 | ); 617 | } 618 | 619 | private static Node ReverseNode(Node aNode) 620 | { 621 | List theNodes = new List(aNode.theNodes); 622 | theNodes.Reverse(); 623 | 624 | return new Node(aNode.theMonoid, theNodes); 625 | } 626 | 627 | public override ViewL LeftView() 628 | { 629 | T head = frontDig.digNodes[0]; 630 | 631 | List newFront = new List(frontDig.digNodes); 632 | newFront.RemoveAt(0); 633 | 634 | return new ViewL(head, 635 | FTreeM.Create(newFront, innerFT, backDig) 636 | ); 637 | } 638 | 639 | public override ViewR RightView() 640 | { 641 | int lastIndex = backDig.digNodes.Count - 1; 642 | T last = backDig.digNodes[lastIndex]; 643 | 644 | List newBack = new List(backDig.digNodes); 645 | newBack.RemoveAt(lastIndex); 646 | 647 | return new ViewR(FTreeM.CreateR(frontDig, innerFT, newBack), 648 | last 649 | ); 650 | } 651 | 652 | public override FTreeM Push_Front(T t) 653 | { 654 | if (frontDig.digNodes.Count == 4) 655 | { 656 | List newFront = new List(frontDig.digNodes); 657 | newFront.RemoveAt(0); 658 | 659 | return new DeepFTreeM 660 | (theMonoid, 661 | new Digit(theMonoid, 662 | t, frontDig.digNodes[0]), 663 | innerFT.Push_Front(new Node(theMonoid, newFront)), 664 | backDig 665 | ); 666 | } 667 | else //less than four digits in front -- will accomodate one more 668 | { 669 | List newFront = new List(frontDig.digNodes); 670 | newFront.Insert(0, t); 671 | 672 | return new DeepFTreeM 673 | (theMonoid, 674 | new Digit(theMonoid, 675 | newFront), 676 | innerFT, backDig); 677 | } 678 | } 679 | 680 | public override FTreeM Push_Back(T t) 681 | { 682 | int cntbackDig = backDig.digNodes.Count; 683 | 684 | 685 | if (backDig.digNodes.Count == 4) 686 | { 687 | List newBack = new List(backDig.digNodes); 688 | newBack.RemoveAt(cntbackDig - 1); 689 | 690 | return new DeepFTreeM 691 | (theMonoid, 692 | frontDig, 693 | innerFT.Push_Back(new Node(theMonoid, newBack)), 694 | new Digit(theMonoid, 695 | backDig.digNodes[cntbackDig - 1], 696 | t) 697 | ); 698 | 699 | } 700 | else //less than three digits at the back -- will accomodate one more 701 | { 702 | List newBack = new List(backDig.digNodes); 703 | newBack.Add(t); 704 | 705 | return new DeepFTreeM 706 | (theMonoid, 707 | frontDig, 708 | innerFT, 709 | new Digit(theMonoid, newBack)); 710 | } 711 | 712 | } 713 | 714 | public override IEnumerable ToSequence() 715 | { 716 | ViewL lView = LeftView(); 717 | 718 | yield return lView.head; 719 | 720 | foreach (T t in lView.ftTail.ToSequence()) 721 | yield return t; 722 | } 723 | 724 | public override IEnumerable ToSequenceR() 725 | { 726 | ViewR rView = RightView(); 727 | 728 | yield return rView.last; 729 | 730 | foreach (T t in rView.ftInit.ToSequenceR()) 731 | yield return t; 732 | } 733 | 734 | 735 | public override FTreeM App2(List ts, FTreeM rightFT) 736 | { 737 | if (rightFT is EmptyFTreeM) 738 | { 739 | FTreeM resultFT = this; 740 | 741 | foreach (T t in ts) 742 | { 743 | resultFT = resultFT.Push_Back(t); 744 | } 745 | 746 | return resultFT; 747 | 748 | } 749 | 750 | else if (rightFT is SingleFTreeM) 751 | { 752 | FTreeM resultFT = this; 753 | 754 | foreach (T t in ts) 755 | { 756 | resultFT = resultFT.Push_Back(t); 757 | } 758 | 759 | return resultFT.Push_Back(rightFT.LeftView().head); 760 | 761 | } 762 | 763 | else // the right tree is also a deep tree 764 | { 765 | DeepFTreeM deepRight = rightFT as DeepFTreeM; 766 | 767 | List cmbList = new List(backDig.digNodes); 768 | 769 | cmbList.AddRange(ts); 770 | 771 | cmbList.AddRange(deepRight.frontDig.digNodes); 772 | 773 | FTreeM resultFT = 774 | new DeepFTreeM 775 | (theMonoid, 776 | frontDig, 777 | innerFT.App2(FTreeM.ListOfNodes 778 | (theMonoid, cmbList), 779 | deepRight.innerFT 780 | ), 781 | deepRight.backDig 782 | ); 783 | 784 | return resultFT; 785 | } 786 | } 787 | 788 | 789 | public override FTreeM Merge(FTreeM rightFT) 790 | { 791 | List emptyList = new List(); 792 | 793 | return App2(emptyList, rightFT); 794 | } 795 | 796 | } 797 | 798 | } 799 | -------------------------------------------------------------------------------- /src/FingerTree/FingerTreeSplits.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Linq; 5 | 6 | namespace FingerTree 7 | { 8 | // Types: 9 | // U -- the type of Containers that can be split 10 | // T -- the type of elements in a container of type U 11 | // V -- the type of the Measure-value when an element is measured 12 | public class Split where U : ISplittable where T : IMeasured 13 | { 14 | public U left; 15 | 16 | public T splitItem; 17 | 18 | public U right; 19 | 20 | public Split(U left, T splitItem, U right) 21 | { 22 | this.left = left; 23 | this.splitItem = splitItem; 24 | this.right = right; 25 | } 26 | } 27 | 28 | public delegate bool MPredicate (V v); 29 | 30 | 31 | public class Pair 32 | { 33 | public T first; 34 | public V second; 35 | 36 | public Pair(T first, V second) 37 | { 38 | this.first = first; 39 | this.second = second; 40 | } 41 | } 42 | 43 | public abstract partial class FTreeM where T : IMeasured 44 | { 45 | public abstract Split, T, M> Split(MPredicate predicate, M acc); 46 | 47 | public abstract Pair, FTreeM> SeqSplit(MPredicate predicate); 48 | 49 | public FTreeM takeUntil(MPredicate predicate) 50 | { 51 | return SeqSplit(predicate).first; 52 | } 53 | 54 | public FTreeM dropUntil(MPredicate predicate) 55 | { 56 | return SeqSplit(predicate).second; 57 | } 58 | 59 | public T Lookup(MPredicate predicate, M acc) 60 | { 61 | return dropUntil(predicate).LeftView().head; 62 | } 63 | 64 | public partial class Digit : ISplittable 65 | where U : IMeasured 66 | { 67 | // Assumption: predicate is false on the left end 68 | // and true on the right end of the container 69 | public Split, U, V> Split(MPredicate predicate, V acc) 70 | { 71 | int cnt = digNodes.Count; 72 | 73 | if (cnt == 0) 74 | throw new Exception("Error: Split of an empty Digit attempted!"); 75 | //else 76 | U headItem = digNodes[0]; 77 | if(cnt == 1) 78 | return new Split,U,V> 79 | (new Digit(theMonoid, new List()), 80 | headItem, 81 | new Digit(theMonoid, new List()) 82 | ); 83 | //else 84 | List digNodesTail = new List(digNodes.GetRange(1, cnt - 1)); 85 | Digit digitTail = new Digit(theMonoid, digNodesTail); 86 | 87 | V acc1 = theMonoid.theOp(acc, headItem.Measure()); 88 | if (predicate(acc1)) 89 | return new Split, U, V> 90 | (new Digit(theMonoid, new List()), 91 | headItem, 92 | digitTail 93 | ); 94 | //else 95 | Split,U,V> tailSplit = digitTail.Split(predicate, acc1); 96 | 97 | tailSplit.left.digNodes.Insert(0, headItem); 98 | 99 | return tailSplit; 100 | } 101 | } 102 | } 103 | 104 | public partial class EmptyFTreeM : FTreeM where T : IMeasured 105 | { 106 | public override Split, T, M> Split(MPredicate predicate, M acc) 107 | { 108 | throw new Exception("Error: Split attempted on an EmptyFTreeM !"); 109 | } 110 | 111 | public override Pair, FTreeM> SeqSplit(MPredicate predicate) 112 | { 113 | return new Pair,FTreeM> 114 | (new EmptyFTreeM(theMonoid), 115 | new EmptyFTreeM(theMonoid) 116 | ); 117 | } 118 | } 119 | 120 | public partial class SingleFTreeM : FTreeM where T : IMeasured 121 | { 122 | public override Split, T, M> Split(MPredicate predicate, M acc) 123 | { 124 | return new Split,T,M> 125 | (new EmptyFTreeM(theMonoid), 126 | theSingle, 127 | new EmptyFTreeM(theMonoid) 128 | ); 129 | } 130 | 131 | public override Pair, FTreeM> SeqSplit(MPredicate predicate) 132 | { 133 | M theMeasure = theSingle.Measure(); 134 | 135 | if(predicate(theMeasure)) 136 | return new Pair,FTreeM> 137 | (new EmptyFTreeM(theMonoid), 138 | this 139 | ); 140 | //else 141 | return new Pair, FTreeM> 142 | (this, 143 | new EmptyFTreeM(theMonoid) 144 | ); 145 | 146 | 147 | } 148 | 149 | } 150 | 151 | public partial class DeepFTreeM : FTreeM where T : IMeasured 152 | { 153 | public override Split, T, M> Split(MPredicate predicate, M acc) 154 | { 155 | M vPr = theMonoid.theOp(acc, frontDig.Measure()); 156 | 157 | if(predicate(vPr)) 158 | { 159 | Split, T, M> 160 | frontSplit = frontDig.Split(predicate, acc); 161 | 162 | return new Split, T, M> 163 | (FTreeM.FromSequence(frontSplit.left.digNodes, theMonoid), 164 | frontSplit.splitItem, 165 | FTreeM.Create(frontSplit.right.digNodes, innerFT, backDig) 166 | ); 167 | } 168 | //else 169 | M vM = theMonoid.theOp(vPr, innerFT.Measure()); 170 | 171 | if (predicate(vM)) 172 | { 173 | var midSplit = innerFT.Split(predicate, vPr); 174 | 175 | var midLeft = midSplit.left; 176 | var midItem = midSplit.splitItem; 177 | 178 | var splitMidLeft = 179 | (new Digit(theMonoid, midItem.theNodes)).Split 180 | (predicate, 181 | theMonoid.theOp(vPr, midLeft.Measure()) 182 | ); 183 | 184 | T finalsplitItem = splitMidLeft.splitItem; 185 | 186 | FTreeM finalLeftTree = 187 | FTreeM.CreateR(frontDig, midLeft, splitMidLeft.left.digNodes); 188 | 189 | FTreeM finalRightTree = 190 | FTreeM.Create(splitMidLeft.right.digNodes, midSplit.right, backDig); 191 | 192 | return new Split, T, M> 193 | (finalLeftTree, finalsplitItem, finalRightTree); 194 | 195 | } 196 | //else 197 | Split, T, M> 198 | backSplit = backDig.Split(predicate, vM); 199 | 200 | return new Split, T, M> 201 | (FTreeM.CreateR(frontDig, innerFT, backSplit.left.digNodes), 202 | backSplit.splitItem, 203 | FTreeM.FromSequence(backSplit.right.digNodes, theMonoid) 204 | ); 205 | } 206 | 207 | public override Pair, FTreeM> SeqSplit(MPredicate predicate) 208 | { 209 | if(!predicate(Measure())) 210 | return new Pair, FTreeM> 211 | (this, 212 | new EmptyFTreeM(theMonoid) 213 | ); 214 | //else 215 | Split, T, M> theSplit = Split(predicate, theMonoid.zero); 216 | 217 | return new Pair, FTreeM> 218 | (theSplit.left, theSplit.right.Push_Front(theSplit.splitItem) 219 | ); 220 | } 221 | 222 | } 223 | } -------------------------------------------------------------------------------- /src/FingerTree/OrderedSequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace FingerTree 6 | { 7 | public class Key where V : IComparable 8 | { 9 | public delegate V getKey(T t); 10 | 11 | // maybe we shouldn't care for NoKey, as this is too theoretic 12 | public V NoKey; 13 | 14 | public getKey KeyAssign; 15 | 16 | public Key(V noKey, getKey KeyAssign) 17 | { 18 | NoKey = noKey; 19 | this.KeyAssign = KeyAssign; 20 | } 21 | } 22 | 23 | public class KeyMonoid where V : IComparable 24 | { 25 | public Key KeyObj; 26 | 27 | public Monoid theMonoid; 28 | 29 | public V aNextKeyOp(V v1, V v2) 30 | { 31 | return (v2.CompareTo(KeyObj.NoKey) == 0) ? v1 : v2; 32 | } 33 | 34 | //constructor 35 | public KeyMonoid(Key KeyObj) 36 | { 37 | this.KeyObj = KeyObj; 38 | 39 | this.theMonoid = 40 | new Monoid(KeyObj.NoKey, new Monoid.monOp(aNextKeyOp)); 41 | } 42 | } 43 | 44 | public class OrdElem : Elem where V : IComparable 45 | { 46 | public Key KeyObj; 47 | 48 | public OrdElem(T t, Key KeyObj) 49 | : base(t) 50 | { 51 | this.KeyObj = KeyObj; 52 | } 53 | 54 | public override V Measure() 55 | { 56 | return KeyObj.KeyAssign(this.Element); 57 | } 58 | } 59 | 60 | public class OrderedSequence where V : IComparable 61 | { 62 | private FTreeM, V> treeRep; 63 | 64 | private Key KeyObj; 65 | 66 | 67 | private static bool theLTMethod2(V v1, V v2) 68 | { 69 | return v1.CompareTo(v2) < 0; 70 | } 71 | 72 | private static bool theLEMethod2(V v1, V v2) 73 | { 74 | return v1.CompareTo(v2) <= 0; 75 | } 76 | 77 | private static bool theGTMethod2(V v1, V v2) 78 | { 79 | return v1.CompareTo(v2) > 0; 80 | } 81 | 82 | // Constructor 1 83 | public OrderedSequence(Key KeyObj) 84 | { 85 | treeRep = new EmptyFTreeM, V>(new KeyMonoid(KeyObj).theMonoid); 86 | this.KeyObj = KeyObj; 87 | } 88 | 89 | // Constructor 2 90 | public OrderedSequence(Key KeyObj, IEnumerable aList) 91 | { 92 | this.KeyObj = KeyObj; 93 | 94 | OrderedSequence tempSeq = new OrderedSequence(KeyObj); 95 | treeRep = new EmptyFTreeM, V>(new KeyMonoid(KeyObj).theMonoid); 96 | 97 | foreach (T t in aList) 98 | tempSeq = tempSeq.Push_Back(new OrdElem(t, KeyObj)); 99 | 100 | treeRep = tempSeq.treeRep; 101 | } 102 | 103 | // Constructor 3 104 | protected OrderedSequence(Key KeyObj, FTreeM, V> anOrdElTree) 105 | { 106 | this.KeyObj = KeyObj; 107 | treeRep = anOrdElTree; 108 | } 109 | 110 | 111 | OrderedSequence Push_Back(OrdElem ordEl) 112 | { 113 | var viewR = treeRep.RightView(); 114 | 115 | if (viewR != null) 116 | { 117 | if (viewR.last.Measure() 118 | .CompareTo(ordEl.Measure()) 119 | > 0) 120 | throw new Exception( 121 | "OrderedSequence Error: PushBack() of an element less than the biggest seq el." 122 | ); 123 | } 124 | //else 125 | return new OrderedSequence(KeyObj, treeRep.Push_Back(ordEl)); 126 | } 127 | 128 | OrderedSequence Push_Front(OrdElem ordEl) 129 | { 130 | var viewL = treeRep.LeftView(); 131 | 132 | if (viewL != null) 133 | { 134 | if (treeRep.LeftView().head.Measure() 135 | .CompareTo(ordEl.Measure()) 136 | < 0) 137 | throw new Exception( 138 | "OrderedSequence Error: PushFront() of an element greater than the smallest seq el." 139 | ); 140 | } 141 | //else 142 | return new OrderedSequence(KeyObj, treeRep.Push_Front(ordEl)); 143 | } 144 | 145 | public Pair, OrderedSequence> 146 | Partition(V vK) 147 | { 148 | Pair, V>, FTreeM, V>> baseSeqSplit 149 | = 150 | treeRep.SeqSplit(new MPredicate 151 | (FP.Curry 152 | (theLEMethod2, vK) 153 | ) 154 | ); 155 | 156 | OrderedSequence left 157 | = new OrderedSequence(KeyObj, baseSeqSplit.first); 158 | 159 | OrderedSequence right 160 | = new OrderedSequence(KeyObj, baseSeqSplit.second); 161 | 162 | return new 163 | Pair, 164 | OrderedSequence 165 | > 166 | (left, right); 167 | } 168 | 169 | public OrderedSequence Insert(T t) 170 | { 171 | Pair, OrderedSequence> tPart 172 | = Partition(KeyObj.KeyAssign(t)); 173 | 174 | OrdElem tOrd = new OrdElem(t, KeyObj); 175 | 176 | return new 177 | OrderedSequence 178 | ( 179 | KeyObj, 180 | tPart.first.treeRep.Merge(tPart.second.treeRep.Push_Front(tOrd)) 181 | ); 182 | 183 | } 184 | 185 | public OrderedSequence DeleteAll(T t) 186 | { 187 | V vK = KeyObj.KeyAssign(t); // the Key of t 188 | 189 | Pair, OrderedSequence> 190 | tPart = Partition(vK); 191 | 192 | OrderedSequence seqPrecedestheEl = tPart.first; 193 | OrderedSequence seqStartsWiththeEl = tPart.second; 194 | 195 | Pair, V>, FTreeM, V>> lastTreeSplit 196 | = 197 | seqStartsWiththeEl.treeRep.SeqSplit 198 | (new MPredicate 199 | (FP.Curry 200 | (theLTMethod2, vK) 201 | ) 202 | ); 203 | 204 | //OrderedSequence seqBeyondtheEl = 205 | // new OrderedSequence(KeyObj, lastTreeSplit.second); 206 | 207 | return new OrderedSequence 208 | (KeyObj, 209 | seqPrecedestheEl.treeRep.Merge(lastTreeSplit.second) 210 | ); 211 | } 212 | 213 | public OrderedSequence Merge(OrderedSequence ordSeq2) 214 | { 215 | FTreeM, V> theMergedTree 216 | = OrdMerge(treeRep, ordSeq2.treeRep); 217 | 218 | return new OrderedSequence 219 | (KeyObj, theMergedTree); 220 | } 221 | 222 | private static FTreeM, V> 223 | OrdMerge(FTreeM, V> ordTree1, 224 | FTreeM, V> ordTree2 225 | ) 226 | { 227 | ViewL, V> lView2 = ordTree2.LeftView(); 228 | 229 | if (lView2 == null) 230 | return ordTree1; 231 | //else 232 | OrdElem bHead = lView2.head; 233 | FTreeM, V> bTail = lView2.ftTail; 234 | 235 | // Split ordTree1 on elems <= and then > bHead 236 | Pair, V>, FTreeM, V>> 237 | tree1Split = ordTree1.SeqSplit 238 | (new MPredicate 239 | (FP.Curry (theLEMethod2, bHead.Measure())) 240 | ); 241 | 242 | FTreeM, V> leftTree1 = tree1Split.first; 243 | FTreeM, V> rightTree1 = tree1Split.second; 244 | 245 | // OrdMerge the tail of ordTree2 246 | // with the right-split part of ordTree1 247 | FTreeM, V> 248 | mergedRightparts = OrdMerge(bTail, rightTree1); 249 | 250 | return leftTree1.Merge(mergedRightparts.Push_Front(bHead)); 251 | } 252 | 253 | public IEnumerable ToSequence() 254 | { 255 | foreach (OrdElem ordEl in treeRep.ToSequence()) 256 | yield return ordEl.Element; 257 | } 258 | } 259 | } -------------------------------------------------------------------------------- /src/FingerTree/PriorityQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace FingerTree 6 | { 7 | public static class Prio 8 | { 9 | public static Monoid theMonoid = 10 | new Monoid(double.NegativeInfinity, new Monoid.monOp(aMaxOp)); 11 | 12 | public static double aMaxOp(double d1, double d2) 13 | { 14 | return (d1 > d2) ? d1 : d2; 15 | } 16 | 17 | } 18 | 19 | public class CompElem : Elem 20 | { 21 | private double dblRep; 22 | 23 | public CompElem(T t) 24 | : base(t) 25 | { 26 | dblRep = double.Parse(t.ToString()); 27 | } 28 | 29 | public override double Measure() 30 | { 31 | return this.dblRep; 32 | } 33 | } 34 | 35 | public class PriorityQueue : FTreeM, double> 36 | { 37 | private FTreeM, double> treeRep = 38 | new EmptyFTreeM, double>(Prio.theMonoid); 39 | 40 | private static bool theLessOrEqMethod2(double d1, double d2) 41 | { 42 | return d1 <= d2; 43 | } 44 | 45 | public PriorityQueue(IEnumerable aList) 46 | { 47 | foreach (T t in aList) 48 | treeRep = treeRep.Push_Back(new CompElem(t)); 49 | } 50 | 51 | public PriorityQueue(FTreeM, double> elemTree) 52 | { 53 | treeRep = elemTree; 54 | } 55 | 56 | public override Monoid treeMonoid 57 | { 58 | get 59 | { 60 | return treeRep.treeMonoid; 61 | } 62 | } 63 | 64 | public override double Measure() 65 | { 66 | return treeRep.Measure(); 67 | } 68 | 69 | public override FTreeM, double> Push_Front(CompElem elemT) 70 | { 71 | return new PriorityQueue 72 | (treeRep.Push_Front(elemT)); 73 | } 74 | 75 | 76 | public override FTreeM, double> Push_Back(CompElem elemT) 77 | { 78 | return new PriorityQueue 79 | (treeRep.Push_Back(elemT)); 80 | } 81 | 82 | public override IEnumerable> ToSequence() 83 | { 84 | return treeRep.ToSequence(); 85 | } 86 | 87 | public override IEnumerable> ToSequenceR() 88 | { 89 | return treeRep.ToSequenceR(); 90 | } 91 | 92 | 93 | public override ViewL, double> LeftView() 94 | { 95 | ViewL, double> internLView = treeRep.LeftView(); 96 | 97 | internLView.ftTail = new PriorityQueue(internLView.ftTail); 98 | 99 | return internLView; 100 | } 101 | 102 | public override ViewR, double> RightView() 103 | { 104 | ViewR, double> internRView = treeRep.RightView(); 105 | internRView.ftInit = new PriorityQueue(internRView.ftInit); 106 | 107 | return internRView; 108 | } 109 | 110 | public override FTreeM, double> Merge(FTreeM, double> rightFT) 111 | { 112 | if(!(rightFT is PriorityQueue)) 113 | throw new Exception("Error: PriQue merge with non-PriQue attempted!"); 114 | //else 115 | return new PriorityQueue 116 | ( 117 | treeRep.Merge(((PriorityQueue)rightFT).treeRep) 118 | ); 119 | } 120 | 121 | public override Split, double>, CompElem, double> 122 | Split(MPredicate predicate, double acc) 123 | { 124 | Split, double>, CompElem, double> internSplit 125 | = treeRep.Split(predicate, acc); 126 | 127 | internSplit.left = 128 | new PriorityQueue(internSplit.left); 129 | internSplit.right = 130 | new PriorityQueue(internSplit.right); 131 | 132 | return internSplit; 133 | 134 | } 135 | 136 | public override Pair, double>, FTreeM, double>> 137 | SeqSplit(MPredicate predicate) 138 | { 139 | Pair, double>, FTreeM, double>> internPair 140 | = treeRep.SeqSplit(predicate); 141 | 142 | internPair.first = new PriorityQueue(internPair.first); 143 | internPair.second = new PriorityQueue(internPair.second); 144 | 145 | return internPair; 146 | } 147 | 148 | public override FTreeM, double> 149 | App2(List> ts, FTreeM, double> rightFT) 150 | { 151 | return treeRep.App2(ts, rightFT); 152 | } 153 | 154 | public Pair> extractMax() 155 | { 156 | var trSplit = 157 | treeRep.Split(new MPredicate 158 | (FP.Curry 159 | (theLessOrEqMethod2, treeRep.Measure()) 160 | ), 161 | Prio.theMonoid.zero 162 | ); 163 | return new Pair> 164 | (trSplit.splitItem.Element, 165 | new PriorityQueue(trSplit.left.Merge(trSplit.right)) 166 | ); 167 | } 168 | } 169 | 170 | } -------------------------------------------------------------------------------- /src/FingerTree/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("FingerTree")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("At Home")] 12 | [assembly: AssemblyProduct("FingerTree")] 13 | [assembly: AssemblyCopyright("Copyright © At Home 2008")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("48be7d20-3eb1-4d7e-97c5-f17fcbe0d716")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /src/FingerTree/RandAccessSequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace FingerTree 6 | { 7 | 8 | public static class Size 9 | { 10 | public static Monoid theMonoid = 11 | new Monoid(0, new Monoid.monOp(anAddOp)); 12 | 13 | public static uint anAddOp(uint s1, uint s2) 14 | { 15 | return s1 + s2; 16 | } 17 | 18 | } 19 | 20 | public static class FP 21 | { 22 | public static Func Curry(this Func func, X x) 23 | { 24 | return (y) => func(x, y); 25 | } 26 | 27 | } 28 | 29 | public abstract class Elem : IMeasured 30 | { 31 | protected T theElem; 32 | 33 | public Elem(T t) 34 | { 35 | theElem = t; 36 | } 37 | 38 | public T Element 39 | { 40 | get { return theElem; } 41 | } 42 | 43 | public abstract V Measure(); 44 | } 45 | 46 | public class SizedElem : Elem 47 | { 48 | public SizedElem(T t) : base(t) 49 | { 50 | } 51 | 52 | public override uint Measure() 53 | { 54 | return 1; 55 | } 56 | } 57 | 58 | public class Seq : FTreeM, uint> 59 | { 60 | public FTreeM, uint> treeRep = 61 | new EmptyFTreeM, uint>(Size.theMonoid); 62 | 63 | private static bool theLessThanIMethod2(uint n, uint i) 64 | { 65 | return n < i; 66 | } 67 | 68 | public Seq(IEnumerable aList) 69 | { 70 | foreach (T t in aList) 71 | treeRep = treeRep.Push_Back(new SizedElem(t)); 72 | } 73 | 74 | public Seq(FTreeM, uint> elemTree) 75 | { 76 | treeRep = elemTree; 77 | } 78 | 79 | public override Monoid treeMonoid 80 | { 81 | get 82 | { 83 | return treeRep.treeMonoid; 84 | } 85 | } 86 | 87 | public override uint Measure() 88 | { 89 | return treeRep.Measure(); 90 | } 91 | 92 | public override FTreeM, uint> Push_Front(SizedElem t) 93 | { 94 | return new Seq(treeRep.Push_Front(t)); 95 | } 96 | 97 | public override FTreeM, uint> Push_Back(SizedElem t) 98 | { 99 | return new Seq(treeRep.Push_Back(t)); 100 | } 101 | 102 | public override IEnumerable> ToSequence() 103 | { 104 | return treeRep.ToSequence(); 105 | } 106 | 107 | public override IEnumerable> ToSequenceR() 108 | { 109 | return treeRep.ToSequenceR(); 110 | } 111 | 112 | public override ViewL, uint> LeftView() 113 | { 114 | ViewL, uint> internLView = treeRep.LeftView(); 115 | 116 | internLView.ftTail = new Seq(internLView.ftTail); 117 | 118 | return internLView; 119 | } 120 | 121 | public override ViewR, uint> RightView() 122 | { 123 | ViewR, uint> internRView = treeRep.RightView(); 124 | internRView.ftInit = new Seq(internRView.ftInit); 125 | 126 | return internRView; 127 | } 128 | 129 | public override FTreeM, uint> Reverse() 130 | { 131 | return new Seq(treeRep.Reverse()); 132 | } 133 | 134 | public override FTreeM, uint> Merge(FTreeM, uint> rightFT) 135 | { 136 | //if (!(rightFT is Seq)) 137 | // throw new Exception("Error: Seq merge with non-Seq attempted!"); 138 | ////else 139 | return treeRep.Merge(rightFT); 140 | } 141 | 142 | public override Split, uint>, SizedElem, uint> 143 | Split(MPredicate predicate, uint acc) 144 | { 145 | Split, uint>, SizedElem, uint> internSplit 146 | = treeRep.Split(predicate, acc); 147 | 148 | internSplit.left = 149 | new Seq(internSplit.left); 150 | internSplit.right = 151 | new Seq(internSplit.right); 152 | 153 | return internSplit; 154 | } 155 | 156 | public override Pair, uint>, FTreeM, uint>> 157 | SeqSplit(MPredicate predicate) 158 | { 159 | Pair, uint>, FTreeM, uint>> internPair 160 | = treeRep.SeqSplit(predicate); 161 | 162 | internPair.first = new Seq(internPair.first); 163 | internPair.second = new Seq(internPair.second); 164 | 165 | return internPair; 166 | } 167 | 168 | public override FTreeM, uint> 169 | App2(List> ts, FTreeM, uint> rightFT) 170 | { 171 | return treeRep.App2(ts, rightFT); 172 | } 173 | 174 | 175 | 176 | public uint length 177 | { 178 | get { return treeRep.Measure(); } 179 | } 180 | 181 | public Pair, Seq> SplitAt(uint ind) 182 | { 183 | var treeSplit = 184 | treeRep.SeqSplit(new MPredicate 185 | (FP.Curry(theLessThanIMethod2, ind)) 186 | ); 187 | return new Pair, Seq> 188 | (new Seq(treeSplit.first), 189 | new Seq(treeSplit.second) 190 | ); 191 | } 192 | 193 | public T ElemAt(uint ind) 194 | { 195 | return treeRep.Split(new MPredicate 196 | (FP.Curry(theLessThanIMethod2, ind)), 197 | 0 198 | ).splitItem.Element; 199 | } 200 | 201 | public T this[uint index] 202 | { 203 | get 204 | { 205 | return ElemAt(index); 206 | } 207 | } 208 | 209 | public Seq InsertAt(uint index, T t) 210 | { 211 | if (index > length) 212 | throw new IndexOutOfRangeException 213 | (string.Format("Error: Attempt to insert at position: {0} " 214 | + "exceeding the length: {1} of this sequence.", 215 | index, 216 | length 217 | ) 218 | ); 219 | //else 220 | Pair, Seq> theSplit = this.SplitAt(index); 221 | 222 | return new Seq 223 | ( 224 | theSplit.first.Merge(theSplit.second.Push_Front(new SizedElem(t))) 225 | ); 226 | } 227 | 228 | public Seq RemoveAt(uint index) 229 | { 230 | if (index > length) 231 | throw new IndexOutOfRangeException 232 | (string.Format("Error: Attempt to remove at position: {0} " 233 | + "exceeding the length: {1} of this sequence.", 234 | index, 235 | length 236 | ) 237 | ); 238 | //else 239 | Pair, Seq> theSplit = this.SplitAt(index); 240 | 241 | return new Seq 242 | ( 243 | theSplit.first.treeRep.Merge(theSplit.second.treeRep.LeftView().ftTail) 244 | ); 245 | } 246 | 247 | } 248 | } 249 | -------------------------------------------------------------------------------- /src/TestDotNetCollection/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("TestDotNetCollection")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("At Home")] 12 | [assembly: AssemblyProduct("TestDotNetCollection")] 13 | [assembly: AssemblyCopyright("Copyright © At Home 2008")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("ea5ed4a4-be45-43db-b2ac-d985f12e7fee")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/TestDotNetCollection/TestCollAndString.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FSeq; 6 | 7 | namespace TestCollAndString 8 | { 9 | class TestPerformance 10 | { 11 | static void Main(string[] args) 12 | { 13 | int nLength = 100000; 14 | uint nOperations = 100000; 15 | 16 | StringTest strTest = new StringTest(nLength, nOperations); 17 | FStringTest fstrTest = new FStringTest(nLength, nOperations); 18 | 19 | double msecs13 = fstrTest.fstringRemove(); 20 | 21 | double msecs12 = fstrTest.fstringSubstring(); 22 | 23 | double msecs11 = fstrTest.fstringInsert(); 24 | 25 | double msecs10 = fstrTest.fstringConcat(); 26 | 27 | double msecs9 = strTest.stringSubstring(); 28 | 29 | double msecs8 = strTest.stringRemove(); 30 | 31 | double msecs7 = strTest.stringInsert(); 32 | 33 | double msecs6 = strTest.stringConcat(); 34 | 35 | List intContent = new List(); 36 | 37 | for (UInt32 i = 0; i < nLength; i++) 38 | intContent.Add(i); 39 | 40 | CollectionTest shortPerfTest = new 41 | CollectionTest(nLength, nOperations, intContent); 42 | 43 | double msecs18 = shortPerfTest.fseqReverse(); 44 | 45 | double msecs17 = shortPerfTest.fseqTake(); 46 | 47 | double msecs16 = shortPerfTest.fseqSkip(); 48 | 49 | double msecs15 = shortPerfTest.fseqElementAt(); 50 | 51 | double msecs14 = shortPerfTest.fseqConcat(); 52 | 53 | double msecs5 = shortPerfTest.colReverse(); 54 | 55 | double msecs4 = shortPerfTest.colTake(); 56 | 57 | double msecs3 = shortPerfTest.colSkip(); 58 | 59 | double msecs2 = shortPerfTest.colElementAt(); 60 | 61 | double msecs = shortPerfTest.colConcat(); 62 | } 63 | } 64 | 65 | public class FStringTest 66 | { 67 | public UInt32 nOperations = 0; 68 | public int nStringLength = 100000; 69 | public FString theString = new FString(); 70 | public FString theString2 = new FString(); 71 | 72 | public FStringTest(int contLength, UInt32 nOperations) 73 | { 74 | this.nOperations = nOperations; 75 | this.nStringLength = contLength; 76 | 77 | FString str100Filler = new FString( 78 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/!@#$%^*()-_+=|?.,`~абгдежзийклмнопрст" 79 | ); 80 | 81 | for (int i = 0; i < contLength / str100Filler.Length(); i++) 82 | { 83 | theString = theString.Merge(str100Filler); 84 | theString2 = theString2.Merge(str100Filler); 85 | } 86 | } 87 | 88 | public double fstringConcat() 89 | { 90 | Double milliSeconds = 0D; 91 | char c; 92 | 93 | DateTime start = DateTime.Now; 94 | for (UInt32 i = 0; i < nOperations; i++) 95 | { 96 | { 97 | FString strConcat = theString.concat(theString2); 98 | 99 | c = strConcat.itemAt(2 * nStringLength - 1); 100 | } 101 | } 102 | DateTime end = DateTime.Now; 103 | 104 | milliSeconds += (end - start).TotalMilliseconds; 105 | 106 | return milliSeconds; 107 | } 108 | 109 | public double fstringInsert() 110 | { 111 | Double milliSeconds = 0D; 112 | char c; 113 | 114 | 115 | int halfLength = nStringLength / 2; 116 | 117 | DateTime start = DateTime.Now; 118 | for (UInt32 i = 0; i < nOperations; i++) 119 | { 120 | { 121 | FString strInsert = theString.insert(nStringLength - 1000, theString); 122 | 123 | c = strInsert.itemAt(2 * nStringLength - 1); 124 | } 125 | } 126 | DateTime end = DateTime.Now; 127 | 128 | milliSeconds += (end - start).TotalMilliseconds; 129 | 130 | return milliSeconds; 131 | } 132 | 133 | public double fstringRemove() 134 | { 135 | Double milliSeconds = 0D; 136 | 137 | char c = ' '; 138 | 139 | DateTime start = DateTime.Now; 140 | for (UInt32 i = 0; i < nOperations; i++) 141 | { 142 | { 143 | FString fstrSubstr = theString.remove(1000, nStringLength - 100); 144 | 145 | c = fstrSubstr.itemAt((int)fstrSubstr.Length() -1); 146 | } 147 | } 148 | DateTime end = DateTime.Now; 149 | 150 | milliSeconds += (end - start).TotalMilliseconds; 151 | 152 | return milliSeconds; 153 | 154 | } 155 | 156 | 157 | public double fstringSubstring() 158 | { 159 | Double milliSeconds = 0D; 160 | 161 | char c = ' '; 162 | 163 | DateTime start = DateTime.Now; 164 | for (UInt32 i = 0; i < nOperations; i++) 165 | { 166 | { 167 | FString fstrSubstr = theString.substring(1000, nStringLength - 1000); 168 | 169 | c = fstrSubstr.itemAt(nStringLength - 1005); 170 | } 171 | } 172 | DateTime end = DateTime.Now; 173 | 174 | milliSeconds += (end - start).TotalMilliseconds; 175 | 176 | return milliSeconds; 177 | } 178 | } 179 | 180 | public class StringTest 181 | { 182 | public UInt32 nOperations = 0; 183 | public int nStringLength = 100000; 184 | public string theString = string.Empty; 185 | public string theString2 = string.Empty; 186 | 187 | public StringTest(int contLength, UInt32 nOperations) 188 | { 189 | this.nOperations = nOperations; 190 | this.nStringLength = contLength; 191 | 192 | string str100Filler = 193 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/!@#$%^*()-_+=|?.,`~абгдежзийклмнопрст"; 194 | 195 | for (int i = 0; i < contLength / str100Filler.Length; i++) 196 | { 197 | theString += str100Filler; 198 | theString2 += str100Filler; 199 | } 200 | } 201 | 202 | public double stringConcat() 203 | { 204 | Double milliSeconds = 0D; 205 | 206 | char c; 207 | 208 | DateTime start = DateTime.Now; 209 | for (UInt32 i = 0; i < nOperations; i++) 210 | { 211 | { 212 | string strConcat = theString + theString2; 213 | 214 | c = strConcat[2 * nStringLength - 1]; 215 | } 216 | } 217 | DateTime end = DateTime.Now; 218 | 219 | milliSeconds += (end - start).TotalMilliseconds; 220 | 221 | return milliSeconds; 222 | } 223 | 224 | public double stringInsert() 225 | { 226 | Double milliSeconds = 0D; 227 | 228 | char c; 229 | 230 | DateTime start = DateTime.Now; 231 | for (UInt32 i = 0; i < nOperations; i++) 232 | { 233 | { 234 | string strInsert = theString.Insert(nStringLength - 1000, theString2); 235 | 236 | c = strInsert[2 * nStringLength - 1005]; 237 | } 238 | } 239 | DateTime end = DateTime.Now; 240 | 241 | milliSeconds += (end - start).TotalMilliseconds; 242 | 243 | return milliSeconds; 244 | } 245 | 246 | public double stringRemove() 247 | { 248 | Double milliSeconds = 0D; 249 | 250 | char c = ' '; 251 | 252 | DateTime start = DateTime.Now; 253 | for (UInt32 i = 0; i < nOperations; i++) 254 | { 255 | { 256 | string strRemove = theString.Remove(1000, nStringLength - 1000); 257 | 258 | c = strRemove[strRemove.Length - 1]; 259 | } 260 | } 261 | DateTime end = DateTime.Now; 262 | 263 | milliSeconds += (end - start).TotalMilliseconds; 264 | 265 | return milliSeconds; 266 | } 267 | 268 | public double stringSubstring() 269 | { 270 | Double milliSeconds = 0D; 271 | 272 | char c = ' '; 273 | 274 | DateTime start = DateTime.Now; 275 | for (UInt32 i = 0; i < nOperations; i++) 276 | { 277 | { 278 | string strSubstr = theString.Substring(1000, nStringLength - 1000); 279 | 280 | c = strSubstr[nStringLength - 1005]; 281 | } 282 | } 283 | DateTime end = DateTime.Now; 284 | 285 | milliSeconds += (end - start).TotalMilliseconds; 286 | 287 | return milliSeconds; 288 | } 289 | } 290 | 291 | public class CollectionTest 292 | { 293 | public UInt32 nOperations = 0; 294 | public int nContLength = 0; 295 | public IEnumerable theContainer = null; 296 | public FNSeq theFContainer = null; 297 | public FNSeq theFContainer2 = null; 298 | 299 | List theContainer2 = new List(); 300 | 301 | public CollectionTest(int contLength, UInt32 nOperations, IEnumerable contents) 302 | { 303 | this.nContLength = contLength; 304 | 305 | this.nOperations = nOperations; 306 | 307 | theContainer = contents; 308 | foreach(T t in contents) 309 | theContainer2.Add(t); 310 | 311 | theFContainer = new FNSeq(contents); 312 | theFContainer2 = new FNSeq(contents); 313 | 314 | } 315 | 316 | public double fseqConcat() 317 | { 318 | Double milliSeconds = 0D; 319 | T myT = default(T); 320 | 321 | DateTime start = DateTime.Now; 322 | for (UInt32 i = 0; i < nOperations; i++) 323 | { 324 | { 325 | FNSeq theResult = 326 | theFContainer.Merge(theFContainer2); 327 | myT = theResult.itemAt(nContLength + nContLength - 1); 328 | } 329 | } 330 | DateTime end = DateTime.Now; 331 | 332 | milliSeconds += (end - start).TotalMilliseconds; 333 | 334 | return milliSeconds; 335 | } 336 | 337 | public double colConcat() 338 | { 339 | Double milliSeconds = 0D; 340 | T myT = default(T); 341 | 342 | DateTime start = DateTime.Now; 343 | for (UInt32 i = 0; i < nOperations; i++) 344 | { 345 | { 346 | IEnumerable theResult = 347 | theContainer.Concat(theContainer); 348 | myT = theResult.ElementAt(nContLength + nContLength - 1); 349 | } 350 | } 351 | DateTime end = DateTime.Now; 352 | 353 | milliSeconds += (end - start).TotalMilliseconds; 354 | 355 | return milliSeconds; 356 | 357 | } 358 | 359 | public double fseqElementAt() 360 | { 361 | Double milliSeconds = 0D; 362 | 363 | int indMid = nContLength / 2; 364 | 365 | T elem; 366 | 367 | for (UInt32 i = 0; i < nOperations; i++) 368 | { 369 | DateTime start = DateTime.Now; 370 | { 371 | elem = theFContainer2.itemAt(indMid); 372 | } 373 | DateTime end = DateTime.Now; 374 | 375 | milliSeconds += (end - start).TotalMilliseconds; 376 | 377 | } 378 | return milliSeconds; 379 | } 380 | 381 | public double colElementAt() 382 | { 383 | Double milliSeconds = 0D; 384 | 385 | int indMid = nContLength / 2; 386 | 387 | T elem; 388 | 389 | for (UInt32 i = 0; i < nOperations; i++) 390 | { 391 | DateTime start = DateTime.Now; 392 | { 393 | elem = theContainer2[indMid]; 394 | } 395 | DateTime end = DateTime.Now; 396 | 397 | milliSeconds += (end - start).TotalMilliseconds; 398 | 399 | } 400 | return milliSeconds; 401 | 402 | } 403 | 404 | public double fseqSkip() 405 | { 406 | Double milliSeconds = 0D; 407 | T myT = default(T); 408 | 409 | int halfLength = nContLength / 2; 410 | 411 | DateTime start = DateTime.Now; 412 | for (UInt32 i = 0; i < nOperations; i++) 413 | { 414 | { 415 | FNSeq skippedFSeq = theFContainer.skip(halfLength); 416 | myT = skippedFSeq.itemAt(halfLength - 1); 417 | } 418 | } 419 | DateTime end = DateTime.Now; 420 | 421 | milliSeconds += (end - start).TotalMilliseconds; 422 | 423 | return milliSeconds; 424 | } 425 | 426 | public double fseqTake() 427 | { 428 | Double milliSeconds = 0D; 429 | T myT = default(T); 430 | 431 | int halfLength = nContLength / 2; 432 | 433 | DateTime start = DateTime.Now; 434 | for (UInt32 i = 0; i < nOperations; i++) 435 | { 436 | { 437 | FNSeq skippedFSeq = theFContainer.take(halfLength); 438 | myT = skippedFSeq.itemAt(halfLength - 1); 439 | } 440 | } 441 | DateTime end = DateTime.Now; 442 | 443 | milliSeconds += (end - start).TotalMilliseconds; 444 | 445 | return milliSeconds; 446 | } 447 | 448 | public double colSkip() 449 | { 450 | Double milliSeconds = 0D; 451 | T myT = default(T); 452 | 453 | int halfLength = nContLength / 2; 454 | 455 | DateTime start = DateTime.Now; 456 | for (UInt32 i = 0; i < nOperations; i++) 457 | { 458 | { 459 | IEnumerable skippedCol = theContainer.Skip(halfLength); 460 | myT = skippedCol.ElementAt(halfLength - 1); 461 | } 462 | } 463 | DateTime end = DateTime.Now; 464 | 465 | milliSeconds += (end - start).TotalMilliseconds; 466 | 467 | return milliSeconds; 468 | 469 | } 470 | 471 | public double colTake() 472 | { 473 | Double milliSeconds = 0D; 474 | T myT = default(T); 475 | 476 | int halfLength = nContLength / 2; 477 | 478 | DateTime start = DateTime.Now; 479 | for (UInt32 i = 0; i < nOperations; i++) 480 | { 481 | { 482 | IEnumerable takenCol = theContainer.Take(halfLength); 483 | myT = takenCol.ElementAt(halfLength - 1); 484 | } 485 | } 486 | DateTime end = DateTime.Now; 487 | 488 | milliSeconds += (end - start).TotalMilliseconds; 489 | 490 | return milliSeconds; 491 | 492 | } 493 | 494 | public double fseqReverse() 495 | { 496 | Double milliSeconds = 0D; 497 | T myT = default(T); 498 | 499 | DateTime start = DateTime.Now; 500 | for (UInt32 i = 0; i < nOperations; i++) 501 | { 502 | { 503 | FNSeq fsReversed = theFContainer.reverse(); 504 | myT = fsReversed.itemAt(nContLength - 1); 505 | } 506 | } 507 | DateTime end = DateTime.Now; 508 | 509 | milliSeconds += (end - start).TotalMilliseconds; 510 | 511 | return milliSeconds; 512 | } 513 | 514 | public double colReverse() 515 | { 516 | Double milliSeconds = 0D; 517 | T myT = default(T); 518 | 519 | DateTime start = DateTime.Now; 520 | for (UInt32 i = 0; i < nOperations; i++) 521 | { 522 | { 523 | IEnumerable takenCol = theContainer.Reverse(); 524 | myT = takenCol.ElementAt(nContLength - 1); 525 | } 526 | } 527 | DateTime end = DateTime.Now; 528 | 529 | milliSeconds += (end - start).TotalMilliseconds; 530 | 531 | return milliSeconds; 532 | 533 | } 534 | } 535 | } 536 | -------------------------------------------------------------------------------- /src/TestDotNetCollection/TestCollAndString.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {CFBD54BD-4EDD-4793-8913-4611B3D33682} 9 | Exe 10 | Properties 11 | TestDotNetCollection 12 | TestDotNetCollection 13 | v4.6.1 14 | 512 15 | 16 | 17 | 18 | 19 | 3.5 20 | 21 | 22 | 23 | true 24 | full 25 | false 26 | bin\Debug\ 27 | DEBUG;TRACE 28 | prompt 29 | 4 30 | false 31 | 32 | 33 | pdbonly 34 | true 35 | bin\Release\ 36 | TRACE 37 | prompt 38 | 4 39 | false 40 | 41 | 42 | 43 | 44 | 3.5 45 | 46 | 47 | 3.5 48 | 49 | 50 | 3.5 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | {51807875-b25d-4f87-8b8d-5100e5f26bad} 62 | FingerTree 63 | 64 | 65 | {9cdd93a8-3350-4f13-a001-f640bf28b96d} 66 | FNSeq 67 | 68 | 69 | 70 | 71 | 72 | 73 | 80 | -------------------------------------------------------------------------------- /src/TestDotNetCollection/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | --------------------------------------------------------------------------------