Repository navigation
Expand file tree
/
Copy pathArrayCacheBySymbolBySide.php
More file actions
61 lines (57 loc) · 2.35 KB
/
Copy pathArrayCacheBySymbolBySide.php
File metadata and controls
61 lines (57 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace ccxt\pro;
class ArrayCacheBySymbolBySide extends ArrayCache {
public $hashmap;
private $index;
public function __construct($max_size = null) {
parent::__construct($max_size);
$this->nested_new_updates_by_symbol = true;
$this->hashmap = array();
$this->index = array();
}
public function append($item) {
if (array_key_exists($item['symbol'], $this->hashmap)) {
$by_side = &$this->hashmap[$item['symbol']];
} else {
$by_side = array();
$this->hashmap[$item['symbol']] = &$by_side;
}
if (array_key_exists($item['side'], $by_side)) {
$prev_ref = &$by_side[$item['side']];
# updates the reference
$prev_ref = $item;
$item = &$prev_ref;
$index = array_search($item['symbol'] . $item['side'], $this->index);
array_splice($this->index, $index, 1);
array_splice($this->deque, $index, 1);
} else {
$by_side[$item['side']] = &$item;
if (count($this->deque) === $this->max_size) {
$delete_item = array_shift($this->deque);
array_shift($this->index);
unset($this->hashmap[$delete_item['symbol']][$delete_item['side']]);
}
}
# this allows us to effectively pass by reference
$this->deque[] = &$item;
$this->index[] = $item['symbol'] . $item['side'];
if ($this->clear_all_updates) {
$this->clear_all_updates = false;
$this->clear_updates_by_symbol = array();
$this->all_new_updates = 0;
$this->new_updates_by_symbol = array();
}
if (!array_key_exists($item['symbol'], $this->new_updates_by_symbol)) {
$this->new_updates_by_symbol[$item['symbol']] = array();
}
if ($this->clear_updates_by_symbol[$item['symbol']] ?? false) {
$this->clear_updates_by_symbol[$item['symbol']] = false;
$this->new_updates_by_symbol[$item['symbol']] = array();
}
$side_set = &$this->new_updates_by_symbol[$item['symbol']];
$before_length = count($side_set);
$side_set[$item['side']] = 1;
$after_length = count($side_set);
$this->all_new_updates = ($this->all_new_updates ?? 0) + ($after_length - $before_length);
}
}