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. Other Discussions
  3. Clever Code
  4. Translating a jagged array

Translating a jagged array

Scheduled Pinned Locked Moved Clever Code
data-structuresquestion
10 Posts 5 Posters 53 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.
  • L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #1

    The code below translates a jagged array. For a jagged array A, the result is a n*m jagged array, where the outer size is n=max(A[i].Length) and the inner size of each subarray is m=A.GetLength(0). Remark: Holes after null or incomplete subarrays will be filled with default(T). Usage:

    MyType[][] array = ...;
    MyType[][] translated = array.Translate();

    For examle: 1.

    array = new int[5][] {
    new int[] { 5, 6, 7 },
    new int [] { 1, 2, 3, 4 },
    new int [] { },
    new int [] { 0, 1 },
    null,
    };
    expected = new int[4][] {
    new int[] { 5, 1, 0, 0, 0 },
    new int[] { 6, 2, 0, 1, 0 },
    new int[] { 7, 3, 0, 0, 0 },
    new int[] { 0, 4, 0, 0, 0 },
    };

    array = new int[2][] {
    null,
    null,
    };
    expected = new int[1][] {
    new int[] { 0, 0 },
    };

    For nullable types, there would be null instead of 0.

    public static class JaggedArrayExtensions
    {
    /// <summary>
    /// Translates a given jagged array. Holes after null or incomplete subarrays will be fileld with default(T).
    /// </summary>
    /// <typeparam name="T">Type of elements in the array</typeparam>
    /// <param name="array">The array to translate</param>
    /// <returns>A n*m jagged array, where the outer size is n=max(A[i].Length) and
    /// the inner size of each subarray is m=A.GetLength(0).</returns>
    public static T[][] Translate(this T[][] array)
    {
    if (array == null)
    throw new ArgumentNullException();
    if (array.Length == 0)
    return new T[0][];
    int columnCount = array.Max(sub => sub == null ? 1 : sub.Length);
    int rowCount = array.Length;
    T[][] res = new T[columnCount][];
    for (int i = 0; i < columnCount; i++)
    {
    res[i] = new T[rowCount];
    for (int j = 0; j < rowCount; j++)
    {
    if (array[j] == null || i >= array[j].Length)
    res[i][j] = default(T);
    else
    res[i][j] = array[j][i];
    }
    }
    return res;
    }
    }

    Greetings - Jacek

    N L R 3 Replies Last reply
    0
    • L Lutoslaw

      The code below translates a jagged array. For a jagged array A, the result is a n*m jagged array, where the outer size is n=max(A[i].Length) and the inner size of each subarray is m=A.GetLength(0). Remark: Holes after null or incomplete subarrays will be filled with default(T). Usage:

      MyType[][] array = ...;
      MyType[][] translated = array.Translate();

      For examle: 1.

      array = new int[5][] {
      new int[] { 5, 6, 7 },
      new int [] { 1, 2, 3, 4 },
      new int [] { },
      new int [] { 0, 1 },
      null,
      };
      expected = new int[4][] {
      new int[] { 5, 1, 0, 0, 0 },
      new int[] { 6, 2, 0, 1, 0 },
      new int[] { 7, 3, 0, 0, 0 },
      new int[] { 0, 4, 0, 0, 0 },
      };

      array = new int[2][] {
      null,
      null,
      };
      expected = new int[1][] {
      new int[] { 0, 0 },
      };

      For nullable types, there would be null instead of 0.

      public static class JaggedArrayExtensions
      {
      /// <summary>
      /// Translates a given jagged array. Holes after null or incomplete subarrays will be fileld with default(T).
      /// </summary>
      /// <typeparam name="T">Type of elements in the array</typeparam>
      /// <param name="array">The array to translate</param>
      /// <returns>A n*m jagged array, where the outer size is n=max(A[i].Length) and
      /// the inner size of each subarray is m=A.GetLength(0).</returns>
      public static T[][] Translate(this T[][] array)
      {
      if (array == null)
      throw new ArgumentNullException();
      if (array.Length == 0)
      return new T[0][];
      int columnCount = array.Max(sub => sub == null ? 1 : sub.Length);
      int rowCount = array.Length;
      T[][] res = new T[columnCount][];
      for (int i = 0; i < columnCount; i++)
      {
      res[i] = new T[rowCount];
      for (int j = 0; j < rowCount; j++)
      {
      if (array[j] == null || i >= array[j].Length)
      res[i][j] = default(T);
      else
      res[i][j] = array[j][i];
      }
      }
      return res;
      }
      }

      Greetings - Jacek

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      Put this in as a Tip/Trick


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      L 1 Reply Last reply
      0
      • N Nagy Vilmos

        Put this in as a Tip/Trick


        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

        L Offline
        L Offline
        Lutoslaw
        wrote on last edited by
        #3

        Done. Hopefully I won't be univoted for cross-posting. However, a Tip/Trick is a different class of post, so I do have an excuse. :cool:

        Greetings - Jacek

        1 Reply Last reply
        0
        • L Lutoslaw

          The code below translates a jagged array. For a jagged array A, the result is a n*m jagged array, where the outer size is n=max(A[i].Length) and the inner size of each subarray is m=A.GetLength(0). Remark: Holes after null or incomplete subarrays will be filled with default(T). Usage:

          MyType[][] array = ...;
          MyType[][] translated = array.Translate();

          For examle: 1.

          array = new int[5][] {
          new int[] { 5, 6, 7 },
          new int [] { 1, 2, 3, 4 },
          new int [] { },
          new int [] { 0, 1 },
          null,
          };
          expected = new int[4][] {
          new int[] { 5, 1, 0, 0, 0 },
          new int[] { 6, 2, 0, 1, 0 },
          new int[] { 7, 3, 0, 0, 0 },
          new int[] { 0, 4, 0, 0, 0 },
          };

          array = new int[2][] {
          null,
          null,
          };
          expected = new int[1][] {
          new int[] { 0, 0 },
          };

          For nullable types, there would be null instead of 0.

          public static class JaggedArrayExtensions
          {
          /// <summary>
          /// Translates a given jagged array. Holes after null or incomplete subarrays will be fileld with default(T).
          /// </summary>
          /// <typeparam name="T">Type of elements in the array</typeparam>
          /// <param name="array">The array to translate</param>
          /// <returns>A n*m jagged array, where the outer size is n=max(A[i].Length) and
          /// the inner size of each subarray is m=A.GetLength(0).</returns>
          public static T[][] Translate(this T[][] array)
          {
          if (array == null)
          throw new ArgumentNullException();
          if (array.Length == 0)
          return new T[0][];
          int columnCount = array.Max(sub => sub == null ? 1 : sub.Length);
          int rowCount = array.Length;
          T[][] res = new T[columnCount][];
          for (int i = 0; i < columnCount; i++)
          {
          res[i] = new T[rowCount];
          for (int j = 0; j < rowCount; j++)
          {
          if (array[j] == null || i >= array[j].Length)
          res[i][j] = default(T);
          else
          res[i][j] = array[j][i];
          }
          }
          return res;
          }
          }

          Greetings - Jacek

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

          You don't need GetLength(0), because the array is not multidimensional but jagged, so the outer array is "just any old 1D array".

          L 1 Reply Last reply
          0
          • L Lost User

            You don't need GetLength(0), because the array is not multidimensional but jagged, so the outer array is "just any old 1D array".

            L Offline
            L Offline
            Lutoslaw
            wrote on last edited by
            #5

            :thumbsup: fixed.

            Greetings - Jacek

            L 1 Reply Last reply
            0
            • L Lutoslaw

              The code below translates a jagged array. For a jagged array A, the result is a n*m jagged array, where the outer size is n=max(A[i].Length) and the inner size of each subarray is m=A.GetLength(0). Remark: Holes after null or incomplete subarrays will be filled with default(T). Usage:

              MyType[][] array = ...;
              MyType[][] translated = array.Translate();

              For examle: 1.

              array = new int[5][] {
              new int[] { 5, 6, 7 },
              new int [] { 1, 2, 3, 4 },
              new int [] { },
              new int [] { 0, 1 },
              null,
              };
              expected = new int[4][] {
              new int[] { 5, 1, 0, 0, 0 },
              new int[] { 6, 2, 0, 1, 0 },
              new int[] { 7, 3, 0, 0, 0 },
              new int[] { 0, 4, 0, 0, 0 },
              };

              array = new int[2][] {
              null,
              null,
              };
              expected = new int[1][] {
              new int[] { 0, 0 },
              };

              For nullable types, there would be null instead of 0.

              public static class JaggedArrayExtensions
              {
              /// <summary>
              /// Translates a given jagged array. Holes after null or incomplete subarrays will be fileld with default(T).
              /// </summary>
              /// <typeparam name="T">Type of elements in the array</typeparam>
              /// <param name="array">The array to translate</param>
              /// <returns>A n*m jagged array, where the outer size is n=max(A[i].Length) and
              /// the inner size of each subarray is m=A.GetLength(0).</returns>
              public static T[][] Translate(this T[][] array)
              {
              if (array == null)
              throw new ArgumentNullException();
              if (array.Length == 0)
              return new T[0][];
              int columnCount = array.Max(sub => sub == null ? 1 : sub.Length);
              int rowCount = array.Length;
              T[][] res = new T[columnCount][];
              for (int i = 0; i < columnCount; i++)
              {
              res[i] = new T[rowCount];
              for (int j = 0; j < rowCount; j++)
              {
              if (array[j] == null || i >= array[j].Length)
              res[i][j] = default(T);
              else
              res[i][j] = array[j][i];
              }
              }
              return res;
              }
              }

              Greetings - Jacek

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #6

              How about this one?

              public static T[][] Translate2<T>(this T[][] array)
              {
              if (array == null)
              throw new ArgumentNullException();
              if (array.Length == 0)
              return new T[0][];

              return Enumerable.Range(0, array.Max(sub => sub == null ? 1 : sub.Length)).
                  Select(i => Enumerable.Range(0, array.Length).
                      Select(j => array\[j\] == null || i >= array\[j\].Length ? 
                          default(T) : array\[j\]\[i\]).ToArray()).ToArray();
              

              }

              Robert

              L 1 Reply Last reply
              0
              • R Robert Rohde

                How about this one?

                public static T[][] Translate2<T>(this T[][] array)
                {
                if (array == null)
                throw new ArgumentNullException();
                if (array.Length == 0)
                return new T[0][];

                return Enumerable.Range(0, array.Max(sub => sub == null ? 1 : sub.Length)).
                    Select(i => Enumerable.Range(0, array.Length).
                        Select(j => array\[j\] == null || i >= array\[j\].Length ? 
                            default(T) : array\[j\]\[i\]).ToArray()).ToArray();
                

                }

                Robert

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

                No offense, but I would file that under "LINQ where it doesn't help and only slows things down"

                R W 2 Replies Last reply
                0
                • L Lutoslaw

                  :thumbsup: fixed.

                  Greetings - Jacek

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

                  Also, you don't need to fill holes with default(T). They're already filled with that.

                  1 Reply Last reply
                  0
                  • L Lost User

                    No offense, but I would file that under "LINQ where it doesn't help and only slows things down"

                    R Offline
                    R Offline
                    Robert Rohde
                    wrote on last edited by
                    #9

                    I know but its so much fun... :-D

                    1 Reply Last reply
                    0
                    • L Lost User

                      No offense, but I would file that under "LINQ where it doesn't help and only slows things down"

                      W Offline
                      W Offline
                      william sharkey
                      wrote on last edited by
                      #10

                      Without data, who knows? If it were done .AsParallel it may be faster on certain system/jagged array combos. Then again, if you find yourself translating jagged arrays on large sets of data, it may be a sign of choosing the wrong data structure.

                      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