13 I have the following vectors with 0s and 1s: test1 <- c(rep(0,20),rep(1,5),rep(0,10),rep(1,15)) test1 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 […]
18 This is from: https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/include/std/type_traits template<typename _Xp, typename _Yp> using __cond_res = decltype(false ? declval<_Xp(&)()>()() : declval<_Yp(&)()>()()); … template<typename _Tp1, typename _Tp2> struct __common_reference_impl<_Tp1, _Tp2, 3, void_t<__cond_res<_Tp1, _Tp2>>> { using type = __cond_res<_Tp1, _Tp2>; }; I’m trying to figure out what _Xp(&)() is – is it a function call signature? i.e. a constructor? Doesn’t really […]
9 I am wondering if there can be a version of Ackermann function with better time complexity than the standard variation. This is not a homework and I am just curious. I know the Ackermann function doesn’t have any practical use besides as a performance benchmark, because of the deep recursion. I know the numbers […]
11 Generally, the default constructor should be the fastest way of making an empty container. That’s why I was surprised to see that it’s worse than initializing to an empty string literal: #include <string> std::string make_default() { return {}; } std::string make_empty() { return ""; } This compiles to: (clang 16, libc++) make_default(): mov rax, […]
13 For min(ctz(x), ctz(y)), we can use ctz(x | y) to gain better performance. But what about max(ctz(x), ctz(y))? ctz represents "count trailing zeros". C++ Version (Compiler Explorer) #include <algorithm> #include <bit> #include <cstdint> int32_t test2(uint64_t x, uint64_t y) { return std::max(std::countr_zero(x), std::countr_zero(y)); } Rust Version (Compiler Explorer) pub fn test2(x: u64, y: u64) -> […]
11 I’m looking to find an efficient method of matching all values of vector x in vector y rather than just the first position, as is returned by match(). What I’m after essentially is the default behavior of pmatch() but without partial matching: x <- c(3L, 1L, 2L, 3L, 3L, 2L) y <- c(3L, 3L, […]
7 I have a Pandas DataFrame that looks like this: df = pd.DataFrame({‘col1’: [1, 2, 3], ‘col2’: [4, 5, 6], ‘col3’: [7, 8, 9]}) df col1 col2 col3 0 1 4 7 1 2 5 8 2 3 6 9 I would like to create a Pandas DataFrame like this: df_new col1 col2 col3 0 […]
6 The following C# program silently and implicitly calls an explicit decimal-to-long conversion operator, losing precision. I don’t understand why this happens. As far as I understand, in C# explicit operator should not be called implicitly by the language. Especially that in this case the silent explicit conversion is losing precision (1.1M => 1L). This […]
17 This is a question from Rust quiz 28: struct Guard; impl Drop for Guard { fn drop(&mut self) { print!("1"); } } fn main() { let _guard = Guard; print!("3"); let _ = Guard; print!("2"); } Such code prints 3121, in the third line of main, assigning to _ means a immediate drop. However, […]
17 I have an Animal class with constructor and destructor. Cat have a private Brain* attribute. Upon construction, Cat creates his Brain using new Brain(); Upon destruction, Cat deletes his Brain. I don’t understand why The cat’s and brain’s destructors not called, when my Base class destructor is virtual? #include <iostream> using std::cout ; using […]