PDA

View Full Version : Suggest me a container



giantdragon
2nd June 2011, 23:50
I have a list of structures which contains IP, port and transfer speed fields. List must be always sorted by speed (to take N-fastest clients) and have logarithmic search time by IP/port fields. Could you suggest me appropriate container for this?

SixDegrees
3rd June 2011, 00:03
...std::map

giantdragon
3rd June 2011, 00:14
std::map or QMap not a solution, as I understood they are always sorted and searched by THE SAME field, but I need to sort by one field and search by other. QMap can be searched by value, but it will be slow.

SixDegrees
3rd June 2011, 00:30
Use two. Use pointers.

DanH
3rd June 2011, 02:59
1) Use SQL.
2) As suggested, keep your objects separate and use pointers in two separate maps.
3) Roll your own. It's not rocket science.

Santosh Reddy
3rd June 2011, 08:37
You may need to maintain 3 containers, to achieve the specified performance.

1. sorted with speed (to take N-fastest clients), use qSort() to sort
2. sorted with IP/Port, use qSort() to sort then use qBinaryFind() to search
3. the master container with actual objects, the first two contain references to this container

you need to implement the appropriate operators (like ==, >) too use the containers (stl / qt algorithms) to be able to sort, find etc.

harishiva
12th September 2011, 11:44
The other option is to use Qhash