void insert(Vertex_info* v) { buffer.push_back(v); // this will create the initial, sorted buckets if (buffer.size() >= size) makeBucket(); }
int ExternalSort::sort() { if (outOfCore==false) { if (sortDimension==0) std::sort(buffer.begin(), buffer.end(), SpatialObjectXAsc()); else if (sortDimension==1) std::sort(buffer.begin(), buffer.end(), SpatialObjectYAsc()); else std::sort(buffer.begin(), buffer.end(), SpatialObjectZAsc()); rewind(); return buffer.size(); } uint64 count = 0; if (buffer.empty()==false) makeBucket(); sorted = new BufferedFile(); sorted->maketemporary(); uint32 Bsize = 0; Bsize = buckets.size(); buffer.reserve(Bsize); uint32 eofCount = 0; uint32 outindex = 0; for (uint32 i = 0; i < Bsize; i++) { SpatialObject* temp = SpatialObjectFactory::create(objType); buckets.at(i)->read(temp); if (buckets.at(i)->eof) { delete buckets.at(i); buckets.at(i) = NULL; delete temp; temp = NULL; eofCount++; } buffer.push_back(temp); } while (eofCount < Bsize) { // set pointer to first item for (uint32 i = 0; i < Bsize; i++) if (buckets.at(i) != NULL) { outindex = i; break; } if (outindex == Bsize) break; // find min item for (uint32 i = 0; i < Bsize; i++) if (buckets.at(i) != NULL) if (buffer.at(i)->getCenter()[sortDimension] < buffer.at(outindex)->getCenter()[sortDimension]) outindex = i; //write Sorted Content. sorted->write(buffer.at(outindex)); #ifdef PROGRESS count++; if (count%10000000==0) cout << "SORTING: " << count << " OBJECTS DONE"<< endl; #endif delete buffer.at(outindex); SpatialObject* temp = NULL; temp = SpatialObjectFactory::create(objType); buckets.at(outindex)->read(temp); if (buckets.at(outindex)->eof) { delete buckets.at(outindex); buckets.at(outindex) = NULL; delete temp; temp = NULL; eofCount++; } buffer.at(outindex) = temp; } for (vector<BufferedFile*>::iterator it = buckets.begin(); it != buckets.end(); ++it) delete *it; buckets.clear(); for (vector<SpatialObject*>::iterator it = buffer.begin(); it != buffer.end(); ++it) delete *it; buffer.clear(); rewind(); return count; }
void ExternalSort::insert(SpatialObject* object) { buffer.push_back(object); if (buffer.size() >= size) makeBucket(); }
int sort (process_sorted_output_fn process_sorted_output) { std::cerr << "\nstart sorting"; int count=0; if (buffer.empty()==false) makeBucket(); int Bsize = buckets.size(); buffer.reserve(Bsize); int eofCount=0; int outindex=0; for (int i=0; i<Bsize; i++) { Vertex_info* temp = new Vertex_info(); if (temp->load(*(buckets.at(i)))==false) { delete buckets.at(i); buckets.at(i)=NULL; delete temp; temp=NULL; eofCount++; } buffer.push_back(temp); } while (eofCount<Bsize) { // set pointer to first item for (int i=0; i<Bsize; i++) if (buckets.at(i)!=NULL) { outindex=i; break; } if (outindex==Bsize) break; // find min item for (int i=0; i<Bsize; i++) if (buckets.at(i)!=NULL) { double d1[3],d2[3]; d1[0] = buffer.at(i)->vi.coords[0]; d1[1] = buffer.at(i)->vi.coords[1]; d1[2] = buffer.at(i)->vi.coords[2]; d2[0] = buffer.at(outindex)->vi.coords[0]; d2[1] = buffer.at(outindex)->vi.coords[1]; d2[2] = buffer.at(outindex)->vi.coords[2]; if (hilbert_ieee_cmp(3,d1,d2)>=0) outindex=i; } //Sorted Output (process_sorted_output) (buffer.at(outindex)->vid, buffer.at(outindex)->vi); #ifdef VERBOSE std::cerr << buffer.at(outindex)->vi.coords[0] << " " << buffer.at(outindex)->vi.coords[1] << " " << buffer.at(outindex)->vi.coords[2]<< std::endl; #endif if (count%10000000==0 &&count>0) std::cout << "\n Points Sorted: " << count; count++; delete buffer.at(outindex); Vertex_info* temp = new Vertex_info(); if (temp->load(*(buckets.at(outindex)))==false) { delete buckets.at(outindex); buckets.at(outindex)=NULL; delete temp; temp=NULL; eofCount++; } buffer.at(outindex) = temp; } return count; }
/* Adds a host list, represented by the passed array and its length, to the hash * table. The array and the length is passed to the hashing function. * * The free_strings flag is true if the strings in the passed array have already * been allocated by the caller. It controls the freeing of such strings in the * case that the passed list already exists in the hash table. * This is a necessary inconvenience: addListToStore is called by the Bison/Flex generated * host graph parser which requires the strings it parses to be strdup'd (otherwise * things go wrong). Calls to addListToStore in other contexts pass arrays with * automatic strings which should not be freed. */ HostList *makeHostList(HostAtom *array, int length, bool free_strings) { #ifdef LIST_HASHING if(list_store == NULL) { list_store = calloc(LIST_TABLE_SIZE, sizeof(Bucket*)); if(list_store == NULL) { print_to_log("Error(addListToStore): malloc failure.\n"); exit(1); } } int hash = hashHostList(array, length); if(list_store[hash] == NULL) { Bucket *bucket = makeBucket(array, length, free_strings); list_store[hash] = bucket; bucket->list->hash = hash; return bucket->list; } /* Check each list in the bucket for equality with the list represented * by the passed array. */ else { Bucket *bucket = list_store[hash]; int index; bool make_bucket = true; while(bucket != NULL) { HostListItem *item = bucket->list->first; for(index = 0; index < length; index++) { if(item == NULL) break; HostAtom atom = array[index]; if(item->atom.type != atom.type) break; if(item->atom.type == 'i') { if(item->atom.num != atom.num) break; } else { if(strcmp(item->atom.str, atom.str) != 0) break; } item = item->next; } /* The lists are equal if and only if the ends of both lists are reached. * If an atom comparison failed, the for loop breaks before the end of * either list is reached. If the array is shorter, then the for loop * exits before item reaches its terminating NULL pointer. If the list * is shorter, the first line in the for loop body will cause the loop * to break before index == length. */ if(index == length && item == NULL) { make_bucket = false; break; } /* Exit the loop while maintaining the pointer to the last item in the * bucket list, because a list is going to be appended! */ if(bucket->next == NULL) break; bucket = bucket->next; } /* If control reaches this point, then no list in the bucket is equal to * the passed list. Make a new list! */ if(make_bucket) { Bucket *new_bucket = makeBucket(array, length, free_strings); bucket->next = new_bucket; new_bucket->prev = bucket; new_bucket->list->hash = hash; return new_bucket->list; } else { bucket->reference_count++; if(free_strings) { for(index = 0; index < length; index++) if(array[index].type == 's') free(array[index].str); } return bucket->list; } } #else HostList *list = NULL; int index; for(index = 0; index < length; index++) list = appendHostAtom(list, array[index], free_strings); return list; #endif }