CountOccurence in VL

In our vvvv martix group chat @toyshung asked how to count occurrences in a spread in VL.

there is a simple solution for all primitive types like Float32, Vectors, Matrix and most types that we have imported from .NET or other libraries. it’s as simple as that:

create a dictionary and increment the value for each entry if its found again.

however, if you patch your own classes and records in VL, the dictionary doesn’t exactly know how to compare two instances of your type. it will just check for “reference equality”. that means it looks for the same location in memory, which will be false in most cases. let’s consider a little example record like this:

if you want to compare the actual content of two instances, you have to provide a so called “equality comparer” to the dictionary that checks whether two instances are same, in the way you want to compare them:

importing the IEqualityComparer from .NET and create it as a region is as slick as that:

see the patch for the concrete implementation, needs a recent vvvv alpha:

Count Occurrences VL.zip (15.8 KB)

3 Likes

Many thanks, the custom type example really helped.

My dictionary case is also a composite one: I want my keys to be a pair of Integers each. With the example I am sure on my way to simply wrap the pair in a Record.

Btw, as before in the chat I would suggest to make CustomEqualityComparer part of VL.Core.It’s generic and versatile (and as of vl right now delegates are working better than interfaces.)

the custom equality comparer type should be in the corelib… or do you mean the .NET interface?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.

This will be in the VL CoreLib in latest versions as CountOccurance node in category Collections.

2 Likes