Rewrote PropList output string sorting to get rid of C4Set<T>::Sort()

C4Set<T>::Sort() breaks the internals of the class which Guenther doesn't like. Now the sorted list is returned as a list of pointers into the original set.
Sven Eberhardt 2012-10-16 15:47:56 +02:00
parent ef674b1779
commit 140dc97b24
2 changed files with 19 additions and 11 deletions

View File

@ -330,16 +330,13 @@ void C4PropList::AppendDataString(StdStrBuf * out, const char * delim, int depth
DataString.Append("...");
return;
}
C4Set<C4Property> sorted_props = Properties;
sorted_props.Sort();
const C4Property * p = sorted_props.First();
while (p)
std::list<const C4Property *> sorted_props = Properties.GetSortedListOfElementPointers();
for (std::list<const C4Property *>::const_iterator p = sorted_props.begin(); p != sorted_props.end(); ++p)
{
DataString.Append(p->Key->GetData());
if (p != sorted_props.begin()) DataString.Append(delim);
DataString.Append((*p)->Key->GetData());
DataString.Append(" = ");
DataString.Append(p->Value.GetDataString(depth - 1));
p = sorted_props.Next(p);
if (p) DataString.Append(delim);
DataString.Append((*p)->Value.GetDataString(depth - 1));
}
}

View File

@ -254,10 +254,21 @@ public:
Size = Size2;
Table = Table2;
}
void Sort()
static bool SortFunc(const T *p1, const T*p2)
{
// sort by keys
std::sort(&Table[0], &Table[Capacity]);
// elements are guarantueed to be non-NULL
return *p1<*p2;
}
std::list<const T *> GetSortedListOfElementPointers() const
{
// return a list of pointers to all elements in this set sorted by the standard less-than operation
// of the elements
// elements of resulting lists are guarantueed to be non-NULL
// list remains valid as long as this set is not changed
std::list<const T *> result;
for (const T *p = First(); p; p = Next(p)) result.push_back(p);
result.sort(C4Set<T>::SortFunc);
return result;
}
};