英文:
How do I manipulate CMake lists as sets?
问题
在CMake中,广泛使用列表。有时您有两个项目列表(基本上是字符串),想要考虑它们的交集、差异或并集。就像在这个情况中,这对我来说刚刚出现。
如何生成这样的交集、差异或并集列表?
注意:输出需要没有重复项,输入不需要。
英文:
In CMake, lists are used extensively. Sometimes you have two lists of items (strings, basically), and you want to consider their intersection, difference or union. Like in this case that just came up for me.
How do I produce such intersection, difference or union lists?
Note: The outputs need to have no duplicates, the inputs not really
答案1
得分: 6
假设我们的列表分别存储在变量 S
和 T
中。
对于并集,请写入:
list(APPEND union_list ${S} ${T})
list(REMOVE_DUPLICATES union_list)
对于集合差异,请写入:
list(APPEND S_minus_T ${S})
list(REMOVE_ITEM S_minus_T ${T})
然后,我们使用一些集合恒等式通过对称差异来获取交集:
- S∩T = (S∪T) ∖ (S∆T)
- S∆T = (S∖T) ∪ (T\S)
英文:
Suppose our lists are in variables S
and T
.
For the union, write:
list(APPEND union_list ${S} ${T})
list(REMOVE_DUPLICATES union_list)
For set difference, write:
list(APPEND S_minus_T ${S})
list(REMOVE_ITEM S_minus_T ${T})
And then we use some set identities to obtain the intersection through the symmetric difference:
- S∩T = (S∪T) ∖ (S∆T)
- S∆T = (S∖T) ∪ (T\S)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论