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