What does RVO do for: Copy Elision vs. List-initialization? - c++17

I have two cases and not sure which is considered correct, in the sense that "correct" does not inhibit copy elision guaranteed by C++17. I put comments with what I think is happening, but I'm not sure.
struct Data {
int x;
int y;
};
Data func() {
int X = 1;
int Y = 2;
return {X,Y}; // Case #1: List-initialization. No temporary created. Not elidable? Must evoke copy-constructor to return value?
//return Data({X,Y}); Case #2: Temporary created via list-initialization. Elidable? prvalue referenced to return value?
}

Related

How to handle future enum values in Dart 2.17+

My app is connected to a custom made peripheral and draws some values from it as ints which I want to convert to enums to streamline the app code.
As the hardware evolves overtime, it is of known fact that new values will come.
In my Flutter app, I handle known values (by the time of app writing) through enum extensions, providing an 'unknown' value as follows:
// Battery live data originator protocol
enum BatteryLiveDataProtocol {
unknown, // 0
rs485, // 1
ttl, // 2
canbus, // 3
uart, //4
;
}
extension BatteryLiveDataProtocolExtension on BatteryLiveDataProtocol {
/// Build the enum out of the int number
static BatteryLiveDataProtocol fromNumber(int number) {
switch (number) {
case 1:
return ttl;
case 2:
return rs485;
case 3:
return canbus;
case 0:
default:
return unknown;
}
}
/// Returns the number correpsonding to the enum
int get number {
switch (this) {
case rs485:
return 1;
case ttl:
return 2;
case canbus:
return 3;
case unknown:
default:
return 0;
}
}
As you can see, this code support future version in a quite handily:
const futureValue = 13;
final p = BatteryLiveDataProtocol.fromNumber(futureValue);
Since the introduction of Dart 2.17 (& Flutter 3) I'd like to take advantage of the enhanced enums as follows:
// Battery live data originato protocol
enum BatteryLiveDataProtocol {
unknown(),
rs485(1),
ttl(2),
canbus(3),
uart(4),
;
const BatteryLiveDataProtocol(this.value);
final num value;
}
But then how can I handle future enum values?
I could do that, everywhere I want to convert the int to the enum:
BatteryLiveDataProtocol p;
try {
const futureValue = 13;
p = BatteryLiveDataProtocol(futureValue);
} catch(e) {
p = BatteryLiveDataProtocol.unknown;
}
But this is cumbersome, isn't it? It'll be cool to have the possibility to provide the body of const BatteryLiveDataProtocol(this.value); ourselves.
[UPDATE] Btw, not sure it was clear enough, but it is still possible to create a static function within the enhanced enum as follows:
static BatteryLiveDataProtocol fromValue(int v) {
BatteryLiveDataProtocol p;
try {
p = BatteryLiveDataProtocol[v];
} catch (e) {
p = unknown;
}
return p;
}
And use it as follows:
const futureValue = 13;
final p = BatteryLiveDataProtocol.fromValue(futureValue);
// Makes p being set to "unknown".
So my question #1: is there a better/clever (aka handy) way to handle future values?
Question #2: Others language handle future enum (in Swift for example): is there a way to generate unknown(actualValue) as a dynamic enum value?
So my question #1: is there a better/clever (aka handy) way to handle future values?
I think you can improve your enum by moving the definition of fromNumber and number into the enum itself rather than as an extension.
Additionally I think you can improve the implementations of both fromNumber and number.
For number, enums already have a getter called index which returns the same value as what you want, so you can simply return the index in your number method.
For fromNumber we can turn it into a factory constructor, and since all enums have a static values list (that contains all enum values in the specific order they are defined), we can get the value we want by indexing into the values list. And for any invalid index we would return unknown.
The benefit of these changes to number and fromNumber is that you will no longer need to update these methods when you add another enum value.
The updated enum is as follows:
enum BatteryLiveDataProtocol {
unknown, // 0
rs485, // 1
ttl, // 2
canbus, // 3
uart, //4
;
factory BatteryLiveDataProtocol.fromNumber(int number) =>
number >= values.length || number < 0 ? unknown : values[number];
int get number => index;
}
Question #2: Others language handle future enum (in Swift for example): is there a way to generate unknown(actualValue) as a dynamic enum value?
No, dart's enhanced enums do not work like swift enums, enums in dart are closer to java enums. Swift calls this feature associated values, but dart enums do not have this feature.
Thanks to #mmcdon20 for the factory proposal (and btw, I couldn't replace static by factory in the code below).
But one want to make sure one handle any values: values and index may be different as in the modified example I propose below where the value of unknown is now set to -1 (index is 0).
So the code has to take care of it:
/// Battery live data originato protocol
enum BatteryLiveDataProtocol {
unknown(-1),
rs485(1),
ttl(2),
canbus(3),
uart(4),
;
const BatteryLiveDataProtocol(this.value);
final num value;
factory BatteryLiveDataProtocol.fromValue(int v) =>
values.firstWhere((x) => x.value == v, orElse: () => unknown);
}

How to return two specific types from a generic method?

T getValue<T>(int i) {
if (T == String) return '$i'; // Error
return i; // Error
}
void main() {
var s = getValue<String>(1);
var i = getValue<int>(1);
}
I want getValue to return string if T is String and int otherwise. How to do that?
You can't restrict the type parameter to just int or String, so it will have to accept more than that (at least their least common supertype, Object, so basically any type).
It's not a particularly helpful way to code. It's possible, but not recommended:
T getValue<T>(int i) {
if (i is T) return i;
return "$i" as T;
}
This will return the int if T allows it (so T being any of int, or a super type of int, which is num, Object, dynamic or void, or any number of Comparable<X> wrappings around any any of those supertypes), and otherwise try to return a string. That will fail with a type error unless T is String (since we've already ruled out all supertypes of String).
You can still call it as getValue<bool>(42) and watch it fail, so the type argument doesn't help with correctness.
It's not particularly effective. I'd rather do:
dynamic getValue(int i, {bool toString = false}) {
if (toString) return "$i";
return i;
}
and call it as:
String x = getValue(42, toString: true); // Add `as String` if you disable downcasts.
int y = getValue(42); // And `as int` here.
The type parameter is really just making things harder. You are going to cast or type-check the result anyway, so might as well do it at the call point, rather than introduce type variables that aren't actually preventing misuse anyway.
(I'd probably just do two different functions, but I assume that there is a reason for wanting one function).
As I mentioned in the comments, I don't see any way that you could use your generic as the return type of your getValue function. Even assuming the return under the if statement worked, there is nothing that can be done about trying to return int i when List is passed as the type. You'll be trying to return an int as a List.
If you change it to dynamic, your code will work fine as it's just using the generic as another parameter.
dynamic getValue<T>(int i) {
if (T == String) return '$i';
return i;
}
void main() {
var s = getValue<String>(1);
var i = getValue<int>(1);
}

Fastest way to check Map for duplicate values?

Given a Map, assignment, what is the fastest way to check if it contains any duplicate values in Dart? I am currently using a Set formed from the Map's values and checking its length against the original Map, which works of course, but I'm wondering if there's an especially performant alternative.
Set d = new Set.from(assignment.values);
if (d.length < assignment.length) {
return false; // indicates has duplicates in this context
}
EDIT:
Tried #mezoni's solution modified for my program, but it actually ran a bit slower than my original version. It probably has more to do with constant times than anything else.
List values = new List.from(assignment.values);
Set set = new Set();
for (var i = 0; i < assignment.length; i++) {
if (!set.add(values[i])) {
return false;
}
}
Complexity wise you won't be able to get anything faster. Creating the Set and filling it with the values of the Map is linear in the number of elements. Clearly you have to run through all the values, so you can't do any better than that.
Maybe you could find a solution with a smaller constant factor, but that's not clear. In particular for larger sets I think the Set solution is pretty efficient.
This is more of a algorithms question than a Dart question. In any case, you have to check every value against the others, giving n-1 + n-2 + ... + n-(n-1) checks, or n^2/2. Programmatically, it's easy to create a set, but you could also generate an array, sort the array, and then iterate once to check for duplicates. That finishes in O(n log n).
Fastets way (if you realy need better performance):
void main() {
// Values from map
var values = [1,2,3,2,1,3,2,1];
var length = values.length;
var set = new Set();
var duplicate = false;
// Only for statistics purpose
var statistics = 0;
for(var i = 0; i < length; i++) {
statistics++;
if(!set.add(values[i])) {
duplicate = true;
break;
}
}
print("Duplicate: $duplicate");
print("Performed in ${statistics} iteration(s) from $length possible");
}
Output:
Duplicate: true
Performed in 4 iteration(s) from 8 possible
P.S.
The first example can be recommended to use with List values.
But because Map.values not a List but Iterable then it would be more efficient do not convert them to List but use as is.
Here is modified sample for use with Iterable objects.
It will be work faster because in this algorithm not required convert all values to the List object because it not want using of all elements without exception.
Instead it wants use as less as possible access operation on original source. If the source supports lazy operation of the access to values (as Iterable) this will be even better.
void main() {
// Values from map
var values = [1,2,3,2,1,3,2,1];
var assignment = {};
var length = values.length;
var key = 0;
for(var value in values) {
assignment[key++] = value;
}
var set = new Set();
var duplicate = false;
// Only for statistics purpose
var statistics = 0;
for(var value in assignment.values) {
statistics++;
if(!set.add(value)) {
duplicate = true;
break;
}
}
print("Duplicate: $duplicate");
print("Performed in ${statistics} iteration(s) from $length possible");
}

How to get the length of an array?

How to get the length of a string array like
str 30 name[];//dynamic array
I used the following for getting the length,but it showing the error as "the variable is not of the type CLASS."
int len=name.get_length();
It sounds like you might be happier using the Array collection class.
http://msdn.microsoft.com/en-us/library/array.aspx
static void TestArray(Args _args)
{
Array strArray = new Array(Types::String);
;
strArray.value(1, 'abc');
strArray.value(2, 'def');
info(strfmt("%1", strArray.lastIndex()));
}
You need the dimOf function. Take a look to the reference:
http://msdn.microsoft.com/en-us/library/aa597117.aspx
Sorry, there is no build-in function to return the string array size. Since you are in full control what you put in the array, there need not be any!
The built-in function dimof returns the allocated size of the array, which is only of practical value for a fixed size array like str 30 name[20], where dimof(name) returns 20.
A clean way to remain in control, is to use a setter function:
static void TestArray(Args _args)
{
str 30 name[];
int n = 0;
int i;
void nameSet(int _i, str 30 _name)
{
n = max(n,_i);
name[_i] = _name;
}
;
nameSet(2,'abc');
nameSet(4,'def');
for (i = 1; i <= n; i++)
info(name[i]);
}
There is no upper bound index limit, so accessing name[7] is perfectly valid and in this case returns a blank value. This may be used to your advantage, if you always use all holes and never stores a blank:
static void TestArray(Args _args)
{
str 30 name[];
int i;
name[1] = 'abc';
name[2] = 'def';
for (i = 1; name[i]; i++)
info(name[i]);
}
Beware that accessing a higher index (in this case higher than 2) may in fact increase the allocated size of the array.

Why Array.zeroCreate still fills null for non nullable type?

Does it imply that whenever I am passed an array of a non nullable type, I should still check if it is null? Actually it is not even possible to check <> null but have to use operator.unchecked .How is it better than C#?
type test=
{
value: int
}
let solution = Array.zeroCreate 10
solution.[0] <- {value = 1}
solution.[1].value // System.NullReferenceException: Object reference not set to an instance of an object
type test =
{value: int;}
val solution : test [] =
[|{value = 1;}; null; null; null; null; null; null; null; null; null|]
val it : unit = ()
It depends where the array is being passed from.
If the array is created and used only within F#, then no, you don't need to check for null; in fact, you shouldn't check for null (using Unchecked.defaultOf) because the F# compiler optimizes some special values like [] (and None, in certain cases) by representing them as null in the compiled IL.
If you're consuming an array being passed in by code written in another language (such as C#), then yes, you should still check for null. If the calling code just creates the array and doesn't mutate it any further, then you'll only need to perform the null checks once.
EDIT : Here's a previous discussion about how the F# compiler optimizes the representation of certain values using null: Why is None represented as null?
As the documentation for Array.zeroCreate indicates, it initializes the elements to Unchecked.defaultof<_>. This therefore carries with it all of the same caveats that direct use of Unchecked.defaultof does. Generally, my advice would be to use Array.create/Array.init whenever possible, and to treat Array.zeroCreate as a possible performance optimization (requiring care whenever dealing with non-nullable types).
You're creating a record type, which is implemented as a class, which is indeed nullable. If you intended to create a struct, your code should look something like this:
type test =
struct
val value: int
new(v) = { value = v }
override x.ToString() = x.value.ToString()
end
let solution = Array.zeroCreate 10
solution.[0] <- test(1)
This outputs: val solution : test [] = [|1; 0; 0; 0; 0; 0; 0; 0; 0; 0|]
You could also write the type using the Struct attribute, saving you a level of indentation.
[<Struct>]
type test =
val value: int
new(v) = { value = v }
override x.ToString() = x.value.ToString()

Resources