| Author |
Message |
< Erlang ~ Set comparision |
| darkolee |
Posted: Thu Apr 01, 2010 4:22 pm |
|
|
|
User
Joined: 02 Dec 2009
Posts: 12
|
Hi
how should two sets be compared? Should the == operator be used?
If so, try out the following code
Set1=sets:from_list([a,q]).
Set2=sets:from_list([q,a]).
Set1==Set2.
The last statement returns false! Does anyone know of the proper way to compare sets which returns true if the elements of both sets are the same (without taking care of the order of these elements)?
Thanks |
|
|
| Back to top |
|
| zajda |
Posted: Thu Apr 01, 2010 10:20 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
hi,
== fails because [q,a] == [a,q] fails also.
if you dont care about performance use just something like:
Code: 3> [] =:= sets:to_list(sets:subtract(S1,S2)).
true
Writing custom code can be tricky, because in set structure there can be some pre-allocated empty segments (it depends what happened before with this set). Btw. thats why in above example we cannot pattern match on new() set.
I would start writing my code from combining chain of functions from library, in above example, into one custom (to cut down number of iterations). Maybe use at the beginning contract_seg/1 (which is not exported from sets) - to cut down length of iteration.
Michał Zajda
------------------
Erlang Solutions Ltd |
Last edited by zajda on Fri Apr 02, 2010 1:04 pm; edited 1 time in total |
|
| Back to top |
|
| darkolee |
Posted: Thu Apr 01, 2010 11:28 pm |
|
|
|
User
Joined: 02 Dec 2009
Posts: 12
|
Thanks for your reply.
I think the correct "heavy-weight" set comparision should be:
Code: [] =:= sets:to_list(
sets:union(
sets:subtract(S1,S2),
sets:subtract(S2,S1)
))
Well, it is inefficient as ugly as it looks, however that is the only way out. Shouldn't there be a simple equal/2 function in the sets module which returns a boolean indicating set equality? As far as I know, the set equality semantics are not affected by any sort of ordering of elements.
Quote: I would start writing my code from combining chain of functions from library, in above example, into one custom (to cut down number of iterations). Maybe use at the beginning contract_seg/1 (which is not exported from sets) - to cut down length of iteration.
I did not understand your final paragraph :s
Thanks |
|
|
| Back to top |
|
| zajda |
Posted: Fri Apr 02, 2010 1:15 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
Quote:
I did not understand your final paragraph :s
yep;) my English sucks sometimes;) let me rephrase it.
What I mean, is to go light weight not heavy weight.
To do it, we need to take a look of internals of the sets module,
to cut down to minimum iterations over set structure. By cut down I mean
make less of them and each one shorter. Maybe the cheapest solution would
be anyway rewriting it to some ordered structure? It need some profiling definitely. |
|
|
| Back to top |
|
| rvirding |
Posted: Thu Apr 08, 2010 10:48 pm |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
Sets (and dict) uses a hash table internally. One effect of this is that two sets which contain the same elements may not have the same internal structure, how it looks internally depends on how you got there, as in your example. All set operations are, of course, guaranteed to give the same results.
You get the same property with gb_sets which uses trees internally, two sets with the same elements are not guaranteed to have the same internal structure. The only set module I know of which has this property is ordsets.
A simple way to test set equality is Code: sets:size(sets:subtract(Set1, Set2)) == 0 If it faster than the other methods I don't know.
This may be a useful function to add to the API. |
|
|
| Back to top |
|
| darkolee |
Posted: Sun Apr 18, 2010 7:00 pm |
|
|
|
User
Joined: 02 Dec 2009
Posts: 12
|
Thanks for your answers. Shouldn't the quick set comparison check be:
Code:
( sets:size(sets:subtract(Set1, Set2)) == 0)
and
( sets:size(sets:subtract(Set2, Set1)) == 0)
???
Thanks |
|
|
| Back to top |
|
| zajda |
Posted: Sun Apr 18, 2010 8:28 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
this is the most elegant and simplest solution from given above in this thread.
It should be the fastest, because, if I am not wrong, set holds in its structure, its own size. No conversion = better performance.
You can always prepare some primitive test case and measure the execution time with timer:tc.
Michał Zajda
------------------
Erlang Solutions Ltd |
|
|
| Back to top |
|
| timrila |
Posted: Tue Jun 12, 2012 9:26 am |
|
|
|
User
Joined: 28 Mar 2012
Posts: 32
|
|
| Back to top |
|
| goods |
Posted: Fri Jun 29, 2012 2:03 am |
|
|
|
User
Joined: 28 Jun 2012
Posts: 14
|
| Christian Louboutin Homme Espadrilles est absolument représentatif de la mode et les loisirs, christian Chaussures Louboutin, est devenu une question de cours le protagoniste populaire de la saison. Nous ferons de notre mieux pour vous satisfaire. Toutes les Louboutin Pas Cher a été travaillé par des artisans et des espadrilles bonne Christian Louboutin pour les Christian Louboutin Homme par la main et c'est le résultat d'une sélection rigoureuse. si vous êtes engagé dans la vogue ou si vous voulez garder le même rythme avec la tendance, Louboutin 2012 chaussures de sport des hommes pourrait être votre meilleur choix. Commandez-le maintenant produits sont expédition libre. |
|
|
| Back to top |
|
| wuji |
Posted: Fri Aug 17, 2012 8:00 am |
|
|
|
User
Joined: 10 Aug 2012
Posts: 654
|
trapped inside the suspect's vehicle are not counted as innocent innocent [h2]cheap designer *beep*[/h2] innocent victims.Just last week in Lubbock, Texas, a toddler was
from the window of a careening SUV. Miraculously, the the replica designer *beep* the child survived.The vast majority of chases do not involve
robbers trying to get away from a shootout. Rather, Rather, knockoff designer *beep* Rather, the National Institute of Justice says, many are routine
stops gone awry. Nearly 90 percent of pursuits are for for [h4]jordan 6 olympic[/h4] for non-violent offenses.According to the institute, 46 percent of chases
drivers under the influence, 32 percent involve stolen cars, and and [h3]cheap Ralph Lauren[/h3] and 27 percent involve drivers with suspended licenses.In 2006, the
Highway Patrol instituted new policies to reduce risks. The CHP CHP cheap replica *beep* CHP discouraged officers from pursuing suspects when doing so would
endanger too many people.They've deployed devices designed to pop tires tires cheap jordans tires of fleeing suspects. They've adopted the use of the |
|
|
| Back to top |
|
| sowei |
Posted: Mon Aug 27, 2012 3:04 am |
|
|
|
Joined: 04 May 2012
Posts: 7
|
|
| Back to top |
|
| dongdongwu |
Posted: Thu Sep 20, 2012 2:11 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word. |
|
|
| Back to top |
|
|
|