public void SplitLeaf(Leaf FullLeaf) { //Initialize values Leaf NewLeaf = new Leaf(NodeSize); int half = FullLeaf.Items.Count / 2; int newIndexValue = FullLeaf.Items[half]; int FullLeafCount = FullLeaf.Items.Count; for (int i = half; i < FullLeaf.Items.Count; i++) { //Add the values to another Leaf NewLeaf.Items.Add(FullLeaf.Items[i]); } for (int i = half; i < FullLeafCount; i++) { //Then remove them from the FullLeaf FullLeaf.Items.RemoveAt(half); } Index Temp = new Index(MainStack.Peek()); MainStack.Peek().Items.Add(newIndexValue); MainStack.Peek().LeafList.Add(NewLeaf); //Split Index if needed if (MainStack.Peek().Items.Count > NodeSize) { SplitIndex(); } }
private void Op2Over() { var a = MainStack.Peek(2); var b = MainStack.Peek(3); MainStack.Push(b); MainStack.Push(a); }
private void Op2Dup() { var a = MainStack.Peek(0); var b = MainStack.Peek(1); MainStack.Push(b); MainStack.Push(a); }
private void Op3Dup() { var a = MainStack.Peek(0); var b = MainStack.Peek(1); var c = MainStack.Peek(2); MainStack.Push(c); MainStack.Push(b); MainStack.Push(a); }
private void OpIfDup() { var shouldDuplicate = MainStack.PeekBool(); if (!shouldDuplicate) { return; } var bytes = MainStack.Peek(); MainStack.Push(bytes); }
/// <summary> /// Method for splitting a leaf and then adding to correct place in BTree /// </summary> /// <param name="FullLeaf">Represents the leaf to be split</param> public void SplitLeaf(Leaf FullLeaf) { //Initialize values Leaf NewLeaf = new Leaf(NodeSize); int half = FullLeaf.Items.Count / 2; int newIndexValue = FullLeaf.Items[half]; int FullLeafCount = FullLeaf.Items.Count; for (int i = half; i < FullLeaf.Items.Count; i++) { //Add the values to another Leaf NewLeaf.Items.Add(FullLeaf.Items[i]); } for (int i = half; i < FullLeafCount; i++) { //Then remove them from the FullLeaf FullLeaf.Items.RemoveAt(half); } //Insert new index item and leaf MainStack.Peek().Insert(newIndexValue, NewLeaf); //Reset Root if it has changed if (MainStack.Count == 1) { Root = new Index(MainStack.Peek()); } //Increment Nodes TreeLeaves++; NodeCount++; //Split Index if needed if (MainStack.Peek().Items.Count > NodeSize) { SplitIndex(); } }
/// <summary> /// Method splitting an index and adding it to the BTree /// </summary> public void SplitIndex() { if (MainStack.Peek().Items.Count <= NodeSize) { //Do nothing since all splitting is done } //Continue to Split else { #region Initialize values //Values to be set and used Index CurrentIndex = new Index(MainStack.Peek()); Index LeftIndex; Index CenterIndex = new Index(NodeSize); Index RightIndex = new Index(NodeSize); List <Leaf> RightLeaves = new List <Leaf>(); int half = CurrentIndex.Items.Count / 2; int newIndexItem = CurrentIndex.Items[half]; //Values to handle being the first root or //a non-inner index bool isFirstRootSplit = false; if (CurrentIndex.IndexList.Count == 0) { isFirstRootSplit = true; } bool needToGetLeaves = false; if (CurrentIndex.LeafList != null) { needToGetLeaves = true; } #endregion #region If split at Root if (MainStack.Count == 1) { //Root = CenterIndex #region Set Right LeafList //Set start to half if the Root doesn't //reference any indexes if (isFirstRootSplit) { for (int rightCount = half; rightCount < CurrentIndex.LeafList.Count; rightCount++) { RightLeaves.Add(CurrentIndex.LeafList[rightCount]); } } #endregion #region Set and Reference RightIndex's Indexes/Leaves for (int i = half + 1; i < CurrentIndex.Items.Count; i++) { RightIndex.Items.Add(CurrentIndex.Items[i]); } if (isFirstRootSplit) { for (int i = 0; i < RightIndex.Items.Count; i++) { RightIndex.LeafList.Add(RightLeaves[i]); } } else { for (int i = half; i < CurrentIndex.Items.Count; i++) { RightIndex.IndexList.Add(CurrentIndex.IndexList[i]); } } #endregion //Dispose values int disposeCount = CurrentIndex.Items.Count; for (int i = half; i < disposeCount; i++) { CurrentIndex.Items.RemoveAt(half); if (isFirstRootSplit) { CurrentIndex.LeafList.RemoveAt(half); } else { CurrentIndex.IndexList.RemoveAt(half); } } //Set Left Index LeftIndex = new Index(CurrentIndex); //Set Right Index Level LeftIndex.IndexLevel = CurrentIndex.IndexLevel; RightIndex.IndexLevel = CurrentIndex.IndexLevel; //Set and Reference CenterIndex CenterIndex.IndexList.Add(LeftIndex); CenterIndex.Insert(newIndexItem, RightIndex); CenterIndex.IndexLevel = 0; Root = new Index(CenterIndex); //Set Index Levels IncrementAllTreeLevels(Root); //Increment Count TreeIndexes += 2; NodeCount += 2; //Pop stack and move up tree MainStack.Pop(); } #endregion #region If split elsewhere else { #region Set Right LeafList if (needToGetLeaves) { //Enter Leaves into RightIndex for (int rightCount = half; rightCount < CurrentIndex.LeafList.Count; rightCount++) { RightLeaves.Add(CurrentIndex.LeafList[rightCount]); } } #endregion #region Set and Reference RightIndex's Indexes/Leaves for (int i = half + 1; i < CurrentIndex.Items.Count; i++) { RightIndex.Items.Add(CurrentIndex.Items[i]); } if (needToGetLeaves) { for (int i = 0; i < RightIndex.Items.Count; i++) { RightIndex.LeafList.Add(RightLeaves[i]); } } else { for (int i = half; i < CurrentIndex.Items.Count; i++) { RightIndex.IndexList.Add(CurrentIndex.IndexList[i]); } } #endregion //Set Right Index Level RightIndex.IndexLevel = CurrentIndex.IndexLevel; //Dispose values int disposeCount = CurrentIndex.Items.Count; for (int i = half; i < disposeCount; i++) { MainStack.Peek().Items.RemoveAt(half); if (needToGetLeaves) { MainStack.Peek().LeafList.RemoveAt(half); } else { MainStack.Peek().IndexList.RemoveAt(half); } } //Pop stack and move up tree MainStack.Pop(); //Add RightIndex to the Index up the tree //with references MainStack.Peek().Insert(newIndexItem, RightIndex); //Reset Root if needed if (MainStack.Count == 1) { Root = new Index(MainStack.Peek()); } //Increment Count TreeIndexes++; NodeCount++; //Recursively call SplitIndex until all //Indexes are split that need it SplitIndex(); } #endregion } }
private void OpSize(ParsedOpCode op) { var length = MainStack.Peek().Length; MainStack.Push(length); }
private void OpTuck() { var a = MainStack.Peek(); MainStack.Push(a, 2); }
private void OpPick() { var n = MainStack.PopInt32(); MainStack.Push(MainStack.Peek(n)); }
private void OpOver() { MainStack.Push(MainStack.Peek(1)); }
private void OpDup() { var value = MainStack.Peek(); MainStack.Push(value); }