From 414c74be84aecd3aff2f7abbb6cab88f50034d84 Mon Sep 17 00:00:00 2001 From: Simon Diesenreiter Date: Sun, 26 Oct 2025 19:07:37 +0100 Subject: [PATCH] feat: add two dimensional data walker, refs: NOISSUE --- TextParser/Data/DataConversionHelpers.cs | 30 ++---- TextParser/Data/DataManipulationHelpers.cs | 21 ++++- TextParser/Data/DataSetManipulatorBase.cs | 2 + .../Data/DefaultTwoDimensionalManipulator.cs | 16 ++++ TextParser/Data/TokenConverter.cs | 4 +- TextParser/Data/TwoDimensionalDataWalker.cs | 91 +++++++++++++++++++ .../Schema/BuildingBlocks/StringBlock.cs | 20 ++-- 7 files changed, 149 insertions(+), 35 deletions(-) create mode 100644 TextParser/Data/TwoDimensionalDataWalker.cs diff --git a/TextParser/Data/DataConversionHelpers.cs b/TextParser/Data/DataConversionHelpers.cs index 3edf187..5ad4c4e 100644 --- a/TextParser/Data/DataConversionHelpers.cs +++ b/TextParser/Data/DataConversionHelpers.cs @@ -5,44 +5,32 @@ using Parsing.Tokenization; public static class DataConversionHelpers { - public static List ConvertData(this List tokenList, Func converter) where TTokenType : IValueToken + public static List ConvertData(this List valueList, Func converter) { var newList = new List(); - foreach (var token in tokenList) + foreach (var value in valueList) { - var typedToken = token as IValueToken; - if (typedToken == null) - { - throw new Exception("Invalid Token type encountered during value conversion"); - } - - newList.Add(converter(typedToken.GetValue())); + newList.Add(converter(value)); } return newList; } - public static List ConvertData(this List tokenList, Func> converter) where TTokenType : IValueToken + public static List ConvertData(this List valueList, Func> converter) { var newList = new List(); - foreach (var token in tokenList) + foreach (var value in valueList) { - var typedToken = token as IValueToken; - if (typedToken == null) - { - throw new Exception("Invalid Token type encountered during value conversion"); - } - - newList.AddRange(converter(typedToken.GetValue())); + newList.AddRange(converter(value)); } return newList; } - public static List> ConvertData(this List> tokenListList, Func converter) where TTokenType : IValueToken + public static List> ConvertData(this List> valueListList, Func converter) { var newListList = new List>(); - foreach (var tokenList in tokenListList) + foreach (var valueList in valueListList) { - newListList.Add(tokenList.ConvertData(converter)); + newListList.Add(valueList.ConvertData(converter)); } return newListList; } diff --git a/TextParser/Data/DataManipulationHelpers.cs b/TextParser/Data/DataManipulationHelpers.cs index 93f6418..4254be2 100644 --- a/TextParser/Data/DataManipulationHelpers.cs +++ b/TextParser/Data/DataManipulationHelpers.cs @@ -21,7 +21,7 @@ public static class DataManipulationHelpers return reducer(data); } - public static List TransformData(this List data, Func transformer) + public static List TransformData(this IEnumerable data, Func transformer) { var newList = new List(); foreach (TType dataItem in data) @@ -31,7 +31,7 @@ public static class DataManipulationHelpers return newList; } - public static List TransformData(this List> data, Func, TNewType> transformer) + public static List TransformData(this IEnumerable> data, Func, TNewType> transformer) { var newList = new List(); foreach (List dataItemList in data) @@ -40,4 +40,21 @@ public static class DataManipulationHelpers } return newList; } + + public static List> IterateData(this List> data, Action iterator) + { + int y = data.Count; + foreach(var rowList in data) + { + y--; + int x = 0; + foreach(var item in rowList) + { + iterator(item, x, y); + x++; + } + } + + return data; + } } \ No newline at end of file diff --git a/TextParser/Data/DataSetManipulatorBase.cs b/TextParser/Data/DataSetManipulatorBase.cs index 2311537..8acdc41 100644 --- a/TextParser/Data/DataSetManipulatorBase.cs +++ b/TextParser/Data/DataSetManipulatorBase.cs @@ -98,6 +98,8 @@ public abstract class DataSetManipulatorBase> FindInSet(TDataType data); + public abstract List> GetAllItems(); + public List> FindAtPosition(IDataIndex currentPosition, List data) { return this.FindAtPosition(currentPosition, data, this.ValidDirections()); diff --git a/TextParser/Data/DefaultTwoDimensionalManipulator.cs b/TextParser/Data/DefaultTwoDimensionalManipulator.cs index 5feb3ea..015a132 100644 --- a/TextParser/Data/DefaultTwoDimensionalManipulator.cs +++ b/TextParser/Data/DefaultTwoDimensionalManipulator.cs @@ -61,6 +61,22 @@ public class DefaultTwoDimensionalManipulator : DataSetManipulatorBas } } + public override List> GetAllItems() + { + var results = new List>(); + + for (int y = 0; y < this.dataSet.Count; y++) + { + for (int x = 0; x < this.dataSet[this.dataSet.Count - y - 1].Count; x++) + { + var singleResult = new SearchResult(new DefaultPositionalDataIndex(x, y)); + results.Add(singleResult); + } + } + + return results; + } + public override List> FindInSet(TDataType data) { var results = new List>(); diff --git a/TextParser/Data/TokenConverter.cs b/TextParser/Data/TokenConverter.cs index ce89918..76e8da9 100644 --- a/TextParser/Data/TokenConverter.cs +++ b/TextParser/Data/TokenConverter.cs @@ -28,9 +28,9 @@ public class TokenConverter if (!token.GetType().IsAssignableTo(typeof(IValueToken))) { - Console.WriteLine(token.GetText()); + Console.WriteLine("Token: " + token.GetText()); Type t = token.GetType(); - throw new Exception("Provided token is not a ValueToken - type: " + t.ToString()); + throw new Exception("Provided token is not a ValueToken - type: " + t.ToString() + " assigned to " + typeof(IValueToken).ToString()); } IValueToken valueToken = token as IValueToken; diff --git a/TextParser/Data/TwoDimensionalDataWalker.cs b/TextParser/Data/TwoDimensionalDataWalker.cs new file mode 100644 index 0000000..f2e8581 --- /dev/null +++ b/TextParser/Data/TwoDimensionalDataWalker.cs @@ -0,0 +1,91 @@ +var manipulator = DefaultTwoDimensionalManipulator.Create(row); + +using System.Runtime.InteropServices; +using Parsing.Data; + +namespace Parsing.Data; + +public static class TwoDimensionalDataWalker +{ + public static TwoDimensionalDataWalker Create(List> dataSet) where TDataType : IEquatable + { + return new TwoDimensionalDataWalker(dataSet); + } +} + +public class TwoDimensionalDataWalker where TDataType : IEquatable +{ + private List> dataSet + + private Func startCondition; + private TDataType startValue; + private bool startValueSet = false; + private Func, TDataType, bool> walkCondition; + private Func, TDataType, bool> endCondition; + private Direction directions; + private DefaultTwoDimenstionalManipulator manipulator; + + public TwoDimensionalDataWalker(List> dataSet) + { + this.dataSet = dataSet; + this.manipulator = DefaultTwoDimenstionalManipulator.Create(dataSet); + } + + public TwoDimensionalDataWalker WithStartCondition(Func startCondition) + { + this.startCondition = startCondition; + return this; + } + + public TwoDimensionalDataWalker WithStartValue(TDataType startValue) + { + this.startValue = startValue; + this.startValueSet = true; + return this; + } + + public TwoDimensionalDataWalker WithStepCondition(Func, TDataType, bool> stepCondition) + { + this.stepCondition = stepCondition; + return this; + } + + public TwoDimensionalDataWalker WithEndCondition(Func, TDataType, bool> endCondition) + { + this.endCondition = endCondition; + return this; + } + + public TwoDimensionalDataWalker WithDirections(Direction directions) + { + this.directions = directions; + return this; + } + + private List>> WalkFromSingleStartPosition(SearchResult start) + { + + } + + public List>> Walk() + { + List> startingPoints; + + if(this.startValueSet) + { + startingPoints = manipulator.FindInSet(this.startValue); + } + else + { + startingPoints = manipulator.GetAllItems(); + } + + var allFoundPaths = new List>>(); + + foreach(var startingPoint in startingPoints) + { + allFoundPaths.AddRange(this.WalkFromSingleStartPosition(startingPoint)); + } + return allFoundPaths; + } +} \ No newline at end of file diff --git a/TextParser/Schema/BuildingBlocks/StringBlock.cs b/TextParser/Schema/BuildingBlocks/StringBlock.cs index 7688421..61517d7 100644 --- a/TextParser/Schema/BuildingBlocks/StringBlock.cs +++ b/TextParser/Schema/BuildingBlocks/StringBlock.cs @@ -28,17 +28,17 @@ class StringBlock : BuildingBlockBase public override bool CanParseWord(string word) { // Here we need to ensure we are not matching any non-string tokens, since string can match pretty much anything - LongBlock longBlock = new LongBlock(); - if (longBlock.CanParseWord(word)) - { - return false; - } + // LongBlock longBlock = new LongBlock(); + // if (longBlock.CanParseWord(word)) + // { + // return false; + // } - IntegerBlock intBlock = new IntegerBlock(); - if (intBlock.CanParseWord(word)) - { - return false; - } + // IntegerBlock intBlock = new IntegerBlock(); + // if (intBlock.CanParseWord(word)) + // { + // return false; + // } return true; }