public void removeFromHashTable(info record) { int hashAdreeLevel1 = this.hFunction1(record.field2); if (this.arrayForReport[hashAdreeLevel1].field2 == record.field2) { this.arrayForReport[hashAdreeLevel1].field1 = "deleted"; this.arrayForReport[hashAdreeLevel1].field2 = "deleted"; this.arrayForReport[hashAdreeLevel1].field3 = "deleted"; this.recorded--; this.recordedCoef = this.recorded / this.arrayForReportSize; } else { int hasAdreeLevel2 = this.hFunction2(record.field2); for (int i = 1; i < arrayForReportSize; i++) { int insertionAdress = (hashAdreeLevel1 + i * hasAdreeLevel2) % arrayForReportSize; if (this.arrayForReport[insertionAdress].field2 == record.field2) { this.arrayForReport[insertionAdress].field1 = "deleted"; this.arrayForReport[insertionAdress].field2 = "deleted"; this.arrayForReport[insertionAdress].field3 = "deleted"; this.recorded--; this.recordedCoef = this.recorded / this.arrayForReportSize; return; } else { continue; } } } }
private void button1_Click(object sender, EventArgs e) { if (MessageBox.Show("Добавление данных может повлечь нарушение целостности информации\n" + "Все равно добавить?", "Добавление данных", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { pMap.info record = new pMap.info(this.textBox2.Text, this.textBox1.Text, this.textBox3.Text); int dataValid = record.validator(this.textBox2.Text, this.textBox1.Text, this.textBox3.Text); if (dataValid != 0) { this.textBox4.Text = record.errorHandler(dataValid); return; } int result = mainWindow.payInfoMap.findInArray(record); if (result == -1) { string[] infoToGrid = new string[4]; infoToGrid[0] = mainWindow.payInfoMap.getEmptyHashAddress(this.textBox1.Text).ToString(); infoToGrid[1] = this.textBox1.Text; infoToGrid[2] = this.textBox2.Text; infoToGrid[3] = this.textBox3.Text; dgw.Rows.Add(infoToGrid); mainWindow.payInfoMap.pushBackArray(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text); mainWindow.payInfoMap.addInArrayForReport(record); this.textBox4.Text = "Запись успешно добавлена в справочник"; string msg = mainWindow.payInfoMap.checkForUpReHashing(); if (msg != null) { dgw.Rows.Clear(); for (int i = 0; i < mainWindow.payInfoMap.arraySize - 1; i++) { infoToGrid[0] = mainWindow.payInfoMap.getHashToRecord(mainWindow.payInfoMap.array[i].field2).ToString(); infoToGrid[1] = mainWindow.payInfoMap.array[i].field2; infoToGrid[2] = mainWindow.payInfoMap.array[i].field1; infoToGrid[3] = mainWindow.payInfoMap.array[i].field3; dgw.Rows.Add(infoToGrid); } MessageBox.Show("Уровень заполнения хеш-таблицы более 75% - произошло рехеширование\nРазмер увеличен", "Рехеширование", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { this.textBox4.Text = "Запись с такими данные уже находится в справочнике"; } } else { return; } }
/// <summary> /// Поиск по всем трём полям /// </summary> //public Tuple<int, info, int> findInHashTable(info record) //{ // int comparisonCounter = 1; // if (recorded == 0) // { // info returnRec = new info(); // return Tuple.Create(-1, returnRec, -1); // } // else // { // int hashAdreeLevel1 = hFunction1(record.field2); // if (this.arrayForReport[hashAdreeLevel1].field2 == record.field2) // { // comparisonCounter++; // return Tuple.Create(hashAdreeLevel1, this.arrayForReport[hashAdreeLevel1], comparisonCounter); // } // else // { // int hasAdreeLevel2 = this.hFunction2(record.field2); // for (int i = 1; i < arrayForReportSize; i++) // { // comparisonCounter++; // int insertionAdress = (hashAdreeLevel1 + i * hasAdreeLevel2) % arrayForReportSize; // if (this.arrayForReport[insertionAdress].field2 == record.field2) // { // return Tuple.Create(insertionAdress, this.arrayForReport[insertionAdress], comparisonCounter); // } // else // { if(this.arrayForReport[insertionAdress].field2 == "deleted") // { // continue; // } // else // { // return Tuple.Create(-2, this.arrayForReport[insertionAdress], -2); // } // } // } // } // } // return Tuple.Create(-2, new info(), comparisonCounter); //} /// <summary> /// Поиск по должности /// </summary> public Tuple <int, info, int, string> findInHashTable(string post) { int comparisonCounter = 1; if (recorded == 0) { info returnRec = new info(); return(Tuple.Create(-1, returnRec, -1, "Справочник не содержит записей")); } else { int hashAdreeLevel1 = hFunction1(post); if (this.arrayForReport[hashAdreeLevel1].field2 == post) { return(Tuple.Create(hashAdreeLevel1, this.arrayForReport[hashAdreeLevel1], comparisonCounter, "Запись найдена")); } else { int hasAdreeLevel2 = this.hFunction2(post); for (int i = 1; i < arrayForReportSize; i++) { comparisonCounter++; int insertionAdress = (hashAdreeLevel1 + i * hasAdreeLevel2) % arrayForReportSize; if (this.arrayForReport[insertionAdress].field2 == post) { return(Tuple.Create(insertionAdress, this.arrayForReport[insertionAdress], comparisonCounter, "Запись найдена")); } else { if (this.arrayForReport[insertionAdress].field2 == null) { return(Tuple.Create(-2, this.arrayForReport[insertionAdress], -2, "Запись не содержится в справочнике")); } else { continue; } } } } } return(Tuple.Create(-2, new info(), comparisonCounter, "")); }
public int findInArray(info record) { int i = 0; foreach (info rec in array) { if (i == arraySize - 1) { i = -1; break; } if (rec == record) { break; } i++; } return(i); }
public string pushBackArray(string field1, string field2, string field3) { info record = new info(field2, field1, field3); if (arraySize == 1) { array[arraySize - 1] = record; arraySize++; Array.Resize(ref array, arraySize); return("Запись успешно добавлена"); } else { array[arraySize - 1] = record; arraySize++; Array.Resize(ref array, arraySize); return("Запись успешно добавлена"); } }
// //Удаление private void button16_Click(object sender, EventArgs e) { if (listBonusInfo.Rows.Count == 1) { return; } int indexRow = listBonusInfo.SelectedCells[0].RowIndex; if (MessageBox.Show("Удаление может повлечь нарушение целостности информации\n" + "Продолжить удаление?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { DataGridViewRow selectedRow = listBonusInfo.Rows[indexRow]; payInfoMap.eraseFromArray(selectedRow.Cells[2].Value.ToString(), selectedRow.Cells[1].Value.ToString(), selectedRow.Cells[3].Value.ToString()); pMap.info record = new pMap.info(selectedRow.Cells[2].Value.ToString(), selectedRow.Cells[1].Value.ToString(), selectedRow.Cells[3].Value.ToString()); payInfoMap.removeFromHashTable(record); listBonusInfo.Rows.RemoveAt(indexRow); string msg = mainWindow.payInfoMap.checkForDownReHashing(); if (msg != null) { listBonusInfo.Rows.Clear(); string[] infoToGrid = new string[4]; for (int i = 0; i < mainWindow.payInfoMap.arraySize - 1; i++) { infoToGrid[0] = mainWindow.payInfoMap.getHashToRecord(mainWindow.payInfoMap.array[i].field2).ToString(); infoToGrid[1] = mainWindow.payInfoMap.array[i].field2; infoToGrid[2] = mainWindow.payInfoMap.array[i].field1; infoToGrid[3] = mainWindow.payInfoMap.array[i].field3; listBonusInfo.Rows.Add(infoToGrid); } MessageBox.Show("Уровень заполнения хеш-таблицы менее 25% - произошло рехеширование\nРазмер уменьшен", "Рехеширование", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { return; } }
public void addInArrayForReport(info record) { int hashAdreeLevel1 = hFunction1(record.field2); if (this.arrayForReport[hashAdreeLevel1].field2 == null || this.arrayForReport[hashAdreeLevel1].field2 == "deleted") { this.arrayForReport[hashAdreeLevel1] = record; this.recorded++; this.recordedCoef = recorded / arrayForReportSize; return; } else { int hasAdreeLevel2 = this.hFunction2(record.field2); for (int i = 1; i < arrayForReportSize; i++) { int insertionAdress = (hashAdreeLevel1 + i * hasAdreeLevel2) % arrayForReportSize; if (this.arrayForReport[insertionAdress].field2 == null || this.arrayForReport[insertionAdress].field2 == "deleted") { this.arrayForReport[insertionAdress] = record; this.recorded++; this.recordedCoef = recorded / arrayForReportSize; return; } else { continue; } } } }
public string eraseFromArray(string field2, string field1, string field3) { info record = new info(field2, field1, field3); if (arraySize == 0) { return("Удаление невозможно справочник пуст"); } else { int refRecordToRemove = this.findInArray(record); if (refRecordToRemove != -1) { this.swapRecords(ref this.array, refRecordToRemove); arraySize--; Array.Resize(ref this.array, arraySize); return("Запись успешно удалена"); } else { return("Удаление невозможно запись не содержится в справочнике"); } } }
//Загрузка спавочников private void initPersonnelInfo_Click(object sender, EventArgs e) { this.listBonusInfo.Rows.Clear(); this.listPayInfo.Rows.Clear(); this.listPersonnelInfo.Rows.Clear(); this.listEmployeeInfo.Rows.Clear(); if (bynaryTreeSourceData.arraySize != 1 && bynaryTreeSourceData.array[0].field1 != null) { bynaryTreeSourceData.root = null; bynaryTreeSourceData.arraySize = 1; Array.Resize(ref bynaryTreeSourceData.array, bynaryTreeSourceData.arraySize); bynaryTreeSourceData.array[0].field1 = null; bynaryTreeSourceData.array[0].field2 = null; bynaryTreeSourceData.array[0].field3 = null; payInfoMap.arraySize = 1; Array.Resize(ref payInfoMap.array, payInfoMap.arraySize); payInfoMap.array[0].field1 = null; payInfoMap.array[0].field2 = null; payInfoMap.array[0].field3 = null; Array.Resize(ref payInfoMap.arrayForReport, 0); payInfoMap.arrayForReportSize = 16; Array.Resize(ref payInfoMap.arrayForReport, payInfoMap.arrayForReportSize); personnelMap.arraySize = 1; Array.Resize(ref personnelMap.array, personnelMap.arraySize); personnelMap.array[0].field1 = null; personnelMap.array[0].field2 = null; personnelMap.array[0].field3 = null; Array.Resize(ref personnelMap.arrayRoot, 0); personnelMap.arrayRootSize = 16; Array.Resize(ref personnelMap.arrayRoot, personnelMap.arrayRootSize); for (int i = 0; i < personnelMap.arrayRootSize - 1; i++) { personnelMap.arrayRoot[i] = new LinkedList(); } employeeMap.arraySize = 1; Array.Resize(ref employeeMap.array, employeeMap.arraySize); employeeMap.array[0].field1 = null; employeeMap.array[0].field2 = null; employeeMap.array[0].field3 = null; Array.Resize(ref employeeMap.arrayRoot, 0); employeeMap.arrayRootSize = 16; Array.Resize(ref employeeMap.arrayRoot, employeeMap.arrayRootSize); for (int i = 0; i < employeeMap.arrayRootSize - 1; i++) { employeeMap.arrayRoot[i] = new LinkedList(); } } if (openFileDialog.ShowDialog() == DialogResult.OK) { objRW.openReader(openFileDialog.FileName); sourceFileName = openFileDialog.FileName; } else { return; } string tmpStringFromFile = objRW.reader.ReadLine(); string[] parsedStringForHashTable = new string[3]; while (tmpStringFromFile != null && tmpStringFromFile[0] == '1') { parsedStringForHashTable = objRW.readerPars(tmpStringFromFile, 1); parsedStringForHashTable[1] = parsedStringForHashTable[1].Remove(0, 2); parsedStringForHashTable[0] = payInfoMap.getEmptyHashAddress(parsedStringForHashTable[1]).ToString(); this.listBonusInfo.Rows.Add(parsedStringForHashTable); payInfoMap.pushBackArray(parsedStringForHashTable[1], parsedStringForHashTable[2], parsedStringForHashTable[3]); pMap.info record = new pMap.info(parsedStringForHashTable[2], parsedStringForHashTable[1], parsedStringForHashTable[3]); payInfoMap.addInArrayForReport(record); tmpStringFromFile = objRW.reader.ReadLine(); } string[] parsedString = new string[2]; while (tmpStringFromFile[0] == '2') { parsedString = objRW.readerPars(tmpStringFromFile); parsedString[0] = parsedString[0].Remove(0, 2); this.listPayInfo.Rows.Add(parsedString); bynaryTreeSourceData.pushBackArray(parsedString[0], parsedString[1], parsedString[2]); tmpStringFromFile = objRW.reader.ReadLine(); } bynaryTreeSourceData.initTreeFromePayArray(ref bynaryTreeSourceData.array); parsedString = null; while (tmpStringFromFile[0] == '3') { parsedStringForHashTable = objRW.readerPars(tmpStringFromFile, 1); parsedStringForHashTable[1] = parsedStringForHashTable[1].Remove(0, 2); parsedStringForHashTable[0] = personnelMap.getHash(parsedStringForHashTable[1]).ToString(); HashTable.info record = new HashTable.info(parsedStringForHashTable[0], parsedStringForHashTable[1], parsedStringForHashTable[2]); this.listPersonnelInfo.Rows.Add(parsedStringForHashTable); personnelMap.pushBackArray(parsedStringForHashTable[1], parsedStringForHashTable[2], parsedStringForHashTable[3]); tmpStringFromFile = objRW.reader.ReadLine(); } personnelMap.initHashTableFromArray(); while (tmpStringFromFile != null) { parsedStringForHashTable = objRW.readerPars(tmpStringFromFile, 1); parsedStringForHashTable[1] = parsedStringForHashTable[1].Remove(0, 2); parsedStringForHashTable[0] = employeeMap.getHash(parsedStringForHashTable[1]).ToString(); HashTable.info record = new HashTable.info(parsedStringForHashTable[0], parsedStringForHashTable[1], parsedStringForHashTable[2]); this.listEmployeeInfo.Rows.Add(parsedStringForHashTable); employeeMap.pushBackArray(parsedStringForHashTable[1], parsedStringForHashTable[2], parsedStringForHashTable[3]); tmpStringFromFile = objRW.reader.ReadLine(); } employeeMap.initHashTableFromArray(); objRW.closeReader(); }