Commit 04d897f7865bc0f4a1de73a3d4a52543f25ff123

Authored by Furkan
1 parent 5cdfbf21

Way faster now

php/classes/CatalogCacheMgr.php
... ... @@ -207,116 +207,61 @@ class CatalogCacheMgr extends TimeTableCacheMgr
207 207 // Wrap the result in parentheses
208 208 return $str;
209 209 }
210   - public function calculateAddColumn($firstColumnId, $secondColumnId, $operation, $newColumnObj, $intervals){
211   - // Computing columns
212   - $newColumnValues = array();
213   - foreach($intervals['intervals'] as $interval){
214   - $fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
215   - $sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
216   -
217   - if ($newColumnObj->size == 1){
218   - switch ($operation) {
219   - case 'plus':
220   - $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] + $interval[$secondColumnId];
221   - break;
222   - case 'minus':
223   - $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] - $interval[$secondColumnId];
224   - break;
225   - case 'multiplication':
226   - $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] * $interval[$secondColumnId];
227   - break;
228   - case 'division':
229   - $newColumnValues[$interval['cacheId']] = ($interval[$secondColumnId] == 0) ? NAN : $interval[$firstColumnId] / $interval[$secondColumnId];;
230   - break;
231   - }
232   - }
233   - else{
234   - switch ($operation) {
235   - case 'plus':
236   - if ($newColumnObj->size > 1){
237   - if (count($fColumnVectorValues) !== count($sColumnVectorValues))
238   - return array('success' => false, 'message' => "Addition not possible");
239   -
240   - $result = [];
241   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
242   - $result[] = $fColumnVectorValues[$i] + $sColumnVectorValues[$i];
243   - $newColumnValues[$interval['cacheId']] = $result;
244   - }
245   - break;
246   - case 'minus':
247   - if ($newColumnObj->size > 1){
248   - if (count($fColumnVectorValues) !== count($sColumnVectorValues))
249   - return array('success' => false, 'message' => "Vectors must be of the same length.");
250   -
251   - $result = [];
252   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
253   - $result[] = $fColumnVectorValues[$i] - $sColumnVectorValues[$i];
254   - $newColumnValues[$interval['cacheId']] = $result;
255   - }
256   - else{
257   - return array('success' => false, 'message' => "Substraction not possible");
258   - }
259   -
260   - break;
261   - case 'multiplication':
262   - if (count($fColumnVectorValues) == count($sColumnVectorValues)) {
263   - $result = [];
264   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
265   - $result[] = $fColumnVectorValues[$i] * $sColumnVectorValues[$i];
266   - $newColumnValues[$interval['cacheId']] = $result;
267   - }
268   - else if(count($fColumnVectorValues) == 1){
269   - $result = [];
270   - for ($i = 0; $i < count($sColumnVectorValues); $i++)
271   - $result[] = $fColumnVectorValues[0] * $sColumnVectorValues[$i];
272   - $newColumnValues[$interval['cacheId']] = $result;
273   - }
274   - else if(count($sColumnVectorValues) == 1){
275   - $result = [];
276   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
277   - $result[] = $sColumnVectorValues[0] * $fColumnVectorValues[$i];
278   - $newColumnValues[$interval['cacheId']] = $result;
279   - }
280   - else{
281   - return array('success' => false, 'message' => "Multiplication not possible");
282   - }
283   -
284   - break;
285   - case 'division':
286   - if (count($fColumnVectorValues) == count($sColumnVectorValues)) {
287   - $result = [];
288   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
289   - $result[] = ($sColumnVectorValues[$i] == 0) ? NAN :$fColumnVectorValues[$i] / $sColumnVectorValues[$i];
290   - $newColumnValues[$interval['cacheId']] = $result;
291   - }
292   - else if(count($sColumnVectorValues) == 1){
293   - $result = [];
294   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
295   - $result[] = ($sColumnVectorValues[0] == 0) ? NAN : $fColumnVectorValues[$i] / $sColumnVectorValues[0];
296   - $newColumnValues[$interval['cacheId']] = $result;
297   - }
298   - else{
299   - return array('success' => false, 'message' => "Division not possible");
300   - }
301   -
302   -
303   - break;
304   - default:
305   - break;
306   - }
307   - }
308 210  
309   -
310   -
311   - }
312   - for ($i = 0; $i < count($newColumnValues); $i++)
313   - {
314   - $data= array();
315   - $data[$newColumnObj->id] = (count($newColumnValues[$i]) > 1) ? $this->vectorToString($newColumnValues[$i]) : $newColumnValues[$i];
316   - if(!$this->modifyIntervalFromId($i, $data))
317   - return array('success' => false, 'message' => "Impossible to modify the column");
  211 + public function calculateAddColumn($firstColumnId, $secondColumnId, $operation, $newColumnObj, $intervals) {
  212 + // Computing columns
  213 + $newColumnValues = array();
  214 + $isSingleValue = ($newColumnObj->size == 1);
  215 +
  216 + foreach ($intervals['intervals'] as $interval) {
  217 + $fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
  218 + $sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
  219 +
  220 + if ($isSingleValue) {
  221 + $newColumnValues[$interval['cacheId']] = $this->performOperation($interval[$firstColumnId], $interval[$secondColumnId], $operation);
  222 + } else {
  223 + $fCount = count($fColumnVectorValues);
  224 + $sCount = count($sColumnVectorValues);
  225 +
  226 + if ($fCount !== $sCount) {
  227 + return array('success' => false, 'message' => "Vectors must be of the same length.");
  228 + }
  229 +
  230 + $result = array();
  231 + for ($i = 0; $i < $fCount; $i++) {
  232 + $result[] = $this->performOperation($fColumnVectorValues[$i], $sColumnVectorValues[$i], $operation);
  233 + }
  234 + $newColumnValues[$interval['cacheId']] = $result;
  235 + }
  236 + }
  237 +
  238 + $data = array();
  239 + foreach ($newColumnValues as $cacheId => $value) {
  240 + $data[$cacheId] = array(
  241 + $newColumnObj->id => (is_array($value)) ? $this->vectorToString($value) : $value
  242 + );
  243 + }
  244 +
  245 + if (!$this->modifyIntervals($data)) {
  246 + return array('success' => false, 'message' => "Impossible to modify the columns");
  247 + }
  248 +
  249 + return array('success' => true, 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(), 'parameters' => $this->cache->getParametersInfo());
  250 +}
  251 +
  252 + private function performOperation($a, $b, $operation) {
  253 + switch ($operation) {
  254 + case 'plus':
  255 + return $a + $b;
  256 + case 'minus':
  257 + return $a - $b;
  258 + case 'multiplication':
  259 + return $a * $b;
  260 + case 'division':
  261 + return ($b == 0) ? NAN : $a / $b;
  262 + default:
  263 + return null;
318 264 }
319   - return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
320 265 }
321 266 }
322 267 ?>
... ...
php/classes/CatalogCacheObject.php
... ... @@ -197,6 +197,26 @@ class CatalogCacheObject extends TimeTableCacheObject
197 197 return true;
198 198 }
199 199  
  200 + public function modifyIntervals($data) {
  201 + $modified = false;
  202 +
  203 + foreach ($this->intervals as $interval) {
  204 + $cacheId = $interval->getId();
  205 +
  206 + $result = parent::modifyIntervalFromId($cacheId, $data);
  207 + if (isset($data[$cacheId])) {
  208 + foreach ($data[$cacheId] as $key => $value) {
  209 + $interval->setParamValue($key, $value);
  210 + $interval->setIsModified(true);
  211 + $this->isModified = true;
  212 + }
  213 + $modified = true;
  214 + }
  215 + }
  216 +
  217 + return $modified;
  218 + }
  219 +
200 220 public function modifyIntervalFromId($cacheId, $data) {
201 221 $result = parent::modifyIntervalFromId($cacheId, $data);
202 222  
... ...
php/classes/TimeTableCacheMgr.php
... ... @@ -267,6 +267,18 @@
267 267 return array('success' => true, 'status' => $this->cache->getStatus());
268 268 }
269 269  
  270 + public function modifyIntervals($data) {
  271 + if (!$this->loadFromFile())
  272 + return array('success' => false, 'message' => 'Cannot load cache file');
  273 +
  274 + $this->cache->modifyIntervals($data);
  275 +
  276 + $this->saveToFile();
  277 +
  278 + return array('success' => true, 'status' => $this->cache->getStatus());
  279 + }
  280 +
  281 +
270 282 public function modifyIntervalFromId($cacheId, $data) {
271 283 if (!$this->loadFromFile())
272 284 return array('success' => false, 'message' => 'Cannot load cache file');
... ...