diff options
author | Igor Pavlov <ipavlov@users.sourceforge.net> | 2007-01-20 03:00:00 +0300 |
---|---|---|
committer | Kornel LesiĆski <kornel@geekhood.net> | 2016-05-28 02:15:49 +0300 |
commit | d9666cf046a8453b33b3e2fbf4d82295a9f87df3 (patch) | |
tree | c722ed19b844b53042aec0c1d7d2f8381140a5ed /CPP/7zip/UI/Common/SortUtils.cpp | |
parent | 804edc5756fede54dbb1aefda6d39d306111938d (diff) |
4.44 beta
Diffstat (limited to 'CPP/7zip/UI/Common/SortUtils.cpp')
-rwxr-xr-x | CPP/7zip/UI/Common/SortUtils.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/CPP/7zip/UI/Common/SortUtils.cpp b/CPP/7zip/UI/Common/SortUtils.cpp new file mode 100755 index 00000000..c0111581 --- /dev/null +++ b/CPP/7zip/UI/Common/SortUtils.cpp @@ -0,0 +1,78 @@ +// SortUtils.cpp + +#include "StdAfx.h" + +#include "SortUtils.h" +#include "Common/Types.h" + +/* +template <class T> +void TSortRefDown(T *p, UInt32 k, UInt32 size, int (*compare)(const T*, const T*, void *), void *param) +{ + T temp = p[k]; + for (;;) + { + UInt32 s = (k << 1); + if (s > size) + break; + if (s < size && compare(p + s + 1, p + s, param) > 0) + s++; + if (compare(&temp, p + s, param) >= 0) + break; + p[k] = p[s]; + k = s; + } + p[k] = temp; +} + +template <class T> +void TSort(T* p, UInt32 size, int (*compare)(const T*, const T*, void *), void *param) +{ + if (size <= 1) + return; + p--; + { + UInt32 i = size / 2; + do + TSortRefDown(p, i, size, compare, param); + while(--i != 0); + } + do + { + T temp = p[size]; + p[size--] = p[1]; + p[1] = temp; + TSortRefDown(p, 1, size, compare, param); + } + while (size > 1); +} +*/ + +static int CompareStrings(const int *p1, const int *p2, void *param) +{ + const UStringVector &strings = *(const UStringVector *)param; + const UString &s1 = strings[*p1]; + const UString &s2 = strings[*p2]; + return s1.CompareNoCase(s2); +} + +void SortStringsToIndices(const UStringVector &strings, CIntVector &indices) +{ + indices.Clear(); + int numItems = strings.Size(); + indices.Reserve(numItems); + for(int i = 0; i < numItems; i++) + indices.Add(i); + indices.Sort(CompareStrings, (void *)&strings); + // TSort(&indices.Front(), indices.Size(), CompareStrings, (void *)&strings); +} + +void SortStrings(const UStringVector &src, UStringVector &dest) +{ + CIntVector indices; + SortStringsToIndices(src, indices); + dest.Clear(); + dest.Reserve(indices.Size()); + for (int i = 0; i < indices.Size(); i++) + dest.Add(src[indices[i]]); +} |