Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Data Structure

Data Structure

Scheduled Pinned Locked Moved C#
tutorialcsharpc++data-structuresquestion
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    duta
    wrote on last edited by
    #1

    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

    S L M G M 5 Replies Last reply
    0
    • D duta

      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

      S Offline
      S Offline
      sph3rex
      wrote on last edited by
      #2

      regexp \b[a-z]+\b\t\t([0-9]+) with option multiline and ignorecase

      Code? Yeah i love it fried together with a glass of wine.

      1 Reply Last reply
      0
      • D duta

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D duta

          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

          M Offline
          M Offline
          Mark Churchill
          wrote on last edited by
          #4

          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.

          A 1 Reply Last reply
          0
          • D duta

            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

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #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.

            1 Reply Last reply
            0
            • D duta

              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

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M Mark Churchill

                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.

                A Offline
                A Offline
                Alan Balkany
                wrote on last edited by
                #7

                Right answer, but hash-table lookups are O(1).

                M 1 Reply Last reply
                0
                • A Alan Balkany

                  Right answer, but hash-table lookups are O(1).

                  M Offline
                  M Offline
                  Mark Churchill
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups