diff options
author | Jacek Antonelli | 2008-08-15 23:44:48 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:48 -0500 |
commit | 9b4f54c826ffa4f94efa866068c9d6ecdfb4b424 (patch) | |
tree | 2f8ae193ab487088962e628f1ee9dee2f5901f01 /linden/indra/lscript/lscript_library/lscript_alloc.cpp | |
parent | Second Life viewer sources 1.13.2.12 (diff) | |
download | meta-impy-9b4f54c826ffa4f94efa866068c9d6ecdfb4b424.zip meta-impy-9b4f54c826ffa4f94efa866068c9d6ecdfb4b424.tar.gz meta-impy-9b4f54c826ffa4f94efa866068c9d6ecdfb4b424.tar.bz2 meta-impy-9b4f54c826ffa4f94efa866068c9d6ecdfb4b424.tar.xz |
Second Life viewer sources 1.13.2.15
Diffstat (limited to 'linden/indra/lscript/lscript_library/lscript_alloc.cpp')
-rw-r--r-- | linden/indra/lscript/lscript_library/lscript_alloc.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/linden/indra/lscript/lscript_library/lscript_alloc.cpp b/linden/indra/lscript/lscript_library/lscript_alloc.cpp index 978d7f2..4636c0b 100644 --- a/linden/indra/lscript/lscript_library/lscript_alloc.cpp +++ b/linden/indra/lscript/lscript_library/lscript_alloc.cpp | |||
@@ -1119,3 +1119,62 @@ S32 lsa_postadd_lists(U8 *buffer, S32 offset1, LLScriptLibData *data, S32 heapsi | |||
1119 | return lsa_heap_add_data(buffer, list1, heapsize, TRUE); | 1119 | return lsa_heap_add_data(buffer, list1, heapsize, TRUE); |
1120 | } | 1120 | } |
1121 | 1121 | ||
1122 | |||
1123 | LLScriptLibData* lsa_randomize(LLScriptLibData* src, S32 stride) | ||
1124 | { | ||
1125 | S32 number = src->getListLength(); | ||
1126 | if (number <= 0) | ||
1127 | { | ||
1128 | return NULL; | ||
1129 | } | ||
1130 | if (stride <= 0) | ||
1131 | { | ||
1132 | stride = 1; | ||
1133 | } | ||
1134 | if(number % stride) | ||
1135 | { | ||
1136 | LLScriptLibData* retval = src->mListp; | ||
1137 | src->mListp = NULL; | ||
1138 | return retval; | ||
1139 | } | ||
1140 | S32 buckets = number / stride; | ||
1141 | |||
1142 | // Copy everything into a special vector for sorting; | ||
1143 | std::vector<LLScriptLibData*> sort_array; | ||
1144 | sort_array.reserve(number); | ||
1145 | LLScriptLibData* temp = src->mListp; | ||
1146 | while(temp) | ||
1147 | { | ||
1148 | sort_array.push_back(temp); | ||
1149 | temp = temp->mListp; | ||
1150 | } | ||
1151 | |||
1152 | // We cannot simply call random_shuffle or similar algorithm since | ||
1153 | // we need to obey the stride. So, we iterate over what we have | ||
1154 | // and swap each with a random other segment. | ||
1155 | S32 index = 0; | ||
1156 | S32 ii = 0; | ||
1157 | for(; ii < number; ii += stride) | ||
1158 | { | ||
1159 | index = ll_rand(buckets) * stride; | ||
1160 | for(S32 jj = 0; jj < stride; ++jj) | ||
1161 | { | ||
1162 | std::swap(sort_array[ii + jj], sort_array[index + jj]); | ||
1163 | } | ||
1164 | } | ||
1165 | |||
1166 | // copy the pointers back out | ||
1167 | ii = 1; | ||
1168 | temp = sort_array[0]; | ||
1169 | while (ii < number) | ||
1170 | { | ||
1171 | temp->mListp = sort_array[ii++]; | ||
1172 | temp = temp->mListp; | ||
1173 | } | ||
1174 | temp->mListp = NULL; | ||
1175 | |||
1176 | src->mListp = NULL; | ||
1177 | |||
1178 | LLScriptLibData* ret_value = sort_array[0]; | ||
1179 | return ret_value; | ||
1180 | } | ||