- if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self])
- //packages.push_back(package);
- CFArrayAppendValue(packages_, CFRetain(package));
+ if (Package *package = [Package newPackageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self]) {
+ if (unsigned index = package.metadata->index_) {
+ --index;
+ if (packages.size() == index) {
+ packages.push_back(package);
+ } else if (packages.size() <= index) {
+ packages.resize(index + 1, nil);
+ packages[index] = package;
+ continue;
+ } else {
+ std::swap(package, packages[index]);
+ if (package != nil)
+ goto lost;
+ if (last != index)
+ continue;
+ }
+ } else lost: {
+ ++lost;
+ if (last == packages.size()) {
+ packages.push_back(package);
+ ++last;
+ } else {
+ packages[last] = package;
+ ++last;
+ }
+ }
+
+ for (; last != packages.size(); ++last)
+ if (packages[last] == nil)
+ break;
+ }