Fastest way of finding the middle value of a triple?

There’s an answer here using min/max and no branches (https://stackoverflow.com/a/14676309/2233603). Actually 4 min/max operations are enough to find the median, there’s no need for xor’s: median = max(min(a,b), min(max(a,b),c)); Though, it won’t give you the median value’s index… Breakdown of all cases: a b c 1 2 3 max(min(1,2), min(max(1,2),3)) = max(1, min(2,3)) = max(1, … Read more

In a “for” statement, should I use `!=` or `

for(i = start; i != end; ++i) This is the “standard” iterator loop. It has the advantage that it works with both pointers and standard library iterators (you can’t rely on iterators having operator< defined). for(i = start; i < end; ++i) This won’t work with standard library iterators (unless they have operator< defined), but … Read more

Why does the Java compiler not understand this variable is always initialized?

As part of aiming for portability, there is a very specific set of rules for what a compiler should accept and what it should reject. Those rules both permit and require only a limited form of flow analysis when determining whether a variable is definitely assigned at its use. See the Java Language Specification Chapter … Read more

How do I conditionally check if an enum is one variant or another?

First have a look at the free, official Rust book The Rust Programming Language, specifically the chapter on enums. match fn initialize(datastore: DatabaseType) { match datastore { DatabaseType::Memory => { // … } DatabaseType::RocksDB => { // … } } } if let fn initialize(datastore: DatabaseType) { if let DatabaseType::Memory = datastore { // … … Read more

Two conditions in one if statement does the second matter if the first is false?

It is common for languages (Java and Python are among them) to evaluate the first argument of a logical AND and finish evaluation of the statement if the first argument is false. This is because: From The Order of Evaluation of Logic Operators, When Java evaluates the expression d = b && c;, it first … Read more

How do I combine 2 select statements into one?

You have two choices here. The first is to have two result sets which will set ‘Test1’ or ‘Test2’ based on the condition in the WHERE clause, and then UNION them together: select ‘Test1′, * from TABLE Where CCC=’D’ AND DDD=’X’ AND exists(select …) UNION select ‘Test2′, * from TABLE Where CCC<>’D’ AND DDD=’X’ AND … Read more