@icgio/icg-utils 1.7.67 → 1.7.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/utils.js +98 -30
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -95,13 +95,45 @@ String.prototype.replaceAt = function (index, replacement) {
95
95
  return this.substr(0, index) + replacement + this.substr(index + replacement.length)
96
96
  }
97
97
 
98
+ function binary_find(search_element, is_ask, keys) {
99
+ let min_index = 0
100
+ let max_index = keys.length - 1
101
+ let current_index
102
+ let current_element
103
+ while (min_index <= max_index) {
104
+ current_index = ((min_index + max_index) / 2) | 0
105
+ current_element = keys[current_index]
106
+ if ((current_element < search_element && is_ask) || (current_element > search_element && !is_ask)) {
107
+ min_index = current_index + 1
108
+ } else if ((current_element > search_element && is_ask) || (current_element < search_element && !is_ask)) {
109
+ max_index = current_index - 1
110
+ } else {
111
+ return {
112
+ found: true,
113
+ index: current_index,
114
+ }
115
+ }
116
+ }
117
+ return {
118
+ found: false,
119
+ index: (current_element < search_element && is_ask) || (current_element > search_element && !is_ask) ? current_index + 1 : current_index,
120
+ }
121
+ }
122
+
98
123
  module.exports.ordered_dict = function (is_ask) {
99
124
  let keys = [],
100
125
  vals = {}
101
126
  return {
127
+ init: function (kvs) {
128
+ for (let [k, v] of kvs) {
129
+ this.insert(k, v)
130
+ }
131
+ },
102
132
  push: function (k, v) {
103
133
  k = parseFloat(k)
104
- if (!vals[k]) keys.push(k)
134
+ if (!vals[k]) {
135
+ keys.push(k)
136
+ }
105
137
  vals[k] = v
106
138
  },
107
139
  insert: function (k, v) {
@@ -126,6 +158,42 @@ module.exports.ordered_dict = function (is_ask) {
126
158
  delete vals[k]
127
159
  }
128
160
  },
161
+ del_before: function (k) {
162
+ k = parseFloat(k)
163
+ let search = binary_find(k, is_ask, keys)
164
+ let pos = search.index
165
+ if (search.found) {
166
+ let keys_to_del = keys.slice(0, pos)
167
+ keys = keys.slice(pos + 1, keys.length)
168
+ for (let k_to_del of keys_to_del) {
169
+ delete vals[k_to_del]
170
+ }
171
+ } else if (pos > 1) {
172
+ let keys_to_del = keys.slice(0, pos - 1)
173
+ keys = keys.slice(pos, keys.length)
174
+ for (let k_to_del of keys_to_del) {
175
+ delete vals[k_to_del]
176
+ }
177
+ }
178
+ },
179
+ del_after: function (k) {
180
+ k = parseFloat(k)
181
+ let search = binary_find(k, is_ask, keys)
182
+ let pos = search.index
183
+ if (search.found) {
184
+ let keys_to_del = keys.slice(pos + 1, keys.length)
185
+ keys = keys.slice(0, pos)
186
+ for (let k_to_del of keys_to_del) {
187
+ delete vals[k_to_del]
188
+ }
189
+ } else {
190
+ let keys_to_del = keys.slice(pos + 1, keys.length)
191
+ keys = keys.slice(0, pos)
192
+ for (let k_to_del of keys_to_del) {
193
+ delete vals[k_to_del]
194
+ }
195
+ }
196
+ },
129
197
  val: function (k) {
130
198
  return vals[k]
131
199
  },
@@ -144,35 +212,35 @@ module.exports.ordered_dict = function (is_ask) {
144
212
  }
145
213
  }
146
214
 
147
- function binary_find(searchElement, is_ask, keys) {
148
- let minIndex = 0
149
- let maxIndex = keys.length - 1
150
- let currentIndex
151
- let currentElement
152
-
153
- while (minIndex <= maxIndex) {
154
- currentIndex = ((minIndex + maxIndex) / 2) | 0
155
- currentElement = keys[currentIndex]
156
-
157
- if ((currentElement < searchElement && is_ask) || (currentElement > searchElement && !is_ask)) {
158
- minIndex = currentIndex + 1
159
- } else if ((currentElement > searchElement && is_ask) || (currentElement < searchElement && !is_ask)) {
160
- maxIndex = currentIndex - 1
161
- } else {
162
- return {
163
- // Modification
164
- found: true,
165
- index: currentIndex,
166
- }
167
- }
168
- }
169
-
170
- return {
171
- // Modification
172
- found: false,
173
- index: (currentElement < searchElement && is_ask) || (currentElement > searchElement && !is_ask) ? currentIndex + 1 : currentIndex,
174
- }
175
- }
215
+ // let asks = [
216
+ // [1, 1],
217
+ // [2, 2],
218
+ // [3, 3],
219
+ // [4, 4],
220
+ // [5, 3],
221
+ // [6, 2],
222
+ // [7, 1],
223
+ // ]
224
+ // let asks_dict = module.exports.ordered_dict(true)
225
+ // asks_dict.init(asks)
226
+ // // asks_dict.del_before(3.333)
227
+ // asks_dict.del_after(5)
228
+ // console.log(asks_dict.entries())
229
+
230
+ // let bids = [
231
+ // [7, 1],
232
+ // [6, 2],
233
+ // [5, 3],
234
+ // [4, 4],
235
+ // [3, 3],
236
+ // [2, 2],
237
+ // [1, 1],
238
+ // ]
239
+ // let bids_dict = module.exports.ordered_dict(false)
240
+ // bids_dict.init(bids)
241
+ // bids_dict.del_before(8)
242
+ // // bids_dict.del_after(7)
243
+ // console.log(bids_dict.entries())
176
244
 
177
245
  module.exports.random_id = function () {
178
246
  return _.random(100000000, 999999999)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.67",
3
+ "version": "1.7.68",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {