<MSDN笔记>Lambdas with the Standard Query Operators

本文深入解析了C#中Func委托的概念、语法特性及其在数据集操作中的应用实例,包括如何定义、实例化和使用Func委托来实现用户自定义表达式,并通过Count、TakeWhile等标准查询操作展示其在数据过滤、计数等方面的强大功能。

Many Standard query operators have an input parameter whose type is one of the Func<T, TResult> family of generic delegates. The Func<T, TResult> delegates use type parameters to define the number and type of input parameters, and the return type of the delegate. Func delegates are very useful for encapsulating user-defined expressions that are applied to each element in a set of source data. For example, consider the following delegate type:


public delegate TResult Func<TArg0, TResult>(TArg0 arg0)

The delegate can be instantiated as Func<int,bool> myFunc where int is an input parameter and bool is the return value. The return value is always specified in the last type parameter. Func<int, string, bool> defines a delegate with two input parameters, int and string, and a return type of bool. The following Func delegate, when it is invoked, will return true or false to indicate whether the input parameter is equal to 5:

Func<int, bool> myFunc = x => x == 5;
    bool result = myFunc(4); // returns false of course

You can also supply a lambda expression when the argument type is an Expression<Func>, for example in the standard query operators that are defined in System.Linq.Queryable. When you specify an Expression<Func> argument, the lambda will be compiled to an expression tree.

A standard query operator, the Count method, is shown here:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    int oddNumbers = numbers.Count(n => n % 2 == 1);

The compiler can infer the type of the input parameter, or you can also specify it explicitly. This particular lambda expression counts those integers (n) which when divided by two have a remainder of 1.

The following method will produce a sequence that contains all the elements in the numbers array that are to the left of the "9" because that is the first number in the sequence that does not meet the condition:

var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
This example shows how to specify multiple input parameters by enclosing them in parentheses. The method returns all the elements in the numbers array until a number is encountered whose value is less than its position. Do not confuse the lambda operator ( =>) with the greater than or equal operator ( >=).

var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);




// __msvc_bit_utils.hpp internal header (core) // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #ifndef __MSVC_BIT_UTILS_HPP #define __MSVC_BIT_UTILS_HPP #include <yvals_core.h> #if _STL_COMPILER_PREPROCESSOR #include <climits> #include <xtr1common> #include _STL_INTRIN_HEADER #pragma pack(push, _CRT_PACKING) #pragma warning(push, _STL_WARNING_LEVEL) #pragma warning(disable : _STL_DISABLED_WARNINGS) _STL_DISABLE_CLANG_WARNINGS #pragma push_macro("new") #undef new _STD_BEGIN extern "C" { extern int __isa_available; } _INLINE_VAR constexpr int _Stl_isa_available_sse42 = 2; // equal to __ISA_AVAILABLE_SSE42 _INLINE_VAR constexpr int _Stl_isa_available_avx2 = 5; // equal to __ISA_AVAILABLE_AVX2 template <class _UInt> constexpr int _Unsigned_integer_digits = sizeof(_UInt) * CHAR_BIT; // Implementation of countl_zero without using specialized CPU instructions. // Used at compile time and when said instructions are not supported. // see "Hacker's Delight" section 5-3 template <class _Ty> _NODISCARD constexpr int _Countl_zero_fallback(_Ty _Val) noexcept { _Ty _Yx = 0; unsigned int _Nx = _Unsigned_integer_digits<_Ty>; unsigned int _Cx = _Unsigned_integer_digits<_Ty> / 2; do { _Yx = static_cast<_Ty>(_Val >> _Cx); if (_Yx != 0) { _Nx -= _Cx; _Val = _Yx; } _Cx >>= 1; } while (_Cx != 0); return static_cast<int>(_Nx) - static_cast<int>(_Val); } #if !defined(_M_CEE_PURE) && !defined(__CUDACC__) #define _HAS_COUNTL_ZERO_INTRINSICS 1 #else // ^^^ intrinsics available / intrinsics unavailable vvv #define _HAS_COUNTL_ZERO_INTRINSICS 0 #endif // ^^^ intrinsics unavailable ^^^ #if _HAS_COUNTL_ZERO_INTRINSICS #if (defined(_M_IX86) && !defined(_M_HYBRID_X86_ARM64)) || (defined(_M_X64) && !defined(_M_ARM64EC)) template <class _Ty> _NODISCARD int _Countl_zero_lzcnt(const _Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; if constexpr (_Digits <= 16) { return static_cast<int>(__lzcnt16(_Val) - (16 - _Digits)); } else if constexpr (_Digits == 32) { return static_cast<int>(__lzcnt(_Val)); } else { #ifdef _M_IX86 const unsigned int _High = _Val >> 32; const auto _Low = static_cast<unsigned int>(_Val); if (_High == 0) { return 32 + _Countl_zero_lzcnt(_Low); } else { return _Countl_zero_lzcnt(_High); } #else // ^^^ defined(_M_IX86) / !defined(_M_IX86) vvv return static_cast<int>(__lzcnt64(_Val)); #endif // ^^^ !defined(_M_IX86) ^^^ } } template <class _Ty> _NODISCARD int _Countl_zero_bsr(const _Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; unsigned long _Result; if constexpr (_Digits <= 32) { if (!_BitScanReverse(&_Result, _Val)) { return _Digits; } } else { #ifdef _M_IX86 const unsigned int _High = _Val >> 32; if (_BitScanReverse(&_Result, _High)) { return static_cast<int>(31 - _Result); } const auto _Low = static_cast<unsigned int>(_Val); if (!_BitScanReverse(&_Result, _Low)) { return _Digits; } #else // ^^^ defined(_M_IX86) / !defined(_M_IX86) vvv if (!_BitScanReverse64(&_Result, _Val)) { return _Digits; } #endif // ^^^ !defined(_M_IX86) ^^^ } return static_cast<int>(_Digits - 1 - _Result); } template <class _Ty> _NODISCARD int _Checked_x86_x64_countl_zero(const _Ty _Val) noexcept { #ifdef __AVX2__ return _Countl_zero_lzcnt(_Val); #else // ^^^ defined(__AVX2__) / !defined(__AVX2__) vvv const bool _Definitely_have_lzcnt = __isa_available >= _Stl_isa_available_avx2; if (_Definitely_have_lzcnt) { return _Countl_zero_lzcnt(_Val); } else { return _Countl_zero_bsr(_Val); } #endif // ^^^ !defined(__AVX2__) ^^^ } #endif // (defined(_M_IX86) && !defined(_M_HYBRID_X86_ARM64)) || (defined(_M_X64) && !defined(_M_ARM64EC)) #if defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64) template <class _Ty> _NODISCARD int _Checked_arm_arm64_countl_zero(const _Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; if (_Val == 0) { return _Digits; } if constexpr (_Digits <= 32) { return static_cast<int>(_CountLeadingZeros(_Val)) - (_Unsigned_integer_digits<unsigned long> - _Digits); } else { return static_cast<int>(_CountLeadingZeros64(_Val)); } } #endif // defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64) #endif // _HAS_COUNTL_ZERO_INTRINSICS // Implementation of countr_zero without using specialized CPU instructions. // Used at compile time and when said instructions are not supported. // see "Hacker's Delight" section 5-4 template <class _Ty> _NODISCARD constexpr int _Countr_zero_fallback(const _Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; return _Digits - _Countl_zero_fallback(static_cast<_Ty>(static_cast<_Ty>(~_Val) & static_cast<_Ty>(_Val - 1))); } // Implementation of popcount without using specialized CPU instructions. // Used at compile time and when said instructions are not supported. template <class _Ty> _NODISCARD constexpr int _Popcount_fallback(_Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; #if (defined(_M_IX86) && !defined(_M_HYBRID_X86_ARM64)) || defined(_M_ARM) if constexpr (_Digits == 64) { // 64-bit bit operations on architectures without 64-bit registers are less efficient, // hence we split the value so that it fits in 32-bit registers return _Popcount_fallback(static_cast<unsigned long>(_Val)) + _Popcount_fallback(static_cast<unsigned long>(_Val >> 32)); } else #endif // (defined(_M_IX86) && !defined(_M_HYBRID_X86_ARM64)) || defined(_M_ARM) { // we static_cast these bit patterns in order to truncate them to the correct size _Val = static_cast<_Ty>(_Val - ((_Val >> 1) & static_cast<_Ty>(0x5555'5555'5555'5555ull))); _Val = static_cast<_Ty>((_Val & static_cast<_Ty>(0x3333'3333'3333'3333ull)) + ((_Val >> 2) & static_cast<_Ty>(0x3333'3333'3333'3333ull))); _Val = static_cast<_Ty>((_Val + (_Val >> 4)) & static_cast<_Ty>(0x0F0F'0F0F'0F0F'0F0Full)); // Multiply by one in each byte, so that it will have the sum of all source bytes in the highest byte _Val = static_cast<_Ty>(_Val * static_cast<_Ty>(0x0101'0101'0101'0101ull)); // Extract highest byte return static_cast<int>(_Val >> (_Digits - 8)); } } #if ((defined(_M_IX86) && !defined(_M_HYBRID_X86_ARM64)) || (defined(_M_X64) && !defined(_M_ARM64EC))) \ && !defined(_M_CEE_PURE) && !defined(__CUDACC__) #define _HAS_TZCNT_BSF_INTRINSICS 1 #else // ^^^ intrinsics available / intrinsics unavailable vvv #define _HAS_TZCNT_BSF_INTRINSICS 0 #endif // ^^^ intrinsics unavailable ^^^ #if _HAS_TZCNT_BSF_INTRINSICS #ifdef __clang__ #define _TZCNT_U32 __builtin_ia32_tzcnt_u32 #define _TZCNT_U64 __builtin_ia32_tzcnt_u64 #else // ^^^ __clang__ / !__clang__ vvv #define _TZCNT_U32 _tzcnt_u32 #define _TZCNT_U64 _tzcnt_u64 #endif // __clang__ template <class _Ty> _NODISCARD int _Countr_zero_tzcnt(const _Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; constexpr _Ty _Max = static_cast<_Ty>(-1); // equal to (numeric_limits<_Ty>::max)() if constexpr (_Digits <= 32) { // Intended widening to int. This operation means that a narrow 0 will widen // to 0xFFFF....FFFF0... instead of 0. We need this to avoid counting all the zeros // of the wider type. return static_cast<int>(_TZCNT_U32(static_cast<unsigned int>(~_Max | _Val))); } else { #ifdef _M_IX86 const auto _Low = static_cast<unsigned int>(_Val); if (_Low == 0) { const unsigned int _High = _Val >> 32; return static_cast<int>(32 + _TZCNT_U32(_High)); } else { return static_cast<int>(_TZCNT_U32(_Low)); } #else // ^^^ defined(_M_IX86) / !defined(_M_IX86) vvv return static_cast<int>(_TZCNT_U64(_Val)); #endif // ^^^ !defined(_M_IX86) ^^^ } } #undef _TZCNT_U32 #undef _TZCNT_U64 template <class _Ty> _NODISCARD int _Countr_zero_bsf(const _Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; constexpr _Ty _Max = static_cast<_Ty>(-1); // equal to (numeric_limits<_Ty>::max)() unsigned long _Result; if constexpr (_Digits <= 32) { // Intended widening to int. This operation means that a narrow 0 will widen // to 0xFFFF....FFFF0... instead of 0. We need this to avoid counting all the zeros // of the wider type. if (!_BitScanForward(&_Result, static_cast<unsigned int>(~_Max | _Val))) { return _Digits; } } else { #ifdef _M_IX86 const auto _Low = static_cast<unsigned int>(_Val); if (_BitScanForward(&_Result, _Low)) { return static_cast<int>(_Result); } const unsigned int _High = _Val >> 32; if (!_BitScanForward(&_Result, _High)) { return _Digits; } else { return static_cast<int>(_Result + 32); } #else // ^^^ defined(_M_IX86) / !defined(_M_IX86) vvv if (!_BitScanForward64(&_Result, _Val)) { return _Digits; } #endif // ^^^ !defined(_M_IX86) ^^^ } return static_cast<int>(_Result); } template <class _Ty> _NODISCARD int _Checked_x86_x64_countr_zero(const _Ty _Val) noexcept { #ifdef __AVX2__ return _Countr_zero_tzcnt(_Val); #else // ^^^ defined(__AVX2__) / !defined(__AVX2__) vvv const bool _Definitely_have_tzcnt = __isa_available >= _Stl_isa_available_avx2; if (_Definitely_have_tzcnt) { return _Countr_zero_tzcnt(_Val); } else { return _Countr_zero_bsf(_Val); } #endif // ^^^ !defined(__AVX2__) ^^^ } #endif // _HAS_TZCNT_BSF_INTRINSICS #if (defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64)) && !defined(_M_CEE_PURE) && !defined(__CUDACC__) #define _HAS_POPCNT_INTRINSICS 1 #if defined(__AVX__) || defined(_M_ARM64) || defined(_M_ARM64EC) #define _POPCNT_INTRINSICS_ALWAYS_AVAILABLE 1 #else // ^^^ intrinsics always available / intrinsics not always available vvv #define _POPCNT_INTRINSICS_ALWAYS_AVAILABLE 0 #endif // ^^^ intrinsics not always available ^^^ #else // ^^^ intrinsics available / intrinsics unavailable vvv #define _HAS_POPCNT_INTRINSICS 0 #endif // ^^^ intrinsics unavailable ^^^ #if _HAS_POPCNT_INTRINSICS template <class _Ty> _NODISCARD int _Unchecked_popcount(const _Ty _Val) noexcept { constexpr int _Digits = _Unsigned_integer_digits<_Ty>; if constexpr (_Digits <= 16) { return static_cast<int>(__popcnt16(_Val)); } else if constexpr (_Digits == 32) { return static_cast<int>(__popcnt(_Val)); } else { #ifdef _M_IX86 return static_cast<int>(__popcnt(_Val >> 32) + __popcnt(static_cast<unsigned int>(_Val))); #else // ^^^ defined(_M_IX86) / !defined(_M_IX86) vvv return static_cast<int>(__popcnt64(_Val)); #endif // ^^^ !defined(_M_IX86) ^^^ } } template <class _Ty> _NODISCARD int _Checked_popcount(const _Ty _Val) noexcept { #if !_POPCNT_INTRINSICS_ALWAYS_AVAILABLE const bool _Definitely_have_popcnt = __isa_available >= _Stl_isa_available_sse42; if (!_Definitely_have_popcnt) { return _Popcount_fallback(_Val); } #endif // ^^^ !_POPCNT_INTRINSICS_ALWAYS_AVAILABLE ^^^ return _Unchecked_popcount(_Val); } #endif // ^^^ _HAS_POPCNT_INTRINSICS ^^^ template <class _Ty> constexpr bool _Is_standard_unsigned_integer = _Is_any_of_v<remove_cv_t<_Ty>, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long>; template <class _Ty, enable_if_t<_Is_standard_unsigned_integer<_Ty>, int> = 0> _NODISCARD _CONSTEXPR20 int _Countr_zero(const _Ty _Val) noexcept { #if _HAS_TZCNT_BSF_INTRINSICS #if _HAS_CXX20 if (!_STD is_constant_evaluated()) #endif // _HAS_CXX20 { return _Checked_x86_x64_countr_zero(_Val); } #endif // _HAS_TZCNT_BSF_INTRINSICS return _Countr_zero_fallback(_Val); } template <class _Ty, class _Fn> constexpr decltype(auto) _Select_countr_zero_impl(_Fn _Callback) { // TRANSITION, DevCom-1527995: Lambdas in this function ensure inlining #if _HAS_TZCNT_BSF_INTRINSICS && _HAS_CXX20 if (!_STD is_constant_evaluated()) { #ifdef __AVX2__ return _Callback([](_Ty _Val) _STATIC_LAMBDA { return _Countr_zero_tzcnt(_Val); }); #else // ^^^ AVX2 / not AVX2 vvv const bool _Definitely_have_tzcnt = __isa_available >= _Stl_isa_available_avx2; if (_Definitely_have_tzcnt) { return _Callback([](_Ty _Val) _STATIC_LAMBDA { return _Countr_zero_tzcnt(_Val); }); } else { return _Callback([](_Ty _Val) _STATIC_LAMBDA { return _Countr_zero_bsf(_Val); }); } #endif // ^^^ not AVX2 ^^^ } #endif // ^^^ _HAS_TZCNT_BSF_INTRINSICS && _HAS_CXX20 ^^^ // C++17 constexpr gcd() calls this function, so it should be constexpr unless we detect runtime evaluation. return _Callback([](_Ty _Val) _STATIC_LAMBDA { return _Countr_zero_fallback(_Val); }); } template <class _Ty, enable_if_t<_Is_standard_unsigned_integer<_Ty>, int> = 0> _NODISCARD _CONSTEXPR20 int _Popcount(const _Ty _Val) noexcept { #if _HAS_POPCNT_INTRINSICS #if _HAS_CXX20 if (!_STD is_constant_evaluated()) #endif // _HAS_CXX20 { return _Checked_popcount(_Val); } #endif // ^^^ _HAS_POPCNT_INTRINSICS ^^^ return _Popcount_fallback(_Val); } template <class _Ty, class _Fn> _CONSTEXPR20 decltype(auto) _Select_popcount_impl(_Fn _Callback) { // TRANSITION, DevCom-1527995: Lambdas in this function ensure inlining #if _HAS_POPCNT_INTRINSICS #if _HAS_CXX20 if (!_STD is_constant_evaluated()) #endif // _HAS_CXX20 { #if !_POPCNT_INTRINSICS_ALWAYS_AVAILABLE const bool _Definitely_have_popcnt = __isa_available >= _Stl_isa_available_sse42; if (!_Definitely_have_popcnt) { return _Callback([](_Ty _Val) _STATIC_LAMBDA { return _Popcount_fallback(_Val); }); } #endif // ^^^ !_POPCNT_INTRINSICS_ALWAYS_AVAILABLE ^^^ return _Callback([](_Ty _Val) _STATIC_LAMBDA { return _Unchecked_popcount(_Val); }); } #endif // ^^^ _HAS_POPCNT_INTRINSICS ^^^ return _Callback([](_Ty _Val) _STATIC_LAMBDA { return _Popcount_fallback(_Val); }); } #undef _HAS_POPCNT_INTRINSICS #undef _HAS_TZCNT_BSF_INTRINSICS #undef _POPCNT_INTRINSICS_ALWAYS_AVAILABLE _STD_END #pragma pop_macro("new") _STL_RESTORE_CLANG_WARNINGS #pragma warning(pop) #pragma pack(pop) #endif // _STL_COMPILER_PREPROCESSOR #endif // __MSVC_BIT_UTILS_HPP 对代码进行解释
01-03
【3D应力敏感度分析拓扑优化】【基于p-范数全局应力衡量的3D敏感度分析】基于伴随方法的有限元分析和p-范数应力敏感度分析(Matlab代码实现)内容概要:本文档介绍了基于伴随方法的有限元分析与p-范数全局应力衡量的3D应力敏感度分析,并结合拓扑优化技术,提供了完整的Matlab代码实现方案。该方法通过有限元建模计算结构在载荷作用下的应力分布,采用p-范数对全局应力进行有效聚合,避免传统方法中应力约束过多的问题,进而利用伴随法高效求解设计变量对应力的敏感度,为结构优化提供关键梯度信息。整个流程涵盖了从有限元分析、应力评估到敏感度计算的核心环节,适用于复杂三维结构的轻量化与高强度设计。; 适合人群:具备有限元分析基础、拓扑优化背景及Matlab编程能力的研究生、科研人员与工程技术人员,尤其适合从事结构设计、力学仿真与多学科优化的相关从业者; 使用场景及目标:①用于实现高精度三维结构的应力约束拓扑优化;②帮助理解伴随法在敏感度分析中的应用原理与编程实现;③服务于科研复现、论文写作与工程项目中的结构性能提升需求; 阅读建议:建议读者结合有限元理论与优化算法知识,逐步调试Matlab代码,重点关注伴随方程的构建与p-范数的数值处理技巧,以深入掌握方法本质并实现个性化拓展。
下载前必看:https://pan.quark.cn/s/9f13b242f4b9 Android 平板设备远程操控个人计算机的指南 Android 平板设备远程操控个人计算机的指南详细阐述了如何运用 Splashtop Remote 应用程序达成 Android 平板设备对个人计算机的远程操控。 该指南被划分为四个环节:首先,在个人计算机上获取并部署 Splashtop Remote 应用程序,并设定客户端密码;其次,在 Android 平板设备上获取并部署 Splashtop Remote 应用程序,并与之建立连接至个人计算机的通道;再次,在 Splashtop Remote 应用程序中识别已部署个人计算机端软件的设备;最后,运用平板设备对个人计算机实施远程操控。 关键点1:Splashtop Remote 应用程序的部署与配置* 在个人计算机上获取并部署 Splashtop Remote 应用程序,可通过官方网站或其他获取途径进行下载。 * 部署结束后,必须输入客户端密码,该密码在平板控制计算机时用作验证,密码长度至少为8个字符,且需包含字母与数字。 * 在配置选项中,能够设定是否在设备启动时自动运行客户端,以及进行互联网搜索设置。 关键点2:Splashtop Remote 应用程序的 Android 版本获取与部署* 在 Android 平板设备上获取并部署 Splashtop Remote 应用程序,可通过 Google Play Store 或其他获取途径进行下载。 * 部署结束后,必须输入客户端密码,该密码用于连接至个人计算机端软件。 关键点3:运用 Splashtop Remote 远程操控个人计算机* 在 Splashtop Remote 应用程序中识别...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值