How to set process ID in Linux for a specific program

Actually, there is a way to do this. Since kernel 3.3 with CONFIG_CHECKPOINT_RESTORE set(which is set in most distros), there is /proc/sys/kernel/ns_last_pid which contains last pid generated by kernel. So, if you want to set PID for forked program, you need to perform these actions: Open /proc/sys/kernel/ns_last_pid and get fd flock it with LOCK_EX write … Read more

Python frozenset hashing algorithm / implementation

The problem being solved is that the previous hash algorithm in Lib/sets.py had horrendous performance on datasets that arise in a number of graph algorithms (where nodes are represented as frozensets): # Old-algorithm with bad performance def _compute_hash(self): result = 0 for elt in self: result ^= hash(elt) return result def __hash__(self): if self._hashcode is … Read more

Convert Set to HashMap

Simpler Java-8 solution involving Collectors.toMap: Map<Integer, String> mapFromSet = set.stream() .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); An IllegalStateException will be thrown if duplicate key is encountered.

Element at index in a std::set?

It doesn’t cause a crash, it just doesn’t compile. set doesn’t have access by index. You can get the nth element like this: std::set<int>::iterator it = my_set.begin(); std::advance(it, n); int x = *it; Assuming my_set.size() > n, of course. You should be aware that this operation takes time approximately proportional to n. In C++11 there’s … Read more

Is the std::set iteration order always ascending according to the C++ specification?

Per the C++ standard, iteration over the elements in an std::set proceeds in sorted order as determined by std::less or by the optional comparison predicate template argument. (Also per the C++ standard, insertion, lookup and deletion take at most O(lg n) time, so balanced search trees are currently the only viable implementation choice for std::set, … Read more