std::unique analogue in Qt? - qt

I have browsed the documentation, but didn't find one.
Any ideas?

You should be able to just apply std::unique to the iterators of QList. std::unique just requires the iterators to be forward iterators (here and here), and it appears that QList's iterators meet that requirement.

Consider using a QSet instead (and use QSet::toList when you need it as a list).

This is how I created my unique list of integers:
list=QSet::fromList(list).toList();
No need to add STD, it might not be the best way using very large integers though

By now I have the following:
//filter out duplicates: stl algorithm 'unique' would be useful here
QList<int> uniqueIDs;
qStableSort(res);
foreach(int id, res)
if ( (uniqueIDs.empty())
|| (uniqueIDs.back() != id))
uniqueIDs.push_back(id);
swap(res, uniqueIDs);
res being the input for filtering, and not pleased with it.

Related

Is there a Rust function which counts frequencies in a Vec?

I want to count the frequencies of all elements in a given Vec, e.g. something like
count_frequencies(vec![1,1,1,4,1,2,3,5,4])
should return the following Vec:
[(1,4), (2,1), (3,1), (4,2), (5,1)]
(the order does not matter). While I know how I could implement such a function, it seems to me like there should already be an existing implementation in some crate. After some googling, I only found a crate named frequency, but didn't find any example in the documentation.
So, my question is: is there a crate that can achieve this task and, if so, how can I use it?
Edit: If you as well know a function which goes in the other direction, I would also be interested in that :)
Itertools offers counts, but you'd have to convert the result (a HashMap) into a Vec yourself.
Alternatively, sort the vector (costs O(n log n), but may - in practice - be faster than a HashMap-based approach) and use dedup_with_count on the sorted vector.
It isn't really needed because it is one-liner anyway:
let frequencies = v
.iter()
.copied()
.fold(HashMap::new(), |mut map, val|{
map.entry(val)
.and_modify(|frq|*frq+=1)
.or_insert(1);
map
});
There are different requirements for different tasks so there is no need to make some standard method for this.

values and keys guaranteed to be in the consistent order?

When applied to a Dict, will values(...) and keys(...) return items in matching order?
In other words, is zip(keys(d), values(d)) guaranteed to contain precisely the key-value pairs of the dictionary d?
Option 1
The current Julia source code indicates that the keys and vals of a Dict() object are stored as Array objects, which are ordered. Thus, you could just use values() and keys() separately, as in your question formulation. But, it is dangerous to rely on under the hood implementation details that aren't documented, since they might be changed without notice.
Option 2
An OrderedDict from the DataStructures package (along with the functions values() and keys()) is probably the simplest and safest way to be certain of consistent ordering. It's ok if you don't specifically need the ordering.
Option 3
If you don't want to deal with the added hassle of installing and loading the DataStructures package, you could just use Julia's built in syntax for handling this kind of thing, e.g.
Mydict = Dict("a" => 1, "b" => 2, "c" => 1)
a = [(key, val) for (key, val) in Mydict]
The use of zip() as given in the question formulation just adds complexity and risk in this situation.
If you want the entities separate, you could then use:
Keys = [key for (key, val) in Mydict]
Values = [val for (key, val) in Mydict]
or just refer to a[idx][1] for the idx element of Keys when you need it.
Currently your assertion seems to be true:
julia> let
d = [i => i^2 for i in 1:10_000]
z = zip(keys(d), values(d))
for (pair, tupl) in zip(d, z)
#assert pair[1] == tupl[1] && pair[2] == tupl[2]
end
info("Success")
end
INFO: Success
But that is an undocumented implementation detail as Michael Ohlrogge explains.
Stefan Karpinski comment about show(dict) now sorted by key in #16743:
This has performance implications for printing very large Dicts. I don't think it's a good idea. I do, however, think that making Dict ordered is a good idea that we should go ahead with.
See also:
#10116 WIP: try ordered Dict representation.
Most importantly, what are you trying to do? Perhaps an OrederedDict is what you need?
Yes, keys and values return items in matching order. Unless, as Dan Getz pointed out above, the dictionary is modified in between using the two iterators.
I think it would be relatively perverse for a dictionary not to have this behavior. It was obvious to us that the order should match, to the point that it didn't even occur to us to mention this explicitly in the documentation.
Another way to ensure corresponding order between keys and values is using imap from the Iterators package in the following way:
using Iterators
d = Dict(1=>'a',2=>'b',3=>'c')
# keys iterator is `imap(first,d)`
julia> collect(imap(first,d))
3-element Array{Any,1}:
2
3
1
# values iterator is `imap(last,d)`
julia> collect(imap(last,d))
3-element Array{Any,1}:
'b'
'c'
'a'
This method can potentially be adapted for other structures. All the other comments and answers are also good.

Comparing a c++ std::vector's elements with each other

I have a std::vector of double values. Now I need to know if two succeeding elements are within a certain distance in order to process them. I have sorted the vector previously with std::sort.
I have thought about a solution to determine these elements with std::find_if and a lambda expression (c++11), like this:
std::vector<std::vector<double>::iterator> foundElements;
std::vector<double>::iterator foundElement;
while (foundElement != gradients.end())
{
foundElement = std::find_if(gradients.begin(), gradients.end(),
[] (double grad)->bool{
return ...
});
foundElements.push_back(foundElement);
}
But what should the predicate actually return? The plan is that I use the vector of iterators to later modify the vector.
Am I on the right track with this approach or is it too complicated/impossible? What are other, propably more practical solutions?
EDIT: I think I will go after the std::adjacent_find function, as proposed by one answerer.
Read about std::adjacent_find.
Can you enhance the grammar of your question?
"I have a std::vector with different double values. Now I need to know if two succeeding ones (I have sorted the vector previously with std::sort) are within a certain distance to process them)."
Do you imply that each element of a vector of type double is a unique value? If that's the case, can it be reasonably inferred that your goal to find the distance between each of these elements?

Mathematica Map question

Original question:
I know Mathematica has a built in map(f, x), but what does this function look like? I know you need to look at every element in the list.
Any help or suggestions?
Edit (by Jefromi, pieced together from Mike's comments):
I am working on a program what needs to move through a list like the Map, but I am not allowed to use it. I'm not allowed to use Table either; I need to move through the list without help of another function. I'm working on a recursive version, I have an empty list one down, but moving through a list with items in it is not working out. Here is my first case: newMap[#, {}] = {} (the map of an empty list is just an empty list)
I posted a recursive solution but then decided to delete it, since from the comments this sounds like a homework problem, and I'm normally a teach-to-fish person.
You're on the way to a recursive solution with your definition newMap[f_, {}] := {}.
Mathematica's pattern-matching is your friend. Consider how you might implement the definition for newMap[f_, {e_}], and from there, newMap[f_, {e_, rest___}].
One last hint: once you can define that last function, you don't actually need the case for {e_}.
UPDATE:
Based on your comments, maybe this example will help you see how to apply an arbitrary function:
func[a_, b_] := a[b]
In[4]:= func[Abs, x]
Out[4]= Abs[x]
SOLUTION
Since the OP caught a fish, so to speak, (congrats!) here are two recursive solutions, to satisfy the curiosity of any onlookers. This first one is probably what I would consider "idiomatic" Mathematica:
map1[f_, {}] := {}
map1[f_, {e_, rest___}] := {f[e], Sequence##map1[f,{rest}]}
Here is the approach that does not leverage pattern matching quite as much, which is basically what the OP ended up with:
map2[f_, {}] := {}
map2[f_, lis_] := {f[First[lis]], Sequence##map2[f, Rest[lis]]}
The {f[e], Sequence##map[f,{rest}]} part can be expressed in a variety of equivalent ways, for example:
Prepend[map[f, {rest}], f[e]]
Join[{f[e]}, map[f, {rest}] (#Mike used this method)
Flatten[{{f[e]}, map[f, {rest}]}, 1]
I'll leave it to the reader to think of any more, and to ponder the performance implications of most of those =)
Finally, for fun, here's a procedural version, even though writing it made me a little nauseous: ;-)
map3[f_, lis_] :=
(* copy lis since it is read-only *)
Module[{ret = lis, i},
For[i = 1, i <= Length[lis], i++,
ret[[i]] = f[lis[[i]]]
];
ret
]
To answer the question you posed in the comments, the first argument in Map is a function that accepts a single argument. This can be a pure function, or the name of a function that already only accepts a single argument like
In[1]:=f[x_]:= x + 2
Map[f, {1,2,3}]
Out[1]:={3,4,5}
As to how to replace Map with a recursive function of your own devising ... Following Jefromi's example, I'm not going to give to much away, as this is homework. But, you'll obviously need some way of operating on a piece of the list while keeping the rest of the list intact for the recursive part of you map function. As he said, Part is a good starting place, but I'd look at some of the other functions it references and see if they are more useful, like First and Rest. Also, I can see where Flatten would be useful. Finally, you'll need a way to end the recursion, so learning how to constrain patterns may be useful. Incidentally, this can be done in one or two lines depending on if you create a second definition for your map (the easier way), or not.
Hint: Now that you have your end condition, you need to answer three questions:
how do I extract a single element from my list,
how do I reference the remaining elements of the list, and
how do I put it back together?
It helps to think of a single step in the process, and what do you need to accomplish in that step.

Multidimensional vectors in Scheme?

I earlier asked a question about arrays in scheme (turns out they're called vectors but are basically otherwise the same as you'd expect).
Is there an easy way to do multidimensional arrays vectors in PLT Scheme though? For my purposes I'd like to have a procedure called make-multid-vector or something.
By the way if this doesn't already exist, I don't need a full code example of how to implement it. If I have to roll this myself I'd appreciate some general direction though. The way I'd probably do it is to just iterate through each element of the currently highest dimension of the vector to add another dimension, but I can see that being a bit ugly using scheme's recursive setup.
Also, this seems like something I should have been able to find myself so please know that I did actually google it and nothing came up.
The two common approaches are the same as in many languages, either use a vector of vectors, or (more efficiently) use a single vector of X*Y and compute the location of each reference. But there is a library that does that -- look in the docs for srfi/25, which you can get with (require srfi/25).

Resources