[M3devel] STL algorithms? sort/unique?

mika at async.caltech.edu mika at async.caltech.edu
Fri Sep 28 09:56:56 CEST 2012


You're using the wrong (abstract) data structure if you want sort and unique.

A "sequence" is meant to be accessed sequentially...

Not knowing precisely what you're doing it sounds like you might want a
SortedTable... you can get unique on insert and access it in increasing
or decreasing order.

    Mika

Jay K writes:
>--_d2a02ece-d492-410e-88fb-fb53737d7219_
>Content-Type: text/plain; charset="iso-8859-1"
>Content-Transfer-Encoding: quoted-printable
>
>    I have an IntSeq.T with a bunch of integers.   =20
>    What is a good terse efficient idiom for sort and unique?   =20
>
>    In C++ I would say:        vector<int> a=3B        a.push_back(...)=3B =
>       a.push_back(...)=3B        std::sort(a.begin()=2C a.end())=3B       =
> a.resize(std::unique(a.begin()=2C a.end()) - a.begin())=3B        for (aut=
>o i =3D a.begin()=3B i !=3D a.end()=3B ++i)    {
>      do stuff with *i    }=20
>    Nice=2C terse=2C efficient.        In Modula-3?   =20
>
>I know that unique is one of the easiest algorithms to manually write inlin=
>e=2Cbut I shouldn't have to.
>
>(I'm really trying to stay in Modula-3 here=2C but it is definitely a strug=
>gle. :( )
>
>Thank you=2C - Jay
>
> 		 	   		  =
>
>--_d2a02ece-d492-410e-88fb-fb53737d7219_
>Content-Type: text/html; charset="iso-8859-1"
>Content-Transfer-Encoding: quoted-printable
>
><html>
><head>
><style><!--
>.hmmessage P
>{
>margin:0px=3B
>padding:0px
>}
>body.hmmessage
>{
>font-size: 12pt=3B
>font-family:Calibri
>}
>--></style></head>
><body class=3D'hmmessage'><div dir=3D'ltr'>&nbsp=3B &nbsp=3B&nbsp=3BI have =
>an IntSeq.T with a bunch of integers.&nbsp=3B &nbsp=3B&nbsp=3B<br><div><spa=
>n style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><span style=3D"font=
>-size: 12pt=3B ">&nbsp=3B</span>What is a good terse efficient idiom for so=
>rt and unique?<span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><=
>span style=3D"font-size: 12pt=3B ">&nbsp=3B</span></div><div><br></div><div=
>><br></div><div>&nbsp=3B &nbsp=3B In C++ I would say:<span style=3D"font-si=
>ze: 12pt=3B ">&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12pt=3B ">&=
>nbsp=3B</span></div><div><span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=
>=3B</span><span style=3D"font-size: 12pt=3B ">&nbsp=3B</span>vector&lt=3Bin=
>t&gt=3B a=3B<span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><sp=
>an style=3D"font-size: 12pt=3B ">&nbsp=3B</span></div><div><span style=3D"f=
>ont-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12pt=
>=3B ">&nbsp=3B</span>a.push_back(...)=3B<span style=3D"font-size: 12pt=3B "=
>>&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12pt=3B ">&nbsp=3B</span=
>></div><div><div><span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</spa=
>n><span style=3D"font-size: 12pt=3B ">&nbsp=3B</span>a.push_back(...)=3B<sp=
>an style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><span style=3D"fon=
>t-size: 12pt=3B ">&nbsp=3B</span></div><div><span style=3D"font-size: 12pt=
>=3B ">&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12pt=3B ">&nbsp=3B<=
>/span>std::sort(a.begin()=2C a.end())=3B<span style=3D"font-size: 12pt=3B "=
>>&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12pt=3B ">&nbsp=3B</span=
>></div><div><span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><sp=
>an style=3D"font-size: 12pt=3B ">&nbsp=3B</span>a.resize(std::unique(a.begi=
>n()=2C a.end()) - a.begin())=3B<span style=3D"font-size: 12pt=3B ">&nbsp=3B=
> &nbsp=3B</span><span style=3D"font-size: 12pt=3B ">&nbsp=3B</span></div><d=
>iv><span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B for (auto i =3D a.=
>begin()=3B i !=3D a.end()=3B ++i)</span></div><div><span style=3D"font-size=
>: 12pt=3B ">&nbsp=3B &nbsp=3B {<br>&nbsp=3B &nbsp=3B &nbsp=3B do stuff with=
> *i</span></div><div><span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B =
>}</span></div><div>&nbsp=3B</div><div><br></div><div><span style=3D"font-si=
>ze: 12pt=3B ">&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12pt=3B ">&=
>nbsp=3B</span>Nice=2C terse=2C efficient.<span style=3D"font-size: 12pt=3B =
>">&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12pt=3B ">&nbsp=3B</spa=
>n></div><div><span style=3D"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><s=
>pan style=3D"font-size: 12pt=3B ">&nbsp=3B</span>In Modula-3?<span style=3D=
>"font-size: 12pt=3B ">&nbsp=3B &nbsp=3B</span><span style=3D"font-size: 12p=
>t=3B ">&nbsp=3B</span></div><div><br></div><div><br></div><div>I know that =
>unique is one of the easiest algorithms to manually write inline=2C</div><d=
>iv>but I shouldn't have to.</div><div><br></div><div><br></div><div>(I'm re=
>ally trying to stay in Modula-3 here=2C but it is definitely a struggle. :(=
> )</div><div><br></div><div><br></div><div>Thank you=2C</div><div>&nbsp=3B-=
> Jay</div><br><br></div> 		 	   		  </div></body>
></html>=
>
>--_d2a02ece-d492-410e-88fb-fb53737d7219_--



More information about the M3devel mailing list