InplaceMerge.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _INPLACE_MERGE_H
00019 #define _INPLACE_MERGE_H
00020
00021 #include <algorithm>
00022 #if defined(DEBUG_INPLACE_MERGE)
00023 #include "Generic.h"
00024 #include <iostream>
00025 #endif
00026 #include <stdexcept>
00027 #include <vector>
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 template<typename T> void inplace_merge(
00042 std::vector<int> &indeces,
00043 std::vector<int> &counts,
00044 int first,
00045 int last,
00046 std::vector<T> &values)
00047 {
00048 if (first == (last)) return;
00049 if (first == (last-1)) return;
00050
00051
00052
00053
00054 if (first != (last - 2))
00055 {
00056 int middle = (first + last) / 2;
00057 inplace_merge(indeces, counts, middle, last, values);
00058 #if defined(DEBUG_INPLACE_MERGE)
00059 std::cout << values;
00060 #endif
00061 inplace_merge(indeces, counts, first, middle, values);
00062 #if defined(DEBUG_INPLACE_MERGE)
00063 std::cout << values;
00064 #endif
00065
00066
00067
00068 last = middle + 1;
00069 }
00070
00071
00072 typename std::vector<T>::iterator startIterator = values.begin()+indeces[first];
00073 typename std::vector<T>::iterator middleIterator = values.begin() + indeces[last-1];
00074 typename std::vector<T>::iterator endIterator = values.begin() + indeces[last-1] + counts[last - 1];
00075 std::inplace_merge(startIterator, middleIterator, endIterator);
00076 counts[first] += counts[last - 1];
00077 #if defined(DEBUG_INPLACE_MERGE)
00078 std::cout << values;
00079 #endif
00080
00081 return;
00082 }
00083
00084 #endif