// this doesn't take into account auxiliary data such as: sync roots, vtables and so on public static int SizeOfUsefulData(this Object o) { o.AssertNotNull(); var t = o.GetType(); if (t.IsValueType) { // todo. will crash for structures that have a reference-type fields return Marshal.SizeOf(t); } else if (t.IsArray) { // todo. won't work for arrays that have differently sized elements var elcount = o.AssertCast<Array>().Dims().Product(); return elcount * t.GetElementType().SizeOfUsefulData(); } else if (t.IsClass) { // todo. to be implemented throw AssertionHelper.Fail(); } else { throw AssertionHelper.Fail(); } }
public static TypeRule Rule(this Func<Type, bool> t, out IDisposable unreg) { t.AssertNotNull(); var rule = new TypeRule(t); Repository.Rules.Add(rule); unreg = new DisposableAction(() => Repository.Rules.Remove(rule)); return rule; }
public static PropertyRule Rule(this Func<PropertyInfo, bool> pi, out IDisposable unreg) { pi.AssertNotNull(); var rule = new PropertyRule(pi); Repository.Rules.Add(rule); unreg = new DisposableAction(() => Repository.Rules.Remove(rule)); return rule; }
public static Version Version(this FileInfo fi) { fi.AssertNotNull(); if (!fi.Exists) throw new FileNotFoundException(null, fi.FullName); var fvi = FileVersionInfo.GetVersionInfo(fi.FullName); return new Version(fvi.FileMajorPart, fvi.FileMinorPart, fvi.FileBuildPart, fvi.FilePrivatePart); }
public static JittedKernel JitKernel(this String ptx, dim3 reqntid, HardwareIsa target) { ptx.AssertNotNull(); CudaDriver.Ensure(); var tuning = new JitTuning { Reqntid = reqntid }; return ptx.JitKernel(tuning, target); }
public static Object Invoke(this JittedKernel kernel, dim3 gridDim, dim3 blockDim, IEnumerable<KernelArgument> args) { kernel.AssertNotNull(); args = args ?? Seq.Empty<KernelArgument>().ToArray(); CudaDriver.Ensure(); return kernel.Function.Invoke(gridDim, blockDim, args); }
public static KernelResult Run(this JittedFunction function, dim3 gridDim, dim3 blockDim, params KernelArgument[] args) { function.AssertNotNull(); args = args ?? Seq.Empty<KernelArgument>().ToArray(); CudaDriver.Ensure(); return function.Run(gridDim, blockDim, (IEnumerable<KernelArgument>)args); }
public static KernelResult Run(this JittedKernel kernel, dim3 gridDim, dim3 blockDim, params KernelArgument[] args) { kernel.AssertNotNull(); args = args ?? Seq.Empty<KernelArgument>().ToArray(); CudaDriver.Ensure(); return kernel.Function.Run(gridDim, blockDim, args); }
public static KernelResult Run(this JittedFunction function, dim3 gridDim, dim3 blockDim, IEnumerable<KernelArgument> args) { function.AssertNotNull(); args = args ?? Seq.Empty<KernelArgument>().ToArray(); CudaDriver.Ensure(); var invocation = new KernelInvocation(function, args); return invocation.Launch(gridDim, blockDim); }
public static void ValidateName(this String name) { name.AssertNotNull(); var fmt1 = name.Match("^[a-zA-Z][a-zA-Z0-9_$]*$"); var fmt2 = name.Match("^[_$%][a-zA-Z0-9_$]*$"); (fmt1.Success || fmt2.Success).AssertTrue(); var sregs = Sregs.Sigs.Select(sig => sig.Name).ToHashSet(); sregs.Contains(name).AssertFalse(); }
public static JittedKernel JitKernel(this String ptx, JitTuning tuning, HardwareIsa target) { ptx.AssertNotNull(); CudaDriver.Ensure(); var compiler = new JitCompiler(); compiler.Target = target; compiler.Tuning = tuning; var result = compiler.Compile(ptx); return new JittedKernel(result); }
public static JittedKernel JitKernel(this String ptx, JitTuning tuning) { ptx.AssertNotNull(); CudaDriver.Ensure(); var compiler = new JitCompiler(); compiler.TargetFromContext = true; compiler.Tuning = tuning; var result = compiler.Compile(ptx); return new JittedKernel(result); }
/// <summary> /// Exposes the records in a <see cref="CsvReader"/> as an enumeration of <see cref="DataRecord"/>. /// </summary> /// <param name="this"> /// The data source. /// </param> /// <param name="readHeader"> /// If <see langword="true"/>, the first record in <paramref name="this"/> will be read in as the header record. /// </param> /// <returns> /// An enumerable of all records in <paramref name="this"/>. /// </returns> public static IEnumerable<DataRecord> ToEnumerable(this CsvReader @this, bool readHeader = false) { @this.AssertNotNull(nameof(@this)); if (readHeader && @this.HasMoreRecords) { @this.ReadHeaderRecord(); } while (@this.HasMoreRecords) { yield return @this.ReadDataRecord(); } }
public static void WhenActivated( this ISupportsActivation @this, Action<CompositeDisposable> disposables) { @this.AssertNotNull(nameof(@this)); @this .WhenActivated( () => { var d = new CompositeDisposable(); disposables(d); return new[] { d }; }); }
public static IDisposable WhenActivated( this IActivatable @this, Action<CompositeDisposable> disposables, IViewFor view = null) { @this.AssertNotNull(nameof(@this)); return @this .WhenActivated( () => { var d = new CompositeDisposable(); disposables(d); return new[] { d }; }, view); }
/// <summary> /// Asynchronously copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>. /// </summary> /// <param name="this"> /// The data source. /// </param> /// <param name="destination"> /// The data destination. /// </param> /// <returns> /// The number of records written to <paramref name="destination"/>. /// </returns> public async static Task<int> CopyToAsync(this CsvReader @this, CsvWriter destination) { @this.AssertNotNull("@this"); destination.AssertNotNull("destination"); var num = 0; var buffer = new DataRecord[16]; var read = 0; while ((read = await @this.ReadDataRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0) { await destination.WriteRecordsAsync(buffer, 0, read).ConfigureAwait(false); num += read; } return num; }
/// <summary> /// Copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>. /// </summary> /// <param name="this"> /// The data source. /// </param> /// <param name="destination"> /// The data destination. /// </param> /// <returns> /// The number of records written to <paramref name="destination"/>. /// </returns> public static int CopyTo(this CsvReader @this, CsvWriter destination) { @this.AssertNotNull("@this"); destination.AssertNotNull("destination"); var num = 0; var buffer = new DataRecord[16]; var read = 0; while ((read = @this.ReadDataRecords(buffer, 0, buffer.Length)) != 0) { destination.WriteRecords(buffer, 0, read); num += read; } return num; }
// this doesn't take into account auxiliary data such as: sync roots, vtables and so on public static int SizeOfUsefulData(this Type t) { t.AssertNotNull(); if (t.IsValueType) { // todo. will crash for structures that have reference-type fields return Marshal.SizeOf(t); } else if (t.IsArray) { // no idea - since arrays can have different size throw AssertionHelper.Fail(); } else if (t.IsClass) { // todo. to be implemented throw AssertionHelper.Fail(); } else { throw AssertionHelper.Fail(); } }
/// <summary> /// Populates <paramref name="this"/> with data read asynchronously from <paramref name="csvReader"/>. /// </summary> /// <remarks> /// <para> /// If <paramref name="this"/> has columns defined, those columns will be used when populating the data. If no columns have been defined, <paramref name="csvReader"/> must have a /// <see cref="HeaderRecord"/>, which is then used to define the columns for <paramref name="this"/>. If any data record has more values than can fit into the columns defined on /// <paramref name="this"/>, an exception is thrown. /// </para> /// </remarks> /// <param name="this"> /// The <see cref="DataTable"/>. /// </param> /// <param name="csvReader"> /// The <see cref="CsvReader"/>. /// </param> /// <param name="maximumRecords"> /// The maximum number of records to read and add to <paramref name="this"/>. /// </param> /// <returns> /// The number of rows added to <paramref name="this"/> (and therefore the number of data records read from <paramref name="csvReader"/>). /// </returns> public async static Task<int> FillAsync(this DataTable @this, CsvReader csvReader, int? maximumRecords) { @this.AssertNotNull("@this"); csvReader.AssertNotNull("csvReader"); exceptionHelper.ResolveAndThrowIf(maximumRecords.GetValueOrDefault() < 0, "maximumRecordsMustBePositive"); if (@this.Columns.Count == 0) { // table has no columns, so we need to use the CSV header record to populate them exceptionHelper.ResolveAndThrowIf(csvReader.HeaderRecord == null, "noColumnsAndNoHeaderRecord"); foreach (var columnName in csvReader.HeaderRecord) { @this.Columns.Add(columnName); } } var remaining = maximumRecords.GetValueOrDefault(int.MaxValue); var buffer = new DataRecord[16]; while (remaining > 0) { var read = await csvReader.ReadDataRecordsAsync(buffer, 0, Math.Min(buffer.Length, remaining)).ConfigureAwait(false); if (read == 0) { // no more data break; } for (var i = 0; i < read; ++i) { var record = buffer[i]; exceptionHelper.ResolveAndThrowIf(record.Count > @this.Columns.Count, "moreValuesThanColumns", @this.Columns.Count, record.Count); var recordAsStrings = new string[record.Count]; record.CopyTo(recordAsStrings, 0); @this.Rows.Add(recordAsStrings); } remaining -= read; } return maximumRecords.GetValueOrDefault(int.MaxValue) - remaining; }
public static IndentedWriter Indented(this TextWriter writer) { writer.AssertNotNull(); var indented = writer as IndentedWriter; return indented ?? new IndentedWriter(writer); }
public static IndentedWriter Indented(this StringBuilder buf) { buf.AssertNotNull(); return new IndentedWriter(buf); }
public static TypeRule Adhoc(this Func<Type, bool> t) { t.AssertNotNull(); return new TypeRule(t); }
// note. these signatures cause confusion and erroneous overloads // don't uncomment or reintroduce them // public static Object Get<T>(this T target, String name) // { // return target.GetImpl(name, typeof(T), null); // } // // public static Object GetOrDefault<T>(this T target, String name) // { // return target.GetOrDefault<T>(name, default(T)); // } // // public static Object GetOrDefault<T>(this T target, String name, Object @default) // { // return target.GetImpl(name, typeof(T), () => @default); // } // // public static Object GetOrDefault<T>(this T target, String name, Func<Object> @default) // { // return target.GetImpl(name, typeof(T), @default); // } // // public static Object GetOrDefault<T>(this T target, String name, Func<T, Object> @default) // { // return target.GetImpl(name, typeof(T), () => @default(target)); // } private static Object GetImpl(this Object target, String name, Type t, Func<Object> @default) { target.AssertNotNull(); t = t ?? target.GetType(); var f = t.GetField(name, BF.All); var p = t.GetProperties(BF.All).Where(pi => pi.Name == name && pi.GetIndexParameters().IsEmpty()).SingleOrDefault(); (f != null && p != null).AssertFalse(); if (f == null && p == null) { // try to find private slots in base classes var private_fs = t.Hierarchy().Select(bt => bt.GetField(name, BF.All)); var private_ps = t.Hierarchy().Select(bt => bt.GetProperties(BF.All).Where(pi => pi.Name == name && pi.GetIndexParameters().IsEmpty()).SingleOrDefault2()); f = private_fs.SingleOrDefault2(private_f => private_f != null); p = private_ps.SingleOrDefault2(private_f => private_f != null); (f != null && p != null).AssertFalse(); if (f == null && p == null) { // if this doesn't help - we give up if (@default == null) { throw AssertionHelper.Fail(); } else { return @default(); } } } if (f != null) { return f.GetValue(target); } else { return p.GetValue(target, null); } }
public static DelayedWriter Delayed(this StringBuilder buf) { buf.AssertNotNull(); return new DelayedWriter(buf); }
public static Visibility Visibility(this MemberInfo mi) { mi.AssertNotNull(); if (mi is Type) { var t = mi.AssertCast<Type>(); if (t.IsNested) { if (t.IsNestedPublic) return Reflection.Visibility.Public; if (t.IsNestedFamORAssem) return Reflection.Visibility.FamilyOrAssembly; if (t.IsNestedFamily) return Reflection.Visibility.Family; if (t.IsNestedAssembly) return Reflection.Visibility.Assembly; if (t.IsNestedFamANDAssem) return Reflection.Visibility.FamilyAndAssembly; if (t.IsNestedPrivate) return Reflection.Visibility.Private; throw AssertionHelper.Fail(); } else { if (t.IsPublic) return Reflection.Visibility.Public; return Reflection.Visibility.Assembly; } } else if (mi is FieldInfo) { var fi = mi.AssertCast<FieldInfo>(); if (fi.IsPublic) return Reflection.Visibility.Public; if (fi.IsFamilyOrAssembly) return Reflection.Visibility.FamilyOrAssembly; if (fi.IsFamily) return Reflection.Visibility.Family; if (fi.IsAssembly) return Reflection.Visibility.Assembly; if (fi.IsFamilyAndAssembly) return Reflection.Visibility.FamilyAndAssembly; if (fi.IsPrivate) return Reflection.Visibility.Private; throw AssertionHelper.Fail(); } else if (mi is MethodBase) { var mb = mi.AssertCast<MethodBase>(); if (mb.IsPublic) return Reflection.Visibility.Public; if (mb.IsFamilyOrAssembly) return Reflection.Visibility.FamilyOrAssembly; if (mb.IsFamily) return Reflection.Visibility.Family; if (mb.IsAssembly) return Reflection.Visibility.Assembly; if (mb.IsFamilyAndAssembly) return Reflection.Visibility.FamilyAndAssembly; if (mb.IsPrivate) return Reflection.Visibility.Private; throw AssertionHelper.Fail(); } else if (mi is PropertyInfo) { var pi = mi.AssertCast<PropertyInfo>(); var getter = pi.CanRead ? null : pi.GetGetMethod(true); var setter = pi.CanRead ? null : pi.GetGetMethod(true); if (getter == null || setter == null) { throw AssertionHelper.Fail(); } else if (getter == null || setter == null) { return (getter ?? setter).Visibility(); } else { var g_vis = getter.Visibility(); var s_vis = setter.Visibility(); return (Visibility)Math.Max((int)g_vis, (int)s_vis); } } else { throw AssertionHelper.Fail(); } }
public static TypeRule Rule(this Func<Type, bool> t) { t.AssertNotNull(); IDisposable _; return Rule(t, out _); }
public static PropertyRule Rule(this Func<PropertyInfo, bool> pi) { pi.AssertNotNull(); IDisposable _; return Rule(pi, out _); }
public static PropertyRule Adhoc(this Func<PropertyInfo, bool> pi) { pi.AssertNotNull(); return new PropertyRule(pi); }
/// <summary> /// Creates a table in <paramref name="this"/> and populates it with data read asynchronously from <paramref name="csvReader"/>. /// </summary> /// <remarks> /// <para> /// <paramref name="csvReader"/> must have a <see cref="HeaderRecord"/>, which is used to populate the column names of the <see cref="DataTable"/>. /// </para> /// </remarks> /// <param name="this"> /// The <see cref="DataSet"/>. /// </param> /// <param name="csvReader"> /// The <see cref="CsvReader"/>. /// </param> /// <param name="tableName"> /// The name of the table to create and add to <paramref name="this"/> /// </param> /// <param name="maximumRecords"> /// The maximum number of records to read and add to the <see cref="DataTable"/>. /// </param> /// <returns> /// The number of rows added to the <see cref="DataTable"/> (and therefore the number of data records read from <paramref name="csvReader"/>). /// </returns> public async static Task<int> FillAsync(this DataSet @this, CsvReader csvReader, string tableName, int? maximumRecords) { @this.AssertNotNull("@this"); tableName.AssertNotNull("tableName"); var table = @this.Tables.Add(tableName); return await table.FillAsync(csvReader, maximumRecords).ConfigureAwait(false); }
/// <summary> /// Asynchronously writes all rows in <paramref name="this"/> to <paramref name="csvWriter"/>. /// </summary> /// <remarks> /// </remarks> /// <param name="this"> /// The <see cref="DataTable"/>. /// </param> /// <param name="csvWriter"> /// The <see cref="CsvWriter"/>. /// </param> /// <param name="writeHeaderRecord"> /// If <see langword="true"/>, a header record will also be written, which will be comprised of the column names defined for <paramref name="this"/>. /// </param> /// <param name="maximumRows"> /// The maximum number of rows from <paramref name="this"/> that should be written to <paramref name="csvWriter"/>. /// </param> /// <param name="objectToStringConverter"> /// Provides a means of converting values in the <see cref="DataRow"/>s to <see cref="String"/>s. /// </param> /// <returns> /// The actual number of rows from <paramref name="this"/> written to <paramref name="csvWriter"/>. /// </returns> public async static Task<int> WriteCsvAsync(this DataTable @this, CsvWriter csvWriter, bool writeHeaderRecord, int? maximumRows, Func<object, string> objectToStringConverter) { @this.AssertNotNull("@this"); csvWriter.AssertNotNull("csvWriter"); objectToStringConverter.AssertNotNull("objectToStringConverter"); var num = 0; if (writeHeaderRecord) { var columnNames = new string[@this.Columns.Count]; for (var i = 0; i < columnNames.Length; ++i) { columnNames[i] = @this.Columns[i].ColumnName; } await csvWriter.WriteRecordAsync(columnNames).ConfigureAwait(false); } var maximum = maximumRows.GetValueOrDefault(int.MaxValue); var buffer = new DataRecord[16]; var bufferOffset = 0; foreach (DataRow row in @this.Rows) { var record = new DataRecord(); for (var i = 0; i < row.ItemArray.Length; ++i) { record.Add(objectToStringConverter(row.ItemArray[i])); } buffer[bufferOffset++] = record; if (bufferOffset == buffer.Length) { // buffer full await csvWriter.WriteRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false); bufferOffset = 0; } if (++num == maximum) { break; } } // write any outstanding data in buffer await csvWriter.WriteRecordsAsync(buffer, 0, bufferOffset).ConfigureAwait(false); return num; }