clang-format: always break EACH PARAMETER if don't fit - clang

// Try this first, ref 1:
SomeCall(aaa, bbb, ccc);
// If doesn't fit do this, ref 2:
SomeCall(aaa,
bbb,
ccc);
// Don't do, ref 3:
SomeCall(
aaa, bbb, ccc);
I find that currently the only way to automatically break after each parameter (ref 2) is if the option on ref 3 doesn't fit. I would like do be ref 2 even if ref 3 would fit. I use clang 11.

AlignAfterOpenBracket: Align
PenaltyBreakBeforeFirstCallParameter: 9999
Basically set the Penalty value incredibly high

Related

Var in var = var in var + 1

I am still new to Lua and have one question about var in var.
How do I calculate this:?
A=1
X=A
X=X+1
As you can see:
This calculation would result in
A=A+1
But this does not work for me.
I guess I have to format the cars in some way.
I want to do this because I want to be able to change a var in another var when necessary.
The = operator does two things:
Evaluate the right-hand side
Assign the result to the variable on the left-hand side
To illustrate, consider this example:
A = 1 -- A is now 1
X = A + A + A -- X is now 3, and A hasn't changed
X = X + 1 -- X is now 4, and A hasn't changed
Now lets look at your original code, and write out the meaning in plain language.
A=1 -- Create a variable 'A' and assign it the value of one
X=A -- Create the variable 'X' and assign it the current value of 'A'
X=X+1 -- Change 'X' by assigning it the current value of 'X' plus one
Notice how these comments read like "instructions" to a computer, rather than math equations. Lua (and programming in general) should be interpreted as a set of instructions executed from top to bottom.
However, as Egor Skriptunoff alludes to in earlier comments, tables behave differently. See Programming in Lua - Chapter 2.5 for a more detailed explanation of how tables are different.

How to find biggest variant in an enum in Rust?

I'm trying to improve the performance of a rust program, which requires me to reduce the size of some large enums. For example
enum EE {
A, // 0
B(i32), //4
C(i64), // 8
D(String), // 24
E { // 16
x: i64,
y: i32,
},
}
fn main() {
println!("{}", std::mem::size_of::<EE>()); // 32
}
prints 32. But if I want to know the size of EE::A, I get a compile error
error[E0573]: expected type, found variant `EE::A`
--> src/main.rs:14:40
|
14 | println!("{}", std::mem::size_of::<EE::A>());
| ^^^^^
| |
| not a type
| help: try using the variant's enum: `crate::EE`
error: aborting due to previous error
error: could not compile `play_rust`.
Is there a way to find out which variant takes the most space?
No, there is no way to get the size of just one variant of an enum. The best you can do is get the size of what the variant contains, as if it were a standalone struct:
println!("sizeof EE::A: {}", std::mem::size_of::<()>()); // 0
println!("sizeof EE::B: {}", std::mem::size_of::<i32>()); // 4
println!("sizeof EE::C: {}", std::mem::size_of::<i64>()); // 8
println!("sizeof EE::D: {}", std::mem::size_of::<String>()); // 24
println!("sizeof EE::E: {}", std::mem::size_of::<(i64, i32)>()); // 16
Even this isn't especially useful because it includes padding bytes that may be used to store the tag; as you point out, the size of the enum can be reduced to 16 if D is shrunk to a single pointer, but you can't know that from looking at just the sizes. If y were instead defined as i64, the size of each variant would be the same, but the size of the enum would need to be 24. Alignment is another confounding factor that makes the size of an enum more complex than just "the size of the largest variant plus the tag".
Of course, this is all highly platform-dependent, and your code should not rely on any enum having a particular layout (unless you can guarantee it with a #[repr] annotation).
If you have a particular enum you're worried about, it's not difficult to get the size of each contained type. Clippy also has a lint for enums with extreme size differences between variants. However, I don't recommend using size alone to make manual optimizations to enum layouts, or boxing things that are only a few pointers in size -- indirection suppresses other kinds of optimizations the compiler may be able to do. If you prioritize minimal space usage you may accidentally make your code much slower in the process.

set of WideChar: Sets may have at most 256 elements

I have this line:
const
MY_SET: set of WideChar = [WideChar('A')..WideChar('Z')];
The above does not compile, with error:
[Error] Sets may have at most 256 elements
But this line does compile ok:
var WS: WideString;
if WS[1] in [WideChar('A')..WideChar('Z')] then...
And this also compiles ok:
const
MY_SET = [WideChar('A')..WideChar('Z'), WideChar('a')..WideChar('z')];
...
if WS[1] in MY_SET then...
Why is that?
EDIT: My question is why if WS[1] in [WideChar('A')..WideChar('Z')] compiles? and why MY_SET = [WideChar('A')..WideChar('Z'), WideChar('a')..WideChar('z')]; compiles? aren't they also need to apply to the set rules?
A valid set has to obey two rules:
Each element in a set must have an ordinal value less than 256.
The set must not have more than 256 elements.
MY_SET: set of WideChar = [WideChar('A')..WideChar('Z')];
Here you declare a set type (Set of WideChar) which has more than 256 elements -> Compiler error.
if WS[1] in [WideChar('A')..WideChar('Z')]
Here, the compiler sees WideChar('A') as an ordinal value. This value and all other values in the set are below 256. This is ok with rule 1.
The number of unique elements are also within limits (Ord('Z')-Ord('A')+1), so the 2nd rules passes.
MY_SET = [WideChar('A')..WideChar('Z'), WideChar('a')..WideChar('z')];
Here you declare a set that also fulfills the requirements as above. Note that the compiler sees this as a set of ordinal values, not as a set of WideChar.
A set can have no more than 256 elements.
Even with so few elements the set already uses 32 bytes.
From the documentation:
A set is a bit array where each bit indicates whether an element is in the set or not. The maximum number of elements in a set is 256, so a set never occupies more than 32 bytes. The number of bytes occupied by a particular set is equal to
(Max div 8) - (Min div 8) + 1
For this reason only sets of byte, (ansi)char, boolean and enumerations with fewer than 257 elements are possible.
Because widechar uses 2 bytes it can have 65536 possible values.
A set of widechar would take up 8Kb, too large to be practical.
type
Capitals = 'A'..'Z';
const
MY_SET: set of Capitals = [WideChar('A')..WideChar('Z')];
Will compile and work the same.
It does seem a bit silly to use widechar if your code ignores unicode.
As written only the English capitals are recognized, you do not take into account different locales.
In this case it would be better to use code like
if (AWideChar >= 'A') and (AWideChar <= 'Z') ....
That will work no matter how many chars fall in between.
Obviously you can encapsulate this in a function to save on typing.
If you insist on having large sets, see this answer: https://stackoverflow.com/a/2281327/650492

Check similarity between two string expressions in Swift

I have scanned text:
Mils, chiiese, wh_ite ch$col_te
And expression list, example:
- cheese
- bread
- white chocolate
- etc.
I need compare broken expression with expression from my list, ex. "white chocolate" with "wh_ite ch$col_te."
Maybe you recommend some frameworks.
String distance - Levenshtein distance
What you need to do is measure the difference between two string. To do that, you can use the Levenshtein distance.
For your luck, somebody already implemented this algorihtm in Swift HERE.
To make it work in Swift 1.2, you'll just have to autofix some errors that occour, nothing too fancy.
You can then use it like this:
println(levenshtein("wh_ite ch$col_te", bStr: "white chocolate")) // prints 3, because you have to change 3 letters to get from aStr to bStr
println(levenshtein("wh_ite ch$col_te", bStr: "whsdfdsite chosdfsdfcolate")) // prints 13, because you have to change 13 letters to get from aStr to bStr
You then just set the tolerance and you are done!
Dejan Skledar's on the right track -- you want to make use of Levenshtein distance. The implementation he points to needs tweaking to work in Swift 1.2, and it tends to be slow. Here's a Swift 1.2-compatible, faster implementation.
Simply include the Tools class in your project. Once you've done that, you can get a number representing the difference between two strings this way:
Tools.levenshtein("cheese", bStr: "chee_e") // returns 1
Tools.levenshtein("butter", bStr: "b_tt_r") // returns 2
Tools.levenshtein("milk", bStr: "butter") // returns 6
Please find the Swift 4 implementation of Joey deVilla's answer here
You have to call the function like below:
Tools.levenshtein(aStr: "Example", bStr: "Examples")
Use StringMetric and be happy
https://github.com/autozimu/StringMetric.swift
import StringMetric
...
"kitten".distance(between: "sitting") // => 0.746
"君子和而不同".distance(between: "小人同而不和") // => 0.555

How to select Highvalues of the candlestick with data source

I always see the code as datasource="series0".
If series(0) is a candlestick and I want to use Highvalues or Closevalues of the candlestick, how so I select that data? Something like datasource="series0.Highvalues"? (It's worth noting that I use teechart2011 Eval and VB6).
If series(1) is the financial function ExpMovAvg, how to define the width of the ExpMovAvg line with code?
Similarly how do I use the Closevalues in Series(0) for this function? Not merely datasource="series0". Thanks !
I always see the code as datasource="series0", if series(0) is a candlestick and I want to use Highvalues or Closevalues of the candlestick,how to select that data? datasource="series0.Highvalues"? (I use teechart2011 Eval and VB6)
Here you have a simple example in VB6. You can assign any of the 4 ValueLists in the Candle series (Open, Close, High, Low) to the be used by the function with the MandatoryValueList.ValueSource property:
TChart1.AddSeries scCandle
TChart1.Series(0).FillSampleValues
TChart1.AddSeries scLine
TChart1.Series(1).SetFunction tfExpMovAvg
TChart1.Series(1).DataSource = TChart1.Series(0)
TChart1.Series(1).MandatoryValueList.ValueSource = "Close" '"Open" "High" "Low"
if series(1) is the financial function ExpMovAvg, how to define the
width of the ExpMovAvg line with code?
You can set the series' Pen.Width property as follows:
TChart1.Series(1).Pen.Width = 2
Similarly how to use the Closevalues in Series(0) for this function?
not merely datasource="series0", thanks !
This is the same above, isn't it?

Resources