What is the maximum enum flag value I can use here? - visual-c++

Sorry if this has been asked before. I have always struggled with the concept of flags, even those I use them on occasion.
Take this enum:
enum ImportAssignment
{
None = 0,
OCLMHost = 1 << 0,
OCLMCohost = 1 << 1,
OCLMZoomAttendant = 1 << 2,
OCLMChairman = 1 << 3,
OCLMOpenPrayer = 1 << 4,
OCLMClosePrayer = 1 << 5,
OCLMConductorCBS = 1 << 6,
OCLMReaderCBS = 1 << 7,
PTHost = 1 << 8,
PTCohost = 1 << 9,
PTZoomAttendant = 1 << 10,
PTChairman = 1 << 11,
PTHospitality = 1 << 12,
WTConductor = 1 << 13,
WTReader = 1 << 14,
PTSpeaker = 1 << 15,
PTTheme = 1 << 16
};
What would be the largest value I can use here? As in 1 << nn? What maximum value and nn be and why is it that value?
The suggested duplicate:
What is the underlying type of a c++ enum?
Appears to only explain that the underlying variable type of an enum is an int. I already know this. But I still don't really know how large the nn value can be and I do not see how the linked question addresses that.

The logical bitwise shift to the left moves 1 in your example one bit to the left.
| Operation | Binary | Decimal |
|:--------- |:------:| -------:|
| 1<<0 | 0001 | 1 |
| 1<<1 | 0010 | 2 |
| 1<<2 | 0100 | 4 |
| 1<<3 | 1000 | 8 |
| 1<<4 | 10000 | 10 |
Therefore, the biggest nn number would be 63 for the int64 on your platform, 31 if the int is int32.

Related

Google Sheets Divide value if other column not empty

I have a sheet with the following values in minutes to keep track of how long it takes for x task:
+---------+----------+----------+
| A | B | C |
+---------+----------+----------+
| Task | Person 1 | Person 2 |
| Task #1 | 10 | 20 |
| Task #2 | 20 | 0 |
| Task #3 | 0 | 30 |
+---------+----------+----------+
I want to get total of hours for every task but if a task has time for Person 1 and Person 2, I want the average of the time taken for the task.
What I've had in mind previously is:
=(SUM(Tasks!$B$2:$B) + SUM(Tasks!$C$2:$C)) / 2
I thought this would work but then I realized it won't because some tasks are only handled by Person 1.
The total of the previous formula would give me 40, but what I'm expecting as a value should be 65, based on the following calculation:
(10 + 20) / 2 + 20 + 30
=SUMPRODUCT((B2:B4+C2:C4)/((B2:B4>0)+(C2:C4>0)))
Use this array formula:
=SUM(IF((B2:B4>0)*(C2:C4>0),(B2:B4+C2:C4)/2,B2:B4+C2:C4))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

How many times a specific character appears after each other in a string

I’ve been looking for a way to count how many times a specific character appears after each other in a string. All the ways I found just counts how many times character “A” appears in the string in total.
Example of string:
0xAAABBC0123456789AABBCCDD0123456789ABCDEF
Each string is 43 characters long and starts with “0x”. Each string only contains the following characters in random order: 0-9 and A-F, (total amount of 16 different characters). Each character can appear after each other in a row several times, example: “AAA” or "111".
I’m interested in how many times each of the 16 characters maximum appears after each other in one string, and check this through all my strings.
So far I’ve only come up with this Powershell script that counts how many times each character appears per line:
Get-Content " C:\Temp\strings.txt" | ForEach-Object{
New-Object PSObject -Property #{
Strings = $_
Row = $_.ReadCount
9 = [regex]::matches($_,"9").count
D = [regex]::matches($_,"D").count
B = [regex]::matches($_,"B").count
C = [regex]::matches($_,"C").count
7 = [regex]::matches($_,"7").count
3 = [regex]::matches($_,"3").count
1 = [regex]::matches($_,"1").count
8 = [regex]::matches($_,"8").count
F = [regex]::matches($_,"F").count
2 = [regex]::matches($_,"2").count
4 = [regex]::matches($_,"4").count
E = [regex]::matches($_,"E").count
6 = [regex]::matches($_,"6").count
5 = [regex]::matches($_,"5").count
A = [regex]::matches($_,"A").count
0 = [regex]::matches($_,"0").count
}
} | Sort Count -Descending | Export-Csv -Path "C:\Temp\output.csv" –NoTypeInformation
I would preferably do this in Powershell, but if there’s another way of doing this more easily, please let me know.
You could use a lookbehind and a backreference to split the string into repeating groups:
$s = '0xAAABBC0123456789AABBCCDD0123456789ABCDEF'
$repeats = $s.Remove(0, 2) -split '(?<=(.))(?!\1|$)'
Now we can group the substring based on the first letter of each:
$groups = $repeats |Group-Object {$_[0]} -AsHashTable
And finally grab the longest sequence of each character:
'0123456789ABCDEF'.ToCharArray() |%{
[pscustomobject]#{
Character = "$_"
MaxLength = "$($groups[$_] |Sort Length -Descending |Select -First 1)".Length
}
}
And you should end up with a list (for your example) like this:
Character MaxLength
--------- ---------
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
A 3
B 2
C 2
D 2
E 1
F 1
One approach is to iterate the source string character by character and keep track on how many times the character has been seen. This is easily done with a hash table. Like so,
# Hashtable initialization. Add keys for 0-9A-F:
# Each char has initial count 0
$ht = #{}
"ABCDEF0123456789".ToCharArray() | % {
$ht.Add($($_.ToString()), 0)
}
# Test data, the 0x prefix will contain one extra zero
$s = "0xAAABBC0123456789AABBCCDD0123456789ABCDEF"
# Convert data to char array for iteration
# Increment value in hashtable by using the char as key
$s.ToCharArray() | % { $ht[$_.ToString()]+=1 }
# Check results
PS C:\> $ht
Name Value
---- -----
B 5
3 2
5 2
x 1
9 2
2 2
8 2
0 3
1 2
E 1
7 2
F 1
6 2
4 2
D 3
A 6
C 4
Build a HexPair iterating the string position for position (omitting the last) and increment a value in a hash table with the HexPair as the key.
$String = '0xAAABBC0123456789AABBCCDD0123456789ABCDEF'
$Hash=#{}
for ($i=2;$i -le ($string.length-2);$i++){
$Hash[$($String.Substring($i,2))]+=1
}
$Hash.GetEnumerator()|ForEach-Object{
[PSCustomObject]#{HexPair = $_.Name
Count = $_.Value}
} |Sort Count -Descending
Sample output
HexPair Count
------- -----
BC 3
AB 3
AA 3
CD 2
BB 2
9A 2
89 2
78 2
67 2
56 2
45 2
34 2
23 2
12 2
01 2
EF 1
DE 1
DD 1
D0 1
CC 1
C0 1
Alternative output:
$Hash.GetEnumerator()|ForEach-Object{
[PSCustomObject]#{HexPair = $_.Name
Count = $_.Value}
} |Sort HexPair|group Count |%{"Count {0} {1}" -f $_.Name,($_.Group.HexPair -Join(', '))}|Sort
Count 1 C0, CC, D0, DD, DE, EF
Count 2 01, 12, 23, 34, 45, 56, 67, 78, 89, 9A, BB, CD
Count 3 AA, AB, BC
The result came out this way, even though it gives me 15 extra rows per string, I can easily filter unwanted material out in Microsoft Excel.
#Removed all "0x" in textfile before running this script
$strings = Get-Content " C:\Temp\strings_without_0x.txt"
foreach($s in $strings) {
$repeats = $s.Remove(0, 2) -split '(?<=(.))(?!\1|$)'
$groups = $repeats |Group-Object {$_[0]} -AsHashTable
'0123456789ABCDEF'.ToCharArray() |%{
[pscustomobject]#{
String = "$s"
Character = "$_"
MaxLength = "$($groups[$_] |Sort Length -Descending |Select -First 1)".Length
}
} | Sort Count -Descending | Export-Csv -Path "C:\Temp\output.csv" -NoTypeInformation -Append}
Thank you for all great answers!
Try this.
$out=#()
$string="0xAAABBC0123456789AABBCCDD0123456789ABCDEF"
$out+="Character,Count"
$out+='0123456789ABCDEF'.ToCharArray()|%{"$_," + ($string.split("$_")|Where-object{$_ -eq ""}).count}
ConvertFrom-Csv $out |sort count -Descending
This yields the following:
Character Count
--------- -----
A 3
B 2
0 1
C 1
D 1
F 1
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
E 0
You can put it into a function like this:
function count_dups ($string){
$out=#() # null array
$out+="Character,Count" # header
$out+='0123456789ABCDEF'.ToCharArray()|%{"$_," + ($string.split("$_")|Where-object{$_ -eq ""}).count}
return ConvertFrom-Csv $out | sort count -Descending
}
The biggest part of what I'm doing here is this line.
'0123456789ABCDEF'.ToCharArray()|%{"$_," + (string.split("$_")|Where-object{$_ -eq ""}).count}
I am splitting the string into an array on the the characters fed in from the character array '0123456789ABCDEF'. Then I am counting the empty elements in the array.
I'm only creating the array $out so that the output can be formatted like your example.

Cassandra - How to get the Day of week from the timestamp column?

I have a timestamp column in a Cassandra table, how do i get the day of week from the timestamp column using cql?
There isn't support out of the box for this but
If using the CQL is a must you can have a look at the User Defined Functions:
http://cassandra.apache.org/doc/latest/cql/functions.html
http://www.datastax.com/dev/blog/user-defined-functions-in-cassandra-3-0
http://docs.datastax.com/en//cql/latest/cql/cql_using/useCreateUDF.html
Then you could use something as simple as:
How to determine day of week by passing specific date?
or even something like
Aggregation with Group By date in Spark SQL
And then you have a UDF that gives you day of the week when you are working with a dates.
Maybe this answer will be helpful for some one still looking for an answer in 2022.
You can create an user defined function:
CREATE OR REPLACE FUNCTION DOW(
input_date_string varchar,
date_pattern varchar
)
CALLED ON NULL INPUT
RETURNS int
LANGUAGE java AS
'
int ret = -1;
try {
ret = java.time.LocalDate.parse(input_date_string, java.time.format.DateTimeFormatter.ofPattern(date_pattern))
.getDayOfWeek()
.getValue();
} catch (java.lang.Exception ex) {
// error, do nothing here and -1 will be returned
}
return ret;
';
Test
cqlsh:store> create table testdate(key int PRIMARY KEY , date_string varchar );
... insert some date_strings ...
INSERT INTO testdate (key , date_string ) VALUES ( 9, '2022-11-22');
...
cqlsh:store> select date_string, dow(date_string, 'yyyy-MM-dd') from testdate;
date_string | store.dow(date_string, 'yyyy-MM-dd')
-------------+--------------------------------------
50/11/2022 | -1
2022-11-23 | 3
19/11/2024 | -1
2022-11-21 | 1
19/11/2023 | -1
19/11/20249 | -1
2022-11-20 | 7
50/aa/2022 | -1
2022-11-22 | 2
19/11/2024 | -1
Similar function with timestamp argument
CREATE OR REPLACE FUNCTION DOW_TS(
input_date_time timestamp,
zone_id varchar
)
CALLED ON NULL INPUT
RETURNS int
LANGUAGE java AS
'
int ret = -1;
try {
ret = input_date_time.toInstant().atZone(java.time.ZoneId.of(zone_id)).toOffsetDateTime()
.getDayOfWeek()
.getValue();
} catch (java.lang.Exception ex) {
// error, do nothing here and -1 will be returned
}
return ret;
';
Test
cqlsh:store> select id, dt, dow_ts(dt, 'UTC'), dow_ts(dt,'WHAT') from testdt;
id | dt | store.dow_ts(dt, 'UTC') | store.dow_ts(dt, 'WHAT')
----+---------------------------------+-------------------------+--------------------------
1 | 2022-11-19 14:30:47.420000+0000 | 6 | -1
Above functions had been tested with below cassandra's setup:
INFO [main] 2022-11-19 12:25:47,004 CassandraDaemon.java:632 - JVM vendor/version: OpenJDK 64-Bit Server VM/11.0.17
INFO [main] 2022-11-19 12:25:50,737 StorageService.java:736 - Cassandra version: 4.0.7
INFO [main] 2022-11-19 12:25:50,738 StorageService.java:737 - CQL version: 3.4.5
References:
https://docs.datastax.com/en/dse/6.8/cql/cql/cql_using/useCreateUDF.html
https://cassandra.apache.org/_/quickstart.html
Hint: you should ensure to set "enable_user_defined_functions: true" in /etc/cassandra/cassandra.yaml.
With docker option above (https://cassandra.apache.org/_/quickstart.html), you do a quick hack as below
$ docker run --rm -d --name cassandra --hostname cassandra --network cassandra cassandra
$ docker cp cassandra:/etc/cassandra/cassandra.yaml .
Use your favorite editor to change "enable_user_defined_functions: false" to "enable_user_defined_functions: true" in "$(pwd)"/cassandra.yaml
$ docker run --rm -d --name cassandra --hostname cassandra --network cassandra --mount type=bind,source="$(pwd)"/cassandra.yaml,target=/etc/cassandra/cassandra.yaml cassandra
If you have very old cassandra version, which does not support java8 then maybe below altenative would work (see https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week)
CREATE OR REPLACE FUNCTION DOW_Tomohiko_Sakamoto(
input_date_time timestamp
)
CALLED ON NULL INPUT
RETURNS int
LANGUAGE java AS
'
int y = input_date_time.getYear() + 1900;
int m = input_date_time.getMonth() + 1;
int d = input_date_time.getDate();
int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
if (m < 3) {
y -= 1;
}
int ret = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
if (ret == 0) {
ret = 7;
}
return ret;
';
TEST
cqlsh:store> insert into data(id, dt ) VALUES (2, '2022-11-19 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (3, '2022-11-21 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (4, '2022-11-23 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (5, '2022-11-24 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (7, '2022-11-25 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (8, '2022-11-26 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (9, '2022-11-27 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (10, '2022-11-28 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (11, '2020-02-29 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (12, '2020-02-30 00:00:00+0000');
cqlsh:store> insert into data(id, dt ) VALUES (13, '2020-02-31 00:00:00+0000');
cqlsh:store> select id, dt, dow_ts(dt,'UTC'), DOW_Tomohiko_Sakamoto(dt) from data;
id | dt | store.dow_ts(dt, 'UTC') | store.dow_tomohiko_sakamoto(dt)
----+---------------------------------+-------------------------+---------------------------------
5 | 2022-11-24 00:00:00.000000+0000 | 4 | 4
10 | 2022-11-28 00:00:00.000000+0000 | 1 | 1
13 | 2020-02-29 00:00:00.000000+0000 | 6 | 6
11 | 2020-02-29 00:00:00.000000+0000 | 6 | 6
1 | 2022-11-20 17:43:28.568000+0000 | 7 | 7
8 | 2022-11-26 00:00:00.000000+0000 | 6 | 6
2 | 2022-11-19 00:00:00.000000+0000 | 6 | 6
4 | 2022-11-23 00:00:00.000000+0000 | 3 | 3
7 | 2022-11-25 00:00:00.000000+0000 | 5 | 5
9 | 2022-11-27 00:00:00.000000+0000 | 7 | 7
12 | 2020-02-29 00:00:00.000000+0000 | 6 | 6
3 | 2022-11-21 00:00:00.000000+0000 | 1 | 1

How can I change the scatterplot character in the gnuplot dumb terminal?

In gnuplot's dumb terminal, I can plot multiple series of 2D data like so:
set terminal dumb 79 24
plot "series1.dat", "series2.dat"
Points in series 1 are represented as the 'A' character; points in series 2 as 'B', and so on:
I can set the point type as follows:
plot "series1.dat" pt 1
but all the point types are just A-Z modulo pt (that is, 1 = 'A', 26 = 'Z', 27 = 'A', etc.)
It would be easier to understand my plot if I could set one of these to, say, * or .. How can I set the point in each series to something of my choosing?
In version 5.0, you can do this by specifying the character as the point type
plot "series1.dat" pt "*"
will use the * character as the point.
For example, with the data
1 2
5 8
3 9
we get
10 +-+-----------+-------------+-------------+-------------+-----------+-+
+ + + + + +
| * |
| |
8 +-+ * +-+
| |
| |
| |
6 +-+ +-+
| |
| |
4 +-+ +-+
| |
| |
| |
2 +-+ * +-+
| |
| |
+ + + + + +
0 +-+-----------+-------------+-------------+-------------+-----------+-+
0 2 4 6 8 10
where the * character is used for the point.
This is documented under help points which says ...any single printable character may be given instead of a point type....
You ask:
It would be easier to understand my plot if I could set one of these to, say, * or .
I have gnuplot 4.6 patchlevel 6 (2013), and although #Matthew's pt solution does not work for me, if you merely want one of the two series to be a dot ., you actually can do:
plot "series1.dat", "series2.dat" with dots
Example
series1.dat:
1 1
2 2
series2.dat:
4 6
5 7
7 5
8 5
start gnuplot, then
set term dumb 79, 24
plot "series1.dat", "series2.dat" with dots
result:
7 ++--------+---------+---------+----------.---------+---------+--------++
+ + + + + "series1.dat" A +
| "series2.dat" . |
6 ++ . ++
| |
| |
5 ++ . +.
| |
| |
4 ++ ++
| |
| |
| |
3 ++ ++
| |
| |
2 ++ A ++
| |
+ + + + + + + +
1 A+--------+---------+---------+----------+---------+---------+--------++
1 2 3 4 5 6 7 8
The series1.dat remains the default with lines style
however series2.dat is rendered with dots style which generates . for each point

Multiple-digit numbers getting split by space in NuGram?

I'm seeing some unexpected behavior in the NuGram IDE Eclipse plug-in for ABNF grammar development.
Say I have a rule that reads:
$fifties =
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59
;
The sentence generator comes up with the matches 5 0, 5 1, 5 2, ... I would normally expect 50, 51, 52, and so forth, but according to NuGram's coverage tool these are considered OOG.
Come to find that it will split any multiple-digit number with spaces, unless there's a leading non-number:
1234 -> 1 2 3 4
1234asdf -> 1 2 3 4 asdf
asdf1234 -> asdf1234
1234asdf5678 -> 1 2 3 4 asdf5678
As far as I know, a normal ABNF grammar wouldn't do this. Or am I forgetting something?
This is because NuGram IDE considers digits as individual DTMF tones. I agree that this behaviour should only apply to DTMF grammars and not voice grammars.
You can surround sequences of digits with double quotes, like:
$fifties =
"50" | "51" | "52" | "53" | "54" | "55" | "56" | "57" | "58" | "59"
;
Hope that helps!

Resources