-
Notifications
You must be signed in to change notification settings - Fork 400
Add serialization support for mutli-dimensional arrays #782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c3b90fe
Add quantity LinearPowerDensity
trb5016 44c9de2
Merge branch 'master' of https://github.com/angularsen/UnitsNet
trb5016 859dda4
Merge branch 'master' of https://github.com/angularsen/UnitsNet
a907894
Add combinations of Area, Density, and LinearDensity operators
c9d71d1
Changed operator overloads for Area, Density, and LinearDensity to SI…
a8fb5f6
Merge remote-tracking branch 'upstream/master'
0970155
Add serialization support for multi-dimensional arrays
f48deed
Addressed comments from PR 782
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
UnitsNet.Serialization.JsonNet/Internal/MultiDimensionalArrayHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace UnitsNet.Serialization.JsonNet.Internal | ||
{ | ||
|
||
/// <summary> | ||
/// Helper class for working with and manipulating multi-dimension arrays based on their generic index. | ||
/// </summary> | ||
internal static class MultiDimensionalArrayHelpers | ||
{ | ||
|
||
/// <summary> | ||
/// Returns a new array of same Rank and Length as <paramref name="array"/> but with each element converted to <typeparamref name="TResult"/> | ||
/// </summary> | ||
/// <typeparam name="TResult"></typeparam> | ||
/// <param name="array"></param> | ||
/// <returns></returns> | ||
public static Array ConvertArrayElements<TResult>(Array array) | ||
{ | ||
var ret = Array.CreateInstance(typeof(TResult), LastIndex(array)); | ||
var ind = FirstIndex(array); | ||
|
||
while (ind != null) | ||
{ | ||
ret.SetValue((TResult)array.GetValue(ind), ind); | ||
ind = NextIndex(array, ind); | ||
} | ||
return ret; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the index for the 'first' element in a multidimensional array. | ||
/// | ||
/// 'First' is defined as the <see cref="Array.GetLowerBound(int)"/> for each rank of the <paramref name="array"/> | ||
/// | ||
/// E.g., for a zero-based 5x5x5 array this method would return [0, 0, 0]. | ||
/// </summary> | ||
/// <param name="array"></param> | ||
/// <returns>1D integer array specifying the location of the first element in the multidimensional array</returns> | ||
public static int[] FirstIndex(Array array) | ||
{ | ||
return Enumerable.Range(0, array.Rank).Select(x => array.GetLowerBound(x)).ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the index for the 'last' element in a multidimensional array. | ||
/// | ||
/// 'Last' is defined as the <see cref="Array.GetUpperBound(int)"/> for each rank of the <paramref name="array"/> | ||
/// | ||
/// E.g., for a zero-based 5x5x5 array this method would return [4, 4, 4]. | ||
/// </summary> | ||
/// <param name="array"></param> | ||
/// <returns>1D integer array specifying the location of the last element in the multidimensional array</returns> | ||
public static int[] LastIndex(Array array) | ||
{ | ||
return Enumerable.Range(0, array.Rank).Select(x => array.GetUpperBound(x) + 1).ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the 'next' index after the specified multidimensional <paramref name="index"/> | ||
/// | ||
/// The 'next' index is determined by first looping through all elements in the first dimension of the array, then moving on to the next dimension and repeating | ||
/// </summary> | ||
/// <param name="array"></param> | ||
/// <param name="index"></param> | ||
/// <returns>Returns the index location of the next element in <paramref name="array"/> after <paramref name="index"/> as a 1D array of integers. If there is no next index, returns null</returns> | ||
public static int[] NextIndex(Array array, int[] index) | ||
{ | ||
for (var i = 0; i < index.Length; i++) | ||
{ | ||
index[i] += 1; | ||
|
||
if (index[i] <= array.GetUpperBound(i)) | ||
{ | ||
return index; | ||
} | ||
else | ||
{ | ||
index[i] = array.GetLowerBound(i); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.