tuples

Are there any reasons why c++ template packs are passed using std::tuple

时光毁灭记忆、已成空白 提交于 2019-12-12 18:51:39
问题 Let's say we want to create a helper class to reverse template pack e.g. as follows: #include <tuple> #include <utility> #include <typeinfo> #include <iostream> template <class> struct sizer; template <template<class...> class Pack, class... Args> struct sizer<Pack<Args...>> { static constexpr size_t value = sizeof...(Args); }; template <class Pack, class Indices = std::make_index_sequence<sizer<Pack>::value>> struct reverse_pack; template <class... Args, size_t... I> struct reverse_pack<std:

F# : Writing a function that builds a list of tuples recursively, a syntax error

眉间皱痕 提交于 2019-12-12 18:46:50
问题 I'm trying to write a recursive function that return a List < int * int>, but I'm having some trouble with the syntax. The function should return an empty list when the recursione ends, otherwise a tuple (int * int) merged with a List returned by a recursive call to itself: let rec foobar () : List<int * int> = if (recursionIsEnded) then [] else foobar () :: (10, 10) // this is wrong // (10,10) works, but I need to concatenate it with the elements returned by foobar recursive call Could

Python unpack 2-dimensional list of named tuples

老子叫甜甜 提交于 2019-12-12 18:09:07
问题 I have a 2-dimensional list of named tuples (let's say that each tuple has N values), and I want to unpack them into N different 2-dimensional lists where each unpacked 2-D list is composed entirely of a single attribute from the original list. For example if I have this 2-D list: >>> combo = namedtuple('combo', 'i, f, s') >>> combo_mat = [[combo(i + 3*j, float(i + 3*j), str(i + 3*j)) for i in range(3)] for j in range(3)] >>> combo_mat [[combo(i=0, f=0.0, s='0'), combo(i=1, f=1.0, s='1'),

Using itertools to group consecutive tuples by second value

*爱你&永不变心* 提交于 2019-12-12 17:36:01
问题 I have a set of data in the form: X1 = [(1,1),(3,1),(5,0),(3,0),(2,1)] I can't figure out how to group them such that: X2 = [[(1,1),(3,1)],[(5,0),(3,0)],[(2,1)]] i.e. they are grouped in a consecutive fashion by the second value in each tuple. I know it's something with this: http://docs.python.org/2/library/itertools.html#itertools.groupby 回答1: from itertools import groupby from operator import itemgetter X2 = [list(group) for key, group in groupby(X1, itemgetter(1))] Pass a key function to

Remove duplicate tuples after sorting the tuple in R

╄→尐↘猪︶ㄣ 提交于 2019-12-12 16:19:16
问题 I have a question regarding removing duplicates after sorting within a tuple in R. Let's say I have a dataframe of values df<-cbind(c(1,2,7,8,5,1),c(5,6,3,4,1,8),c(1.2,1,-.5,5,1.2,1)) a and b a=df[,1] b=df[,2] temp<-cbind(a,b) What I am doing is uniquing based upon a sorted tuple. For example, I want to keep a=1,2,7,8,1 and b=5,6,3,4,8 with the entry a[5] and b[5] removed. This is basically for determining interactions between two objects. 1 vs 5, 2 vs 6 etc. but 5 vs 1 is the same as 1 vs 5,

How to compare tuples of different length?

对着背影说爱祢 提交于 2019-12-12 16:11:01
问题 I would like to write a comparator which compares tuples of different length but have the same "prefix". Consider following case, I have two tuples. auto t1 = std::make_tuple(10, "Test1"); auto t2 = std::make_tuple(10, "Test", 3.14); I would like to apply "less" for t1 < t2, where only two first members of tuple are compared (same type?) and the third one is just omited. Is it possible? 回答1: Well, since no one has chimed in, here is the solution. It uses C++14 std::index_sequence , so the

Type-safe way to divide a tuple into multiple tuples

这一生的挚爱 提交于 2019-12-12 14:37:44
问题 We have a trait that among other things contains an execute[T <: Record](Seq[(Session) => T]): Seq[T] method, where Record is the supertrait of all traits that we're retrieving from the database trait DbTrait { val threadCount: Int val db: Database protected def getGroupSize(size: Int, threadCount: Int) { (size / threadCount) + if(size % threadCount == 0) 0 else 1 } def execute[T <: Record](funs: Seq[(Session) => T]): Seq[T] } trait DbTraitSingle extends DbTrait { val threadCount = 1 def

Naturally sort a list of alpha-numeric tuples by the tuple's first element in Python

a 夏天 提交于 2019-12-12 12:23:40
问题 A previous stackoverflow question explains how to sort a list of strings alpha-numerically. I would like to sort a list of tuples alphanumerically by the tuple's first element. Example 1: >>> sort_naturally_tuple([('b', 0), ('0', 1), ('a', 2)]) [('0', 1), ('a', 2), ('b', 0)] Example 2: >>> sort_naturally_tuple([('b10', 0), ('0', 1), ('b9', 2)]) [('0', 1), ('b9', 2), ('b10', 0)] Update: To emphasize the alphanumeric factor, please review example 2. 回答1: Using the second answer from the other

CUDA Thrust sort_by_key when the key is a tuple dealt with by zip_iterator's with custom comparison predicate

六月ゝ 毕业季﹏ 提交于 2019-12-12 11:28:49
问题 I've looked through a lot of questions here for something similar and there are quite a few, albeit with one minor change. I'm trying to sort values with a zip_iterator as a compound key. Specifically, I have the following function: void thrustSort( unsigned int * primaryKey, float * secondaryKey, unsigned int * values, unsigned int numberOfPoints) { thrust::device_ptr dev_ptr_pkey = thrust::device_pointer_cast(primaryKey); thrust::device_ptr dev_ptr_skey = thrust::device_pointer_cast

Time complexity of casting lists to tuples in python and vice versa

假如想象 提交于 2019-12-12 11:22:26
问题 what is the time complexity of converting a python list to tuple (and vice versa): tuple([1,2,3,4,5,6,42]) list((10,9,8,7,6,5,4,3,1)) O(N) or O(1), i.e. does the list get copied or is something somewhere internally switched from writable to read-only? Thanks a lot! 回答1: It is an O(N) operation, tuple(list) simply copies the objects from the list to the tuple. SO, you can still modify the internal objects(if they are mutable) but you can't add new items to the tuple. Copying a list takes O(N)