Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Digitizer Block: Remove minor code duplication #100

Open
alexxcons opened this issue Jan 27, 2022 · 2 comments
Open

Digitizer Block: Remove minor code duplication #100

alexxcons opened this issue Jan 27, 2022 · 2 comments

Comments

@alexxcons
Copy link
Collaborator

The number of (enabled) AI/DI ports on the Digitizer on different occasions is counted by looping on all AI's / DI's. Here an example:

int num_enabled_ai_channels = 0;
int num_enabled_di_ports = 0;
for (auto i = 0; i < d_ai_channels; i++) {
if (d_channel_settings[i].enabled) {
num_enabled_ai_channels++;
}
}
for (auto i = 0; i < d_ports; i++) {
if (d_port_settings[i].enabled) {
num_enabled_di_ports++;
}
}

Instead, provide a method which returns the number

@wirew0rm
Copy link
Member

the counting functions already exist in the file:

digitizer_block_impl::get_enabled_aichan_count() const

digitizer_block_impl::get_enabled_diport_count() const

@RalphSteinhagen
Copy link
Member

The number of (enabled) AI/DI ports on the Digitizer on different occasions is counted by looping on all AI's / DI's. Here an example:

int num_enabled_ai_channels = 0;
int num_enabled_di_ports = 0;
for (auto i = 0; i < d_ai_channels; i++) {
if (d_channel_settings[i].enabled) {
num_enabled_ai_channels++;
}
}
for (auto i = 0; i < d_ports; i++) {
if (d_port_settings[i].enabled) {
num_enabled_di_ports++;
}
}

Instead, provide a method which returns the number

Your assessment is correct and std::accumulate is your friend, e.g.

auto nEnabled = std::accumulate(V.begin(), V.end(), 0, [](int a, int b){ return pred(b) ? a+b: a; });

The quoted code is pre-C++11 and C-like which we should start modernising ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants