src from flutter

This commit is contained in:
Морозов Андрей 2021-11-10 12:04:06 +00:00
parent 77c2f02a96
commit 4b3542edad
1 changed files with 193 additions and 52 deletions

View File

@ -1,15 +1,15 @@
import 'dart:io'; import 'dart:io';
class Separators { class Separators {
final String dotToConnections = ": "; static const String dotToConnections = ": ";
final String dotToLength = "|"; static const String dotToLength = "|";
final String space = " "; static const String space = " ";
final String hasLength = "Взвешенный\n"; static const String hasLength = "Взвешенный";
final String hasNoLength = "НеВзвешенный\n"; static const String hasNoLength = "НеВзвешенный";
final String isOriented = "Ориентированный\n"; static const String isOriented = "Ориентированный";
final String isNotOriented = "НеОриентированный\n"; static const String isNotOriented = "НеОриентированный";
final String nL = "\n"; static const String nL = "\n";
final String end = "END"; static const String end = "END";
Separators(); Separators();
} }
@ -25,6 +25,7 @@ class Dot {
String getName() => _name; String getName() => _name;
bool hasConnection(int n) => _ln.containsKey(n); bool hasConnection(int n) => _ln.containsKey(n);
Map<int, int> getL() => _ln; Map<int, int> getL() => _ln;
int getLength(int x) { int getLength(int x) {
if (hasConnection(x)) { if (hasConnection(x)) {
return _ln[x]!; return _ln[x]!;
@ -53,9 +54,9 @@ class Dot {
} }
//******Constructor****** //******Constructor******
Dot() { Dot([String name = "Undefined", int n = -1]) {
_name = "Undefined"; _name = name;
num = -1; num = n;
_ln = <int, int>{}; _ln = <int, int>{};
} }
Dot.fromTwoLists(String name, List<int> num0, List<int> length, Dot.fromTwoLists(String name, List<int> num0, List<int> length,
@ -97,11 +98,9 @@ class Graphs {
bool _oriented = false; //Ориентированность bool _oriented = false; //Ориентированность
//*********************Add************************ //*********************Add************************
bool addDot(Dot a) { String? addDot(Dot a) {
if (getNumByName(a.getName()) != null) { if (getNumByName(a.getName()) != null) {
print( return ("Dot name \"${a.getName()}\" already in use. Change name or use addPath");
"Dot name ${a.getName()} already in use. Change name or use addPath");
return false;
} }
_amount++; _amount++;
a.num = _amount; a.num = _amount;
@ -109,7 +108,7 @@ class Graphs {
_syncNameTable(); _syncNameTable();
checkDots(false); checkDots(false);
if (!_oriented) _fullFix(); if (!_oriented) _fullFix();
return true; return null;
} }
bool addDotFromToLists(String name, List<int> num0, List<int> length, bool addDotFromToLists(String name, List<int> num0, List<int> length,
@ -129,37 +128,40 @@ class Graphs {
return true; return true;
} }
bool addIsolated(String name) { String? addIsolated(String name) {
var o = addDot(Dot.fromTwoLists(name, [], [])); var res = addDot(Dot.fromTwoLists(name, [], []));
_syncNameTable(); _syncNameTable();
return o; return res;
} }
bool addPath(int from, int to, [int len = 0]) { String? addPath(int from, int to, [int len = 0]) {
if (from <= 0 || from > _amount || to <= 0 && to > _amount) { if (from <= 0 || from > _amount || to <= 0 && to > _amount) {
return false; return "Index out of range. Have dots 1..$_amount";
} }
_dots[from - 1].addPath(to, len); _dots[from - 1].addPath(to, len);
if (!_oriented) { if (!_oriented) {
_dots[to - 1].addPath(from, len); _dots[to - 1].addPath(from, len);
} }
return true; return null;
} }
//*********************Add************************ //*********************Add************************
//*********Delete********* //*********Delete*********
bool delPath(int from, int to) { String? delPath(int from, int to) {
if (from <= 0 || from > _amount || to <= 0 && to > _amount) { if (from <= 0 || from > _amount || to <= 0 && to > _amount) {
return false; return "Can't find specified path";
} }
_dots[from - 1].delPath(to); _dots[from - 1].delPath(to);
if (!_oriented) { if (!_oriented) {
_dots[to - 1].delPath(from); _dots[to - 1].delPath(from);
} }
return true; return null;
} }
bool delDot(int inn) { String? delDot(int inn) {
if (inn > _amount || inn < 1) {
return "Index out of range. Allowed 1..$_amount";
}
List<int> toDel = <int>[]; List<int> toDel = <int>[];
for (int i in _dots[inn - 1].getL().keys) { for (int i in _dots[inn - 1].getL().keys) {
toDel.add(i); toDel.add(i);
@ -171,7 +173,13 @@ class Graphs {
_syncNum(); _syncNum();
_syncNameTable(); _syncNameTable();
_fixAfterDel(inn); _fixAfterDel(inn);
return true; return null;
}
void flushData() {
_dots = <Dot>[];
_amount = 0;
_nameTable = <int, String>{};
} }
//*********Delete********* //*********Delete*********
@ -241,12 +249,121 @@ class Graphs {
} }
//******Helper******* //******Helper*******
//*****Setters*******
void setName(String name) => _name = name;
String? flipUseOrientation() {
if (_amount != 0) {
return "Can change use of orientation only in empty graph";
}
_oriented = !_oriented;
return null;
}
String? flipUseLength() {
if (_amount != 0) {
return "Can change use of length only in empty graph";
}
_useLength = !_useLength;
return null;
}
String? replaceDataFromFile(String path) {
File file = File(path);
List<String> lines = file.readAsLinesSync();
if (lines.length < 3) {
return "Not enough lines in file";
}
String name = lines.removeAt(0);
bool oriented;
switch (lines.removeAt(0)) {
case Separators.isOriented:
oriented = true;
break;
case Separators.isNotOriented:
oriented = false;
break;
default:
return "Error on parsing \"IsOriented\"";
}
bool useLength;
switch (lines.removeAt(0).trim()) {
case Separators.hasLength:
useLength = true;
break;
case Separators.hasNoLength:
useLength = false;
break;
default:
return "Error on parsing \"HasLength\"";
}
List<Dot> dots = <Dot>[];
for (var l in lines) {
l = l.trimRight();
if (l != Separators.end) {
var spl = l.split(Separators.space);
List<int> dot = <int>[];
List<int> len = <int>[];
String name = spl.removeAt(0);
name = name.substring(0, name.length - 1);
for (var splitted in spl) {
if (splitted != "") {
var dt = splitted.split(Separators.dotToLength);
if (dt.length == 2) {
int? parsed = int.tryParse(dt[0]);
if (parsed == null) {
return "Error while parsing file\nin parsing int in \"${dt[0]}\"";
}
dot.add(parsed);
if (useLength) {
parsed = int.tryParse(dt[1]);
if (parsed == null) {
return "Error while parsing file\nin parsing int in \"${dt[1]}\"";
}
len.add(parsed);
} else {
len.add(0);
}
} else if (dt.length == 1) {
int? parsed = int.tryParse(splitted);
if (parsed == null) {
return "Error while parsing file\nin parsing int in \"$splitted\"";
}
dot.add(parsed);
len.add(0);
}
}
}
dots.add(Dot.fromTwoLists(name, dot, len));
}
}
_name = name;
_oriented = oriented;
_useLength = useLength;
_dots = dots;
_syncNum();
_syncNameTable();
if (!_oriented) _fullFix();
return null;
}
//*****Setters*******
//*****Getters******* //*****Getters*******
bool getDoubleSided() => _oriented; bool getDoubleSidedBool() => _oriented;
bool getUseLength() => _useLength; String getDoubleSidedStr() {
if (_oriented) return Separators.isOriented;
return Separators.isNotOriented;
}
bool getUseLengthBool() => _useLength;
String getUseLengthStr() {
if (_useLength) return Separators.hasLength;
return Separators.hasNoLength;
}
List<Dot> getDots() => _dots; List<Dot> getDots() => _dots;
String getName() => _name; String getName() => _name;
String? getNameByNum(int n) => _nameTable[n]; String? getNameByNum(int n) => _nameTable[n];
Map<int, String> getNameTable() => _nameTable;
int getDotAmount() => _dots.length; int getDotAmount() => _dots.length;
int? getNumByName(String n) { int? getNumByName(String n) {
for (var i in _nameTable.keys) { for (var i in _nameTable.keys) {
@ -282,6 +399,23 @@ class Graphs {
} }
return out; return out;
} }
/*List<Dot> getNoRepeatDots() {
List<Dot> ret = <Dot>[];
for (int i = 0; i < _amount; i++) {
ret.add(Dot(_dots[i].getName(), _dots[i].num));
}
for (int i = 0; i < _amount; i++) {
for (int j in _dots[i].getL().keys) {
if (!ret[j - 1].hasConnection(i + 1) && !ret[i].hasConnection(j) ||
i == j) {
var len = _dots[i].getLength(j);
ret[i].addPath(j, len);
}
}
}
return ret;
}*/
//*****Getters******* //*****Getters*******
//******Print****** //******Print******
@ -303,31 +437,34 @@ class Graphs {
} }
void printToFile(String name) { void printToFile(String name) {
Separators sep = Separators();
var file = File(name); var file = File(name);
file.writeAsStringSync("$_name\n"); file.writeAsStringSync("$_name\n");
if (_oriented) { if (_oriented) {
file.writeAsStringSync(sep.isOriented, mode: FileMode.append); file.writeAsStringSync("${Separators.isOriented}\n",
mode: FileMode.append);
} else { } else {
file.writeAsStringSync(sep.isNotOriented, mode: FileMode.append); file.writeAsStringSync("${Separators.isNotOriented}\n",
mode: FileMode.append);
} }
if (_useLength) { if (_useLength) {
file.writeAsStringSync(sep.hasLength, mode: FileMode.append); file.writeAsStringSync("${Separators.hasLength}\n",
mode: FileMode.append);
} else { } else {
file.writeAsStringSync(sep.hasNoLength, mode: FileMode.append); file.writeAsStringSync("${Separators.hasNoLength}\n",
mode: FileMode.append);
} }
for (int i = 0; i < _amount; i++) { for (int i = 0; i < _amount; i++) {
file.writeAsStringSync((i + 1).toString() + sep.dotToConnections, file.writeAsStringSync((i + 1).toString() + Separators.dotToConnections,
mode: FileMode.append); mode: FileMode.append);
var d = _dots[i].getL(); var d = _dots[i].getL();
for (var j in d.keys) { for (var j in d.keys) {
file.writeAsStringSync( file.writeAsStringSync(
j.toString() + sep.dotToLength + d[j].toString() + " ", j.toString() + Separators.dotToLength + d[j].toString() + " ",
mode: FileMode.append); mode: FileMode.append);
} }
file.writeAsStringSync(sep.nL, mode: FileMode.append); file.writeAsStringSync(Separators.nL, mode: FileMode.append);
} }
file.writeAsStringSync(sep.end, mode: FileMode.append); file.writeAsStringSync(Separators.end, mode: FileMode.append);
} }
//******Print****** //******Print******
@ -353,22 +490,22 @@ class Graphs {
if (!_oriented) _fullFix(); if (!_oriented) _fullFix();
} }
Graphs.fromFile(String path) { Graphs.fromFile(String path) {
Separators sep = Separators(); replaceDataFromFile(path);
File file = File(path); /*File file = File(path);
List<String> lines = file.readAsLinesSync(); List<String> lines = file.readAsLinesSync();
_name = lines.removeAt(0); _name = lines.removeAt(0);
_oriented = lines.removeAt(0) == sep.isOriented.trim(); _oriented = lines.removeAt(0) == Separators.isOriented.trim();
_useLength = lines.removeAt(0) == sep.hasLength.trim(); _useLength = lines.removeAt(0) == Separators.hasLength.trim();
_dots = <Dot>[]; _dots = <Dot>[];
for (var l in lines) { for (var l in lines) {
if (l != sep.end) { if (l != Separators.end) {
var spl = l.split(sep.space); var spl = l.split(Separators.space);
List<int> dot = <int>[]; List<int> dot = <int>[];
List<int> len = <int>[]; List<int> len = <int>[];
String name = spl.removeAt(0); String name = spl.removeAt(0);
name = name.substring(0, name.length - 1); name = name.substring(0, name.length - 1);
for (var splitted in spl) { for (var splitted in spl) {
var dt = splitted.split(sep.dotToLength); var dt = splitted.split(Separators.dotToLength);
if (dt.length == 2) { if (dt.length == 2) {
dot.add(int.parse(dt[0])); dot.add(int.parse(dt[0]));
if (_useLength) { if (_useLength) {
@ -386,7 +523,7 @@ class Graphs {
} }
_syncNum(); _syncNum();
_syncNameTable(); _syncNameTable();
if (!_oriented) _fullFix(); if (!_oriented) _fullFix();*/
} }
//*******Constructor******** //*******Constructor********
@ -394,14 +531,14 @@ class Graphs {
Graphs.clone(Graphs a) { Graphs.clone(Graphs a) {
_name = a.getName(); _name = a.getName();
_dots = a.getDots(); _dots = a.getDots();
_oriented = a.getDoubleSided(); _oriented = a.getDoubleSidedBool();
_useLength = a.getUseLength(); _useLength = a.getUseLengthBool();
_amount = _dots.length; _amount = _dots.length;
_syncNameTable(); _syncNameTable();
} }
//************Алгоритмы************ //************Алгоритмы************
bool bfsHasPath(int startDot, int goalDot) { /* bool bfsHasPath(int startDot, int goalDot) {
// обход в ширину // обход в ширину
startDot--; startDot--;
goalDot--; goalDot--;
@ -428,9 +565,10 @@ class Graphs {
} }
} }
return false; // Целевой узел недостижим return false; // Целевой узел недостижим
} }*/
List<int>? bfsPath(int startDot, int goalDot) { List<int>? bfsPath(int startDot, int goalDot) {
if (startDot == goalDot) return [startDot];
//if (!bfsHasPath(startDot, goalDot)) return null; //if (!bfsHasPath(startDot, goalDot)) return null;
startDot--; startDot--;
goalDot--; goalDot--;
@ -470,7 +608,6 @@ class Graphs {
//Восстановим кратчайший путь //Восстановим кратчайший путь
//Для восстановления пути пройдём его в обратном порядке, и развернём. //Для восстановления пути пройдём его в обратном порядке, и развернём.
List<int> path = <int>[]; List<int> path = <int>[];
int cur = goalDot; //текущая вершина пути int cur = goalDot; //текущая вершина пути
@ -493,21 +630,25 @@ class Graphs {
List<bool>? dfsIterative(int v) { List<bool>? dfsIterative(int v) {
v--; v--;
//List<int>? pos = <int>[];
List<bool> label = <bool>[]; List<bool> label = <bool>[];
for (int i = 0; i < _amount; i++) { for (int i = 0; i < _amount; i++) {
label.add(false); label.add(false);
} }
List<int> stack = <int>[]; List<int> stack = <int>[];
stack.add(v); stack.add(v);
//pos.add(v);
while (stack.isNotEmpty) { while (stack.isNotEmpty) {
v = stack.removeLast(); v = stack.removeLast();
if (!label[v]) { if (!label[v]) {
label[v] = true; label[v] = true;
for (int i in _dots[v].getL().keys) { for (int i in _dots[v].getL().keys) {
stack.add(i - 1); stack.add(i - 1);
//pos.add(i);
} }
} }
} }
//print(pos);
return label; return label;
} }