Posts
Brahma Command Line Parser
/* Simple commandline argument parser written by Ananth B. http://www.ananthonline.net */ static class CommandLine { public class Switch // Class that encapsulates switch data. { public Switch(string name, Action<IEnumerable<string>> handler, string shortForm) { Name = name; Handler = handler; ShortForm = shortForm; }
public Switch(string name, Action<IEnumerable<string>> handler) { Name = name; Handler = handler; ShortForm = null; } public string Name { get; private set; } public string ShortForm { get; private set; } public Action<IEnumerable<string>> Handler { get; private set; } public int InvokeHandler(string[] values) { Handler(values); return 1; } } /* The regex that extracts names and comma-separated values for switches in the form (<switch>[="value 1",value2,.
read morePosts
Linq to GPU (Brahma)
// Create a data-parallel array and fill it with data var data = new DataParallelArray<float>(computationProvider, new[] { 0f, 1f, 2f, 3f, 4f, 5f, 6f }); // Compile the query CompiledQuery query = computationProvider.Compile<DataParallelArray<float>> ( d => from value in d select value * 2f ); // Run the query on this data IQueryable result = computationProvider.Run(query, data); // Print out the results foreach (float value in result) Console.WriteLine(value); // Get rid of all the stuff we created computationProvider.
read morePosts
.NET 4.0 New Feature - Memory Mapped File
.NET Framework 在 4.0 新增了記憶體對應檔案(Memory Mapped File)功能,將以前需透過 API 才能使用的功能包在 .NET Framework 的 System.IO.MemoryMappedFiles 命名空間中,可用以編輯大小極大的檔案、減少IO的存取次數、提高檔案處理的效能、與在多個處理序中共享其內容。
要使用記憶體對應檔案物件,首先要先建立記憶體對應檔案物件,須了解到記憶體對應檔案有兩種類型;
保存的記憶體對應檔案 非保存的記憶體對應檔案 保存的記憶體對應檔案顧名思義其記憶體對應檔案的內容會與磁碟中的檔案做對應,可將檔案內容放至記憶體對應檔案中用以處理極大的檔案,當處理完畢資料會自動回存回對應檔案。
非保存的記憶體對應檔案則沒有與磁碟中的檔案對應,其所存放的值只是記憶體中的資料,可用來作處理序間通訊(IPC)的共用記憶體,當處理完畢其記憶體對應檔案的資料會被捨棄,且會被GC回收。
使用上是透過 MemoryMappedFile 類別內含的 CreateNew、 CreateOrOpen、與 CreateFromFile 等方法來建立記憶體對應檔案物件。
Method Description CreateNew 建立非保存的記憶體對應檔案物件 CreateOrOpen 建立或開啟非保存的記憶體對應檔案物件 CreateFromFile 建立保存的記憶體對應檔案物件 若是有已建好的記憶體對應檔案,也可以透過 MemoryMappedFile 類別內含的 OpenExisting 方法將已經建立好的記憶體對應檔案物件開啟。
有了記憶體對應檔案物件後,剩下的就是透過記憶體對應檔案檢視去對其作存取的動作。一個記憶體對應檔案物件可產生多個記憶體對應檔案檢視,可設定要針對記憶體對應檔案物件整體或是部份來檢視,需特別注意到記憶體對應檔案檢視也分為兩種類型:
資料流存取檢視 隨機存取檢視 資料流存取檢視透過 MemoryMappedFile.CreateViewStream 建立,採循序存取的方式處理資料,適用於非保存的記憶體對應檔案。
隨機存取檢視透過 MemoryMappedFile.CreateViewAccessor 建立,採隨機存取的方式處理資料,適用於保存的記憶體對應檔案。
這邊來看個非保存的記憶體對應檔案物件的使用範例:
Imports System.IO.MemoryMappedFiles Imports System.Collections.Specialized Imports System.Runtime.Serialization.Formatters.Binary Module Module1 Sub Main() Const capacity As Integer = 512 Const mmfKey As String = "Larry" Dim levelUpBlog As New Blog With {.
read morePosts
使用Extension Method計算漢字筆畫
public static int GetStrokesNumber(this char c) { String hex = BitConverter.ToString(Encoding.GetEncoding("Big5").GetBytes(new char[] { c })).Replace("-", string.Empty); for (int i = 0; i < _strokesNumberData.Length; ++i) { for (int j = 0; j < _strokesNumberData[i].Length; j += 2) { if (hex.CompareTo(_strokesNumberData[i][j]) >= 0 && hex.CompareTo(_strokesNumberData[i][j + 1]) <= 0) return i+1; } } return 0; } }</pre></div> Module CharExtension
Private _strokesNumberData As String()() = { _ New String() {"A440", "A441"}, _ New String() {"A442", "A453", "C940", "C944"}, _ New String() {"A454", "A47E", "C945", "C94C"}, _ New String() {"A4A1", "A4FD", "C94D", "C95C"}, _ New String() {"A4FE", "A5DF", "C95D", "C9AA"}, _ New String() {"A5E0", "A6E9", "C9AB", "C959"}, _ New String() {"A6EA", "A8C2", "CA5A", "CBB0"}, _ New String() {"A8C3", "AB44", "CBB1", "CDDC"}, _ New String() {"AB45", "ADBB", "CDDD", "D0C7", "F9DA", "F9DA"}, _ New String() {"ADBC", "B0AD", "D0C8", "D44A"}, _ New String() {"B0AE", "B3C2", "D44B", "D850"}, _ New String() {"B3C3", "B6C3", "D851", "DCB0", "F9DB", "F9DB"}, _ New String() {"B6C4", "B9AB", "DCB1", "E0EF", "F9D6", "F9D8"}, _ New String() {"B9AC", "BBF4", "E0F0", "E4E5"}, _ New String() {"BBF5", "BEA6", "E4E6", "E8F3", "F9DC", "F9DC"}, _ New String() {"BEA7", "C074", "E8F4", "ECB8", "F9D9", "F9D9"}, _ New String() {"C075", "C24E", "ECB9", "EFB6"}, _ New String() {"C24F", "C35E", "EFB7", "F1EA"}, _ New String() {"C35F", "C454", "F1EB", "F3FC"}, _ New String() {"C455", "C4D6", "F3FD", "F5BF"}, _ New String() {"C3D7", "C56A", "F5C0", "F6D5"}, _ New String() {"C56B", "C5C7", "F6D6", "F7CF"}, _ New String() {"C5C8", "C5C7", "F6D6", "F7CF"}, _ New String() {"C5F1", "C654", "F8A5", "F8ED"}, _ New String() {"C655", "C664", "F8E9", "F96A"}, _ New String() {"C665", "C66B", "F96B", "F9A1"}, _ New String() {"C66C", "C675", "F9A2", "F9B9"}, _ New String() {"C676", "C67A", "F9BA", "F9C5"}, _ New String() {"C67B", "C67E", "F9C6", "F9DC"}} <Extension()> _ Public Function GetStrokesNumber(ByVal c As Char) As Integer Dim hex As [String] = BitConverter.
read morePosts
.NET 4.0 New Feature - Generic Lazy class
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Lazy<Boolean> lazy = new Lazy<Boolean>(() => { Console.WriteLine(“Initializing…”); System.Threading.Thread.Sleep(10000); Console.WriteLine(“Initialized…”); return true; });
Console.WriteLine("IsValueCreated = {0}", lazy.IsValueCreated); Stopwatch sw = Stopwatch.StartNew(); Console.WriteLine("result = {0}", lazy.Value); //Console.WriteLine("result = {0}", lazy.ToString()); Console.WriteLine("Elapsed Time = {0} ms", sw.ElapsedMilliseconds); Console.WriteLine(new string('=', 50)); Console.WriteLine("IsValueCreated = {0}", lazy.IsValueCreated); sw.Restart(); Console.WriteLine("result = {0}", lazy.Value); //Console.WriteLine("result = {0}", lazy.ToString()); Console.WriteLine("Elapsed Time = {0} ms", sw.ElapsedMilliseconds); } } }
read morePosts
[VB.NET]用Extension Method移除控制項
Public Module ControlExtension
#Region “Public Method” <Extension()> _ Public Sub Remove(ByVal ctrl As Control) Dim parent As Control = ctrl.Parent If parent Is Nothing Then Return End If parent.Controls.Remove(ctrl) End Sub #End Region
End Module
read morePosts
.NET 4.0 New Feature - Complex
Sub Main() ShowDetail("Complex.Zero", Complex.Zero) ShowDetail("Complex.One", Complex.One) Dim c1 As New Complex(1, 2) Dim c2 As New Complex(3, 4) ShowDetail("C1", c1) ShowDetail("C2", c2) 'Complex +-*/ Console.WriteLine("{0} + {1} = {2}", c1, c2, c1 + c2) Console.WriteLine("{0} - {1} = {2}", c1, c2, c1 - c2) Console.WriteLine("{0} * {1} = {2}", c1, c2, c1 * c2) Console.WriteLine("{0} / {1} = {2}", c1, c2, c1 / c2) Console.WriteLine() Console.WriteLine("Complex.Add({0}, {1}) = {2}", c1, c2, Complex.
read more