private void AddNewValueRewritten(int key, SelfOrganizeIndexNode val) { var p = _tree; if (p.Key == null) { p.Key = key; p.Value = val; return; } while (true) { if (key < p.Key()) { if (p.LLink() != null) { p = p.LLink(); continue; } else { break; } } if (key > p.Key()) { if (p.RLink() != null) { p = p.RLink(); continue; } else { break; } } if (key == p.Key()) { //success, but we already have this key,so... return; } } var q = _tree.NewNode(key, val); if (key < p.Key()) { p.Left = q; } else { p.Right = q; } p = q; return; }
private void AddNewValueAndBalance(int key, SelfOrganizeIndexNode val) { return; }
void AddNewValue() { OpenFileDialog openDic = new OpenFileDialog(); openDic.Multiselect = false; openDic.Filter = string.Format("{0} (*.{1})|*.{1}", "Index файл", "index"); if (openDic.ShowDialog() == DialogResult.OK) { var basePath = System.IO.Path.ChangeExtension(openDic.FileName, null); var indexFile = basePath + ".index"; var baseFile = basePath + ".base"; var header = Helper.ReadFromBinaryFile <SelfOrganizeIndexHeader>(openDic.FileName, 0); if (header.version == 0) { Console.WriteLine("Incorrect file header"); MessageBox.Show("Incorrect file header"); return; } if (header.version != _version) { Console.WriteLine("Old version,fix it"); MessageBox.Show("Old version, fix it"); return; } Console.WriteLine("Be patient!Operation may take some time,depending on index file"); var lst = Helper.ReadFromBinaryFileList <SelfOrganizeIndexNode>(indexFile, header.nodesBeginOffset, 0, true); while (true) { Console.WriteLine("Enter value to add, -1 to exit"); var data = Console.ReadLine(); if (data.Contains("-1")) { break; } Console.WriteLine("Wait..."); var hash = Helper.GetHash(data, _hashType); var exist = lst.Where(x => x.hash == hash); if (exist.Any()) { Console.WriteLine("This value already exists"); } else { var newNode = new SelfOrganizeFileNode() { text = data }; var lastNodePos = Helper.WriteToBinaryFile <SelfOrganizeFileNode>(baseFile, newNode, FileMode.Append); var indexNode = new SelfOrganizeIndexNode() { hash = Helper.GetHash(data, _hashType), len = System.Runtime.InteropServices.Marshal.SizeOf(newNode), offset = lastNodePos }; lst.Add(indexNode); Helper.WriteToBinaryFile <SelfOrganizeIndexNode>(indexFile, indexNode, FileMode.Append); header.count += 1; Helper.WriteToBinaryFile <SelfOrganizeIndexHeader>(indexFile, header, FileMode.Open, 0); } } } }
void GenerateNewSelfOrganizingFile() { OpenFileDialog openDic = new OpenFileDialog(); openDic.Multiselect = false; var items = new List <SelfOrganizeIndexNode>(); if (openDic.ShowDialog() == DialogResult.OK) { var fileName = GenerateNameForNewFile(); var lines = System.IO.File.ReadAllLines(openDic.FileName); if (lines.Count() > 0) { Console.WriteLine("Be patient!Operation may take some time,depending on input file"); var indexFile = fileName + ".index"; var baseFile = fileName + ".base"; //Write 1st text element var firstFileNode = new SelfOrganizeFileNode() { text = lines.ElementAt(0) }; var firstFileNodePos = Helper.WriteToBinaryFile <SelfOrganizeFileNode>(baseFile, firstFileNode, FileMode.CreateNew); var header = new SelfOrganizeIndexHeader() { version = _version, hashType = _hashType, count = lines.Count(), nodesBeginOffset = 0, }; //Write header var firstIndexPos = Helper.WriteToBinaryFile <SelfOrganizeIndexHeader>(indexFile, header, FileMode.CreateNew, -1, false); //Update header with correct information header.nodesBeginOffset = firstIndexPos; Helper.WriteToBinaryFile <SelfOrganizeIndexHeader>(indexFile, header, FileMode.Open, 0); var firstIndexNode = new SelfOrganizeIndexNode() { hash = Helper.GetHash(firstFileNode.text, _hashType), len = Marshal.SizeOf(firstFileNode), offset = firstFileNodePos }; //Add 1st index Helper.WriteToBinaryFile <SelfOrganizeIndexNode>(indexFile, firstIndexNode, FileMode.Append); var res = new List <SelfOrganizeIndexNode>(); for (int i = 1; i < lines.Count(); i++) { var item = lines[i]; var fileNode = new SelfOrganizeFileNode() { text = item }; var lastNodePos = Helper.WriteToBinaryFile <SelfOrganizeFileNode>(baseFile, fileNode, FileMode.Append); var indexNode = new SelfOrganizeIndexNode() { hash = Helper.GetHash(item, _hashType), len = System.Runtime.InteropServices.Marshal.SizeOf(fileNode), offset = lastNodePos }; res.Add(indexNode); } Helper.WriteToBinaryFileList <SelfOrganizeIndexNode>(indexFile, res, FileMode.Append); res.Clear(); } } }
private void AddNewValueAndBalance(int key, SelfOrganizeIndexNode val) { //Work with 1st insert var init = _tree; if (init.Key == null) { init.Key = key; init.Value = val; init.Balance = 0; _head.Left.Key = _head.Left.Key + 1;//!! return; } var a = 0xFF;//Why not then? A1: var T = _head; var P = _head.RLink(); var S = P; A2: if (key < P.Key) { goto A3; } if (key > P.Key) { goto A4; } if (key == P.Key) { MessageBox.Show("Ключ " + key + " уже присутствует в дереве"); return; } A3: var Q = P.LLink(); if (Q == null) { Q = _tree.NewNode(null, default(SelfOrganizeIndexNode)); P.Left = Q; goto A5; } else { if (Q.Balance != 0) { T = P; S = Q; } P = Q; goto A2; } A4: Q = P.RLink(); if (Q == null) { Q = _tree.NewNode(null, default(SelfOrganizeIndexNode)); P.Right = Q; goto A5; } else { if (Q.Balance != 0) { T = P; S = Q; } P = Q; goto A2; } A5: Q.Key = key; Q.Value = val;//custom Q.Left = Q.Right = null; Q.Balance = 0; A6: if (key < S.Key) { a = -1; } else { a = 1; } var R = P = S.GetByA(a); while (P.Key != Q.Key) { if (key < P.Key) { P.Balance = -1; P = P.LLink(); } else if (key > P.Key) { P.Balance = 1; P = P.RLink(); } else //(key == P.Key) { P = Q; break; } } A7: //i if (S.Balance == 0) { S.Balance = a; _head.Left.Key = _head.Left.Key + 1;//!! return; } //ii if (S.Balance == -a) { S.Balance = 0; return; } //iii if (S.Balance == a) { if (R.Balance == a) { goto A8; } if (R.Balance == -a) { goto A9; } throw new Exception("?!?!?!"); } return; A8: P = R; S.SetByA(a, R.GetByA(-a)); R.SetByA(-a, S); S.Balance = R.Balance = 0; goto A10; A9: P = R.GetByA(-a); R.SetByA(-a, P.GetByA(a)); P.SetByA(a, R); S.SetByA(a, P.GetByA(-a)); P.SetByA(-a, S); if (P.Balance == a) { S.Balance = -a; R.Balance = 0; } else if (P.Balance == 0) { S.Balance = 0; R.Balance = 0; } else if (P.Balance == -a) { S.Balance = 0; R.Balance = a; } else { throw new Exception("?!?!?!?!?!?!?"); } P.Balance = 0; A10: if (S == T.RLink()) // S.Key .. { T.Right = P; } else { T.Left = P; } }
private void AddNewValue(int key, SelfOrganizeIndexNode val) { //T1 var p = _tree; //T1.5 if (p.Key == null) { p.Key = key; p.Value = val; return; } //T2 T2: if (key < p.Key()) { goto T3; } if (key > p.Key()) { goto T4; } if (key == p.Key()) { //success return; } //T3 T3: if (p.LLink() != null) { p = p.LLink(); goto T2; } else { goto T5; } //T4 T4: if (p.RLink() != null) { p = p.RLink(); goto T2; } else { goto T5; } //T5 T5: //Set key in constructor var q = _tree.NewNode(key, val); //Will be set automatically //LLink(q) = null; //RLink(q) = null; if (key < p.Key()) { p.Left = q; } else { p.Right = q; } p = q; return; }