Archived
This forum has been archived. Please start a new discussion on GitHub.
ways to get the max&min value of a Freeze map index?
Dear pals,
Just wonder if there r any C++ API to retrieve the current maximum and minimum values of a particular index field in a freeze map?
Suppose there is students map based on the following SLICE:
and the corresponding freeze map is indexed on the field "id".
Is there any ways to find out the current min & max values of the "id" field stored in the student map.
Thanks
Just wonder if there r any C++ API to retrieve the current maximum and minimum values of a particular index field in a freeze map?
Suppose there is students map based on the following SLICE:
class Student
{
int id;
string name;
}
and the corresponding freeze map is indexed on the field "id".
Is there any ways to find out the current min & max values of the "id" field stored in the student map.
Thanks
0
Comments
-
Indexed fields will generate additional methods. From the Ice manual:
iterator findByMEMBER(MEMBER_TYPE, bool = true); const_iterator findByMEMBER(MEMBER_TYPE, bool = true) const; iterator lowerBoundForMEMBER(MEMBER_TYPE); const_iterator lowerBoundForMEMBER(MEMBER_TYPE) const; iterator upperBoundForMEMBER(MEMBER_TYPE); const_iterator upperBoundForMEMBER(MEMBER_TYPE) const; std::pair<iterator, iterator> equalRangeForMEMBER(MEMBER_TYPE); std::pair<const_iterator, const_iterator> equalRangeForMEMBER(MEMBER_TYPE) const; int MEMBERCount(MEMBER_TYPE) const;
You should also check issue 11 of our newsletter connections which has an article on these methods.
Following, assuming that you also set the sort function to less<int> then to find the lowest id you could do:
m.lowerBoundForId(0);
which will return an iterator to either the record with id 0, or the first record after this. In either case, assuming you never store records with an id < 0 then you are all set
Unfortunately, computing the biggest id is more difficult. You might think you could use upperBoundForId:
m.upperBoundForId(MAX_INT);
However, since you cannot iterate backwards with a freeze map iterator (they are not bidirectional iterators) then this return value does you no good.
Some alternate solutions to help:
- You could create another id field id2 that contains an identical id, index upon that and sort in the opposite direction (greater<int>). Then lowerBoundForId2(MAX_INT) would find the biggest id.
- You could create a dummy record and sort the biggest id in that field and use lowerBoundForId to find the lowest.
- You could compute the biggest and smallest on startup and cache them during the application run.
All things being equal likely the last is the simplest solution.0