MATLAB linked list

MATLAB has access to Java: >> a=java.util.LinkedList; >> li=a.listIterator; >> li.add(2); >> li.add(int8(77)); >> li.add(77); >> li.add(boolean(true)); >> li.add(‘Mr. Bill’); >> li.previous(); >> li.add([1 2 3 4 5]); >> a a = [2.0, 77, 77.0, true, [D@66a917, Mr. Bill] >> a.get(4) ans = 1 2 3 4 5 The one downside of this approach is … Read more

Does java have a “LinkedConcurrentHashMap” data structure?

You can wrap the map in a Collections.synchronizedMap to get a synchronized hashmap that maintains insertion order. This is not as efficient as a ConcurrentHashMap (and doesn’t implement the extra interface methods of ConcurrentMap) but it does get you the (somewhat) thread safe behavior. Even the mighty Google Collections doesn’t appear to have solved this … Read more

How could I speed up my written python code: spheres contact detection (collision) using spatial searching

UPDATE: this post answered is now superseded by this new one (which take into account the updates of the question) providing an even faster code based on a different approach. Step 1: better algorithm First of all, building a k-d tree runs in O(n log n) time and doing a query runs in O(log n) … Read more

Order Statistic Tree in C++

Here is the example of GNU Policy-Based STL MAP implemented as order statistic tree (tested on Linux gcc 4.6.1): #include <iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree< int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> map_t; int main() { map_t s; s.insert(make_pair(12, 1012)); s.insert(make_pair(505, 1505)); s.insert(make_pair(30, 1030)); cout << s.find_by_order(1)->second << ‘\n’; … Read more

How to sort a list of dictionaries by a value of the dictionary in Python?

The sorted() function takes a key= parameter newlist = sorted(list_to_be_sorted, key=lambda d: d[‘name’]) Alternatively, you can use operator.itemgetter instead of defining the function yourself from operator import itemgetter newlist = sorted(list_to_be_sorted, key=itemgetter(‘name’)) For completeness, add reverse=True to sort in descending order newlist = sorted(list_to_be_sorted, key=itemgetter(‘name’), reverse=True)