LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Community Nugget 3-12-2007 Implementing sets with variant attributes

Have you ever wondered how to implement a set in LabVIEW? Sets are like arrays with no duplicate values and where order of the elements is ignored. Ordered sets are sets where the elements are sorted in some way. Variant attributes provide a nice and easy way to implement sets of any LabVIEW datatype.

To implement an ordered set of strings, append each string as a new attribute name to a variant using Set Variant Attribute. There cannot be duplicate attribute names as variant attributes, so LabVIEW automatically guarantees that there are no duplicate strings in the set. To get the elements from the set, use Get Variant Attribute but do not wire the name input. This way you get all the attributes in a set as a alphabetically sorted array.


To implement a set of integer numbers do the same thing, but this time flatten the integer to a string. To get the values as an ordered array use Get Variant Attribute, do not wire anything to name input terminal and convert the values to data using Variant To Data. The integer set is not an ordered set (in the way you would expect) as negative numbers follow positive numbers in internal representation.


You can also implement a set of any data type, not just strings or numbers. The same trick of flattening data to string applies not only to numeric data but to any data. 


Last you may sometimes have a need to remove duplicate references. The above method doesn’t work as Variant To Data node doesn’t accept reference data types. However the following piece of code will remove duplicate references.

Tomi

 

Message Edited by Tomi M on 03-12-2007 12:38 PM

Message Edited by Tomi M on 03-12-2007 12:39 PM

--
Tomi Maila
Message 1 of 25
(12,447 Views)
The forum only accepted three images per post, here comes the last images, a set of references.


Message Edited by Tomi M on 03-12-2007 12:40 PM

--
Tomi Maila
Message 2 of 25
(12,436 Views)

Thank you Tomi!

I will have to experiment with this approach to get my head around what you have done. I have read that the variant has been put "on steroids" since I last looked and the operations are supposed to be much faster.

Has anyone ever compared the new variant performance and how it matches up with non-variant approaches?

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 3 of 25
(12,366 Views)
When to use sets

Perhaps I forgot the most important information, when sets are a useful programming concept.  Sets are valuable when ever you have data that is not allowed to have duplicate values.

As an example consider a system where you have multiple data sources. You may want to allow user to select which data sources to log. To avoid a single data source to be logged multiple times in parallel, you can keep track of logged data sources using a set. If the set already contains the specific data source, it won't be added there again. Every unique value can exists in a set only once, duplicate values are not allowed. When you add a duplicate value to the set, the set remains unchanged.
--
Tomi Maila
Message 4 of 25
(12,355 Views)
Hi all,

This was my first nugget but presumably this will not be last. Please comment if you feel this nugget was too easy or too complicated so I can make my next nuggets of appropriate difficulty level.

Tomi
--
Tomi Maila
Message 5 of 25
(12,315 Views)
Hey Tomi,
 
I knew nothing about sets and this actually proves to be very useful for a few of my running applications.  Not only will it save me a great deal of time, but save me from a few very stressful situations.
 
Seeing as how I didn't know much about this nugget it wasn't too easy nor was it too complicated.  I'll say it was just right.. Smiley Wink
 
Look forward to your next nugget! Smiley Happy
0 Kudos
Message 6 of 25
(12,298 Views)


Seeing as how I didn't know much about this nugget it wasn't too easy nor was it too complicated.  I'll say it was just right.. Smiley Wink
 

Thanks Steve 😉

p.s. You may want to upgrade to LV 8.20 or later as the speed of variant attributes has increased by a giant leap. Or that's what they say.

Tomi
--
Tomi Maila
0 Kudos
Message 7 of 25
(12,267 Views)
Great little nugget. It was at the right level for a monday-friday work week Smiley Wink. I liked it.



Joe.
"NOTHING IS EVER EASY"
0 Kudos
Message 8 of 25
(12,256 Views)
Very nice. I haven't thought about that, so I'll need to consider whether I have places where this would be applicable.

___________________
Try to take over the world!
0 Kudos
Message 9 of 25
(12,249 Views)

"Ben" <x@no.email> wrote in message news:1173712213483-490110@exchange.ni.com...
Thank you Tomi!
I will have to experiment with this approach to get my head around what you have done. I have read that the variant has been put "on steroids" since I last looked and the operations are supposed to be much faster.
Has anyone ever compared the new variant performance and how it matches up with non-variant approaches?
Ben



We did a little test recently (we don't have benchmarks though). Getting/searching an item is generally faster then searching an array, but inserting new items is slower then simply attaching to an array. If you search an item at the beginning of an array, the array search can be faster, because the search routine will stop as soon as the item is found.


Also, an array doesn't have to be ordered. If you force the array to be ordered, you can use optimised search routines to search the array (there are even some VI's on http://zone.ni.com/devzone/cda/epd/p/id/220).


The variant way of searching should be very fast, but if you would like to change the way the items are ordered (e.g. case (in)sensitive), you don't have a lot of options. I sometimes need to search a string with a regular expresion, which is also not possible with variants... So the variant method is fast to search, but not very flexible.


Perhaps someone can do some benchmarking? I'm stuck in 7.1 for now, and the variants are not optimized in this version.


Regards,


Wiebe.
Message 10 of 25
(12,199 Views)