Data Structure
-
Hi there I have a .txt file with the following dates (string \t\t int): aaaaa 5 aaaaa 6 aaabb 7 aaaaa 1 aaabb 5 ... My question is about what data structure to use in order to access as fast is possible those data(read only). I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#. I have to search a string and order the result desc after the int number. Example: search 'aaabb' result: 7, 5
-
Hi there I have a .txt file with the following dates (string \t\t int): aaaaa 5 aaaaa 6 aaabb 7 aaaaa 1 aaabb 5 ... My question is about what data structure to use in order to access as fast is possible those data(read only). I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#. I have to search a string and order the result desc after the int number. Example: search 'aaabb' result: 7, 5
-
Hi there I have a .txt file with the following dates (string \t\t int): aaaaa 5 aaaaa 6 aaabb 7 aaaaa 1 aaabb 5 ... My question is about what data structure to use in order to access as fast is possible those data(read only). I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#. I have to search a string and order the result desc after the int number. Example: search 'aaabb' result: 7, 5
You can use something like this:
string line = ""; List values = new List(); using(FileStream stream = File.OpenRead("textfile.txt")) using(StreamReader reader = new StreamReader(stream)) { while ((line = reader.ReadLine()) != null) { // adjust the search pattern here // Match match = Regex.Match(line, @"^\\w+\\s+(\\d+)$"); Match match = Regex.Match(line, @"^aaabb\\s+(\\d+)$"); if (match.Success) values.Add(Int32.Parse(match.Groups\[1\].Value)); } }
regards
-
Hi there I have a .txt file with the following dates (string \t\t int): aaaaa 5 aaaaa 6 aaabb 7 aaaaa 1 aaabb 5 ... My question is about what data structure to use in order to access as fast is possible those data(read only). I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#. I have to search a string and order the result desc after the int number. Example: search 'aaabb' result: 7, 5
Probably the quick/simple in-memory data structure would be a Dictionary< string, List< int>>. Dictionarys are a hashtable, so lookups are log-n. I'm assuming from your example that there is a small number of ints. As for parsing the file, I'd ignore the other suggestions to use regexs if the file is large. Its said that when a man is faced with a problem and decides to solve it with a regex, then he now has two problems ;) Just string.Split on the space - it'll be orders of magnitude faster than the regex.
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
Hi there I have a .txt file with the following dates (string \t\t int): aaaaa 5 aaaaa 6 aaabb 7 aaaaa 1 aaabb 5 ... My question is about what data structure to use in order to access as fast is possible those data(read only). I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#. I have to search a string and order the result desc after the int number. Example: search 'aaabb' result: 7, 5
duta wrote:
I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#.
A struct in C++ is a different thing from a struct in C#. In C++ a struct is a definition for either an object or a value type, but in C# a struct is always a value type. To efficiently search the data, you should use a data structure like
Dictionary<string, List<int>>
. Something like:// create dictionary
Dictionary<string, List<int>> items = new Dictionary<string, List<int>>();
// populate it with data from the file
foreach (string line in File.ReadAllLines(pathName)) {
string[] data = line.Split('\t');
List<int> list;
if (!items.TryGetValue(data[0], out list) {
list = new List<int>();
items.Add(data[0], list);
}
list.Add(int.Parse(data[2]));
}
// get the list for a specific string
List<int> aaabb = items["aaabb"];
// sort the list
aaabb.Sort();Despite everything, the person most likely to be fooling you next is yourself.
-
Hi there I have a .txt file with the following dates (string \t\t int): aaaaa 5 aaaaa 6 aaabb 7 aaaaa 1 aaabb 5 ... My question is about what data structure to use in order to access as fast is possible those data(read only). I'm very new to C# but I have some experience in C++. There a struct array was an alternative but I don't know how to do that in C#. I have to search a string and order the result desc after the int number. Example: search 'aaabb' result: 7, 5
Personally I would read the data into a datatable using CSVtoTable[^] and then use either linq or a dataview to manupulate the data. This is not the "fastest" method as the datatable carries a fair overhead.
Never underestimate the power of human stupidity RAH
-
Probably the quick/simple in-memory data structure would be a Dictionary< string, List< int>>. Dictionarys are a hashtable, so lookups are log-n. I'm assuming from your example that there is a small number of ints. As for parsing the file, I'd ignore the other suggestions to use regexs if the file is large. Its said that when a man is faced with a problem and decides to solve it with a regex, then he now has two problems ;) Just string.Split on the space - it'll be orders of magnitude faster than the regex.
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.Right answer, but hash-table lookups are O(1).
-
Right answer, but hash-table lookups are O(1).
Ooops. Good spot ;) Both insertion and retrieval should be close to O(1). However the default size of the Dictionary class is 3 - and increasing the capacity is O(n), which is done by increasing the capacity to the next prime number (not sure on the reasoning behind that). This shouldn't affect the use of the Dictionary for this task - we always pick suitable initialization sizes for our collections right guys? ;P
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.