C#,80个字节(38 + 42)
数据
编码器
- 输入项
Int32
l
一个数字
- 输入项
Int32
r
一个数字
- 输出量
Int64
两个整数融合在一起
解码器
- 输入项
Int32
v
值
- 输出
Int32[]
具有两个原始int的数组。
打高尔夫球
// Encoder
e=(l,r)=>{return(long)l<<32|(uint)r;};
// Decoder
d=v=>{return new[]{v>>32,v&0xFFFFFFFFL};};
不打高尔夫球
// Encoder
e = ( l, r ) => {
return (long) l << 32 | (uint) r;
};
// Decoder
d = v => {
return new[] {
v >> 32,
v & 0xFFFFFFFFL };
};
非高尔夫可读
// Encoder
// Takes a pair of ints
e = ( l, r ) => {
// Returns the ints fused together in a long where the first 32 bits are the first int
// and the last 32 bits the second int
return (long) l << 32 | (uint) r;
};
// Decoder
// Takes a long
d = v => {
// Returns an array with the ints decoded where...
return new[] {
// ... the first 32 bits are the first int...
v >> 32,
// ... and the last 32 bits the second int
v & 0xFFFFFFFFL };
};
完整代码
using System;
using System.Collections.Generic;
namespace TestBench {
public class Program {
// Methods
static void Main( string[] args ) {
Func<Int32, Int32, Int64> e = ( l, r ) => {
return(long) l << 32 | (uint) r;
};
Func<Int64, Int64[]> d = v => {
return new[] { v >> 32, v & 0xFFFFFFFFL };
};
List<KeyValuePair<Int32, Int32>>
testCases = new List<KeyValuePair<Int32, Int32>>() {
new KeyValuePair<Int32, Int32>( 13, 897 ),
new KeyValuePair<Int32, Int32>( 54234, 0 ),
new KeyValuePair<Int32, Int32>( 0, 0 ),
new KeyValuePair<Int32, Int32>( 1, 1 ),
new KeyValuePair<Int32, Int32>( 615234, 1223343 ),
};
foreach( KeyValuePair<Int32, Int32> testCase in testCases ) {
Console.WriteLine( $" ENCODER: {testCase.Key}, {testCase.Value} = {e( testCase.Key, testCase.Value )}" );
Console.Write( $"DECODING: {e( testCase.Key, testCase.Value )} = " );
PrintArray( d( e( testCase.Key, testCase.Value ) ) );
Console.WriteLine();
}
Console.ReadLine();
}
public static void PrintArray<TSource>( TSource[] array ) {
PrintArray( array, o => o.ToString() );
}
public static void PrintArray<TSource>( TSource[] array, Func<TSource, String> valueFetcher ) {
List<String>
output = new List<String>();
for( Int32 index = 0; index < array.Length; index++ ) {
output.Add( valueFetcher( array[ index ] ) );
}
Console.WriteLine( $"[ {String.Join( ", ", output )} ]" );
}
}
}
发布
笔记