标签: conditional-statements

如何使用WiX中的功能条件?

我试图使简单的Windows intaller,我不知道如何处理这个.我有两个功能 - feature1和feature2.我希望仅在用户选择要安装的feature1时才安装feature2.所以我尝试过:

<Feature Id='core' Title='Core'
         Description='ØMQ 1.0.0 core functionality and C++ API' Level='1'>
  <ComponentRef Id='Core_include' />
  <ComponentRef Id='Core_bin' />
  <ComponentRef Id='Core_lib' />
  <ComponentRef Id='Core_zmq' />
  <ComponentRef Id='cpp_bin' />
</Feature>

<Feature Id='core_perf' Title='core_perf' Description='0MQ core perf' Level='999'>
    <Condition Level="0">NOT (&amp;core = "3")</Condition>
        <ComponentRef Id='cpp_perf' />
</Feature>
Run Code Online (Sandbox Code Playgroud)

但是,如果用户选择功能核心,则不会安装功能core_perf.

我怎样才能解决这个问题?

installer windows-installer wix conditional-statements

16
推荐指数
2
解决办法
4万
查看次数

Perl Switch声明

如果没有匹配的案例块,有没有办法运行代码块?例如:

switch($a) {

  case // {}
  case // {}
  ...
  # DO SOMETHING IF NONE OF THE ABOVE CASES WERE MATCHED
}
Run Code Online (Sandbox Code Playgroud)

else 不是我想要的,因为它只适用于最后一个案例块.

perl case switch-statement conditional-statements

16
推荐指数
4
解决办法
5万
查看次数

Python Pandas Select index其中index大于x

假设我有一个DataFrame df,日期作为索引和一些值.如何选择日期大于某个值的行x

我知道我可以将索引转换为列然后执行select df[df['date']>x],但是比在索引上执行操作要慢吗?

python indexing conditional-statements pandas

16
推荐指数
2
解决办法
3万
查看次数

整数上的无分支条件 - 速度快,但它们可以更快吗?

我一直在尝试以下内容,并注意到这里定义的无分支"if"(现在有&-!!替换*!!)可以使用clang在64位Intel目标上加速某些瓶颈代码(几乎)2倍:

// Produces x if f is true, else 0 if f is false.
#define  BRANCHLESS_IF(f,x)          ((x) & -((typeof(x))!!(f)))

// Produces x if f is true, else y if f is false.
#define  BRANCHLESS_IF_ELSE(f,x,y)  (((x) & -((typeof(x))!!(f))) | \
                                     ((y) & -((typeof(y)) !(f))))
Run Code Online (Sandbox Code Playgroud)

请注意,f应该是一个没有副作用的相当简单的表达式,以便编译器能够进行最佳的优化.

性能高度依赖于CPU和编译器.clang的无分支'if'表现非常出色; 我还没有找到任何无分支的'if/else'更快的情况.

我的问题是:这些是安全的,可移植的吗(意味着可以保证在所有目标上得到正确的结果),并且可以更快地制作吗?

无分支if/else的示例用法

这些计算64位最小值和最大值.

inline uint64_t uint64_min(uint64_t a, uint64_t b)
{
  return BRANCHLESS_IF_ELSE((a <= b), a, b);
}

inline uint64_t uint64_max(uint64_t a, uint64_t b)
{
  return BRANCHLESS_IF_ELSE((a >= b), …
Run Code Online (Sandbox Code Playgroud)

c macros conditional-statements branch-prediction c11

16
推荐指数
1
解决办法
885
查看次数

将一个线程置于休眠状态,直到另一个线程中的条件得到解决

以下是两个完成(我认为)同样事情的代码块.

我基本上是在尝试学习如何使用Java 1.5的并发来摆脱Thread.sleep(long).第一个示例使用ReentrantLock,第二个示例使用CountDownLatch.我想要做的就是让一个线程进入休眠状态,直到另一个线程中的条件得到解决.

ReentrantLock提供了一个布尔锁定,我用来决定是否唤醒另一个线程,然后我使用条件和等待/信号来休眠另一个线程.据我所知,我需要使用锁的唯一原因是,如果多个线程需要对boolean进行写访问.

CountDownLatch似乎提供与ReentrantLock相同的功能,但没有(不必要的?)锁.然而,感觉就像我通过初始化它只需要一次倒计时来劫持它的预期用途.我认为它应该在多个线程要处理同一个任务时使用,而不是在多个线程在等待一个任务时使用.

所以,问题:

  1. 我在ReentrantLock代码中使用锁定"正确的东西"吗?如果我只在一个线程中写入布尔值,那么锁是否必要?只要我在唤醒任何其他线程之前重置布尔值我不会导致问题,我可以吗?

  2. 是否有一个类似于CountDownLatch的类,我可以使用它来避免锁定(假设我应该在这个实例中避免它们),这更适合这个任务?

  3. 有没有其他方法来改进我应该注意的代码?

例1:

import java.util.concurrent.locks.*;

public class ReentrantLockExample extends Thread {

//boolean - Is the service down?
boolean serviceDown;

// I am using this lock to synchronize access to sDown
Lock serviceLock; 
// and this condition to sleep any threads waiting on the service.
Condition serviceCondition;

public static void main(String[] args) {
    Lock l = new ReentrantLock();
    Condition c = l.newCondition(); 
    ReentrantLockExample rle = new ReentrantLockExample(l, c);

    //Imagine this thread figures out the …
Run Code Online (Sandbox Code Playgroud)

java concurrency locking countdownlatch conditional-statements

15
推荐指数
1
解决办法
1万
查看次数

如何在管道内使用"if"语句

我正试图if在管道内使用.

我知道有where(别名?)过滤器,但是如果我想只在满足某个条件时才激活过滤器怎么办?

我的意思是,例如:

get-something | ? {$_.someone -eq 'somespecific'} | format-table

如何if在管道内使用来打开/关闭过滤器?可能吗?是否有意义?

谢谢

编辑澄清

没有管道,它看起来像这样:

if($filter) {
 get-something | ? {$_.someone -eq 'somespecific'}
}
else {
 get-something
}

在ANSWER的riknik之后编辑

愚蠢的例子显示了我在寻找什么.您有一个存储在变量上的非规范化数据表,$data并且您希望执行一种"向下钻取"数据过滤:

function datafilter {
param([switch]$ancestor,
    [switch]$parent,
    [switch]$child,
    [string]$myancestor,
    [string]$myparent,
    [string]$mychild,
    [array]$data=[])

$data |
? { (!$ancestor) -or ($_.ancestor -match $myancestor) } |
? { (!$parent) -or ($_.parent -match $myparent) } |
? { (!$child) -or ($_.child -match $mychild) } |

} …

powershell pipeline statements conditional-statements

15
推荐指数
1
解决办法
3万
查看次数

用中值替换numpy数组中的零

我有一个像这样的numpy数组:

foo_array = [38,26,14,55,31,0,15,8,0,0,0,18,40,27,3,19,0,49,29,21,5,38,29,17,16]
Run Code Online (Sandbox Code Playgroud)

我想用整个数组的中值替换所有零(其中零值不包括在中位数的计算中)

到目前为止,我有这样的事情:

foo_array = [38,26,14,55,31,0,15,8,0,0,0,18,40,27,3,19,0,49,29,21,5,38,29,17,16]
foo = np.array(foo_array)
foo = np.sort(foo)
print "foo sorted:",foo
#foo sorted: [ 0  0  0  0  0  3  5  8 14 15 16 17 18 19 21 26 27 29 29 31 38 38 40 49 55]
nonzero_values = foo[0::] > 0
nz_values = foo[nonzero_values]
print "nonzero_values?:",nz_values
#nonzero_values?: [ 3  5  8 14 15 16 17 18 19 21 26 27 29 29 31 38 38 40 49 55]
size = …
Run Code Online (Sandbox Code Playgroud)

python arrays replace numpy conditional-statements

15
推荐指数
2
解决办法
4万
查看次数

R重复功能直到满足条件

我正在尝试生成一个排除某些"不良数据"的随机样本.在我对其进行采样之前,我不知道数据是否"糟糕".因此,我需要从人群中随机抽取然后进行测试.如果数据"好",那么保留它.如果数据"不好",则随机抽取另一个并测试它.我想这样做,直到我的样本大小达到25.下面是我尝试编写一个函数的简化示例.谁能告诉我我错过了什么?

df <- data.frame(NAME=c(rep('Frank',10),rep('Mary',10)), SCORE=rnorm(20))
df

random.sample <- function(x) {
  x <- df[sample(nrow(df), 1), ]
  if (x$SCORE > 0) return(x)
 #if (x$SCORE <= 0) run the function again
}

random.sample(df)
Run Code Online (Sandbox Code Playgroud)

r function repeat conditional-statements

15
推荐指数
1
解决办法
4万
查看次数

使用CASE条件和SUM()的SELECT查询

我目前正在使用这些sql语句.我的表有CPaymentType字段,其中包含"Cash"或"Check".我可以通过执行2个SQL语句来总结支付金额,如下所示.在这种情况下,用户甚至不会注意到执行2个sql语句时的速度差异或只是1,但是,我不喜欢我的方式,我只想要1个sql语句.如何使用CASE条件将这些重构为1个语句?我无法弄明白,因为在线示例导致1或0或布尔值.我不希望包含过期的支票付款.非常感谢你.

Select SUM(CAmount) as PaymentAmount 
from TableOrderPayment 
where CPaymentType='Cash' and CStatus='Active';

Select SUM(CAmount) as PaymentAmount 
from TableOrderPayment 
where CPaymentType='Check' and CDate<=SYSDATETIME() and CStatus='Active';
Run Code Online (Sandbox Code Playgroud)

sql sql-server sum case conditional-statements

15
推荐指数
3
解决办法
9万
查看次数

在COUNTIFS中使用OR&AND

我想"AND"在我的COUNTIFS条款中包含一个条件.

像这样的东西:

=COUNTIFS(A1:A196;{"Yes"or "NO"};J1:J196;"Agree")
Run Code Online (Sandbox Code Playgroud)

所以,它应该返回行数:

 (A1:A196 is either "yes" or "no") AND (J1:j196 is "agree")
Run Code Online (Sandbox Code Playgroud)

excel formula conditional-statements

15
推荐指数
1
解决办法
20万
查看次数