-
Notifications
You must be signed in to change notification settings - Fork 5
/
doubly_linked_lists.py
executable file
·353 lines (281 loc) · 13.4 KB
/
doubly_linked_lists.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env -S python3 -u
import compress_pickle
import itertools
import logging
import numpy as np
import os
import arguments_parsing_common
from bidirectional_hashes import BDHStack # type:ignore
from bidirectional_hashes import compute_forward_hash
from chains_objects import ChainGraph, PointerSet
from chains_objects import POINTER_SIZE
from constants import DOUBLY_LINKED_LISTS_FILE
from dask.bag import Bag
from more_itertools import pairwise
from numpy._typing import NDArray
MatchesType = tuple[list[tuple[NDArray,np.int64,NDArray,np.int64]], dict[np.int64,int]]
HashesType = np.dtype([('hash', np.uint64), ('direction', np.bool_), ('head', np.int64),
('tail', np.int64), ('offset', np.int32), ('size', np.uint64)])
def parse_arguments() -> dict:
# Get common parser and add argument
parser = arguments_parsing_common.get_parser()
parser.add_argument('--min-size', type=int, default=3, help="minimum length of chains (default: 3)")
return arguments_parsing_common.parse_arguments(parser)
def bidirectional_hashes(graph: ChainGraph, min_size:int) -> tuple[NDArray[HashesType], NDArray[HashesType]]:
"""
Computes bidirectional hashes.
Returns 2 multidimensional arrays: linear and cyclic
"""
# Initialize data
offset = graph.offset
diffs_min_size = min_size - 1
linear_results, cyclic_results = [], []
# Compute search
for cycle, rtrees in graph.component_breakdowns(min_size):
# Cyclic search
if cycle is not None:
first_hash = compute_forward_hash(np.diff(cycle))
second_hash = compute_forward_hash(np.diff(np.roll(cycle, -1)[::-1]))
# no palindromic sequences or hash conflicts
assert first_hash != second_hash
hashed, direction, head, tail = (second_hash, 1, cycle[1], cycle[0])
if first_hash < second_hash:
hashed, direction, head, tail = (first_hash, 0, cycle[0], cycle[-1])
cyclic_results.append((
hashed,
direction,
head,
tail,
offset,
cycle.size
))
# Linear search
for sink, parent_mapping in rtrees:
diffs = BDHStack()
stack:list[tuple[np.int64, np.int64, np.int64]]
stack = [
(np.int64(0), sink, parent)
for parent in parent_mapping[sink]
]
while stack:
depth, child, parent = stack.pop()
while len(diffs) != depth:
assert len(diffs) > depth
diffs.pop()
diffs.append(child - parent)
depth += 1
assert depth == len(diffs)
if depth >= diffs_min_size:
first_hash, second_hash = diffs.hash()
# fails for palindromic sequences or hash conflicts
assert first_hash != second_hash, list(diffs)
hashed, direction = (second_hash, 1)
if first_hash < second_hash:
hashed, direction = (first_hash, 0)
linear_results.append((
hashed,
direction,
parent,
sink,
offset,
depth + 1
))
stack.extend(
(depth, parent, grandpa)
for grandpa in parent_mapping.get(parent, [])
)
# Process results
linear = np.array(linear_results, HashesType)
cyclic = np.array(cyclic_results, HashesType)
linear.sort(order=('size', 'hash', 'direction'))
cyclic.sort(order=('size', 'hash', 'direction'))
return linear, cyclic
def get_unique_indices(array:NDArray) -> NDArray:
"""
Returns the indices of each first unique element of the sorted array
"""
if array.size == 0:
return np.array([])
mask = np.empty(array.shape, dtype=np.bool_)
mask[0] = True
mask[1:] = array[1:] != array[:-1]
return np.flatnonzero(mask)
def get_parameters_boundaries(rows:NDArray, assigned:dict) -> list[tuple[tuple[np.int64,np.int64,np.int64],tuple[np.int64,np.int64]]]:
"""
Calculates paramaters for building the chain
Results are sorted by lowest right boundary
The reference object is the head if direction == 0, tail otherwise.
Returns a list with the following structure:
- (
(head:int, offset:int, size:int),
(left_boundary:int, right_boundary:int)
)
"""
result = []
for _, direction, head, tail, offset, size in rows:
# Skip rows containing already assigned heads or tails
if head in assigned:
continue
if tail in assigned: # skip rows containing already assigned heads or tails
continue
# Get starting pointer
pointer = head
if direction:
pointer = tail
# By subtracting the offset we get what was pointed by the previous element
pointed = pointer - offset
# Get the boundaries
boundaries = (
pointer,
max(pointer + POINTER_SIZE + 1, pointed + 1)
)
if offset > 0:
boundaries = (
pointed,
pointer + POINTER_SIZE
)
result.append((
(head, offset, size),
boundaries
))
# Sort the results from the lowest right boundary
result.sort(key=lambda pair: pair[1][1])
return result
def compute_chain(pointers: dict[np.int64,np.int64], head:np.int64, offset:np.int64, size:np.int64):
"""Compute a chain using the pointers dictionary."""
result = [head]
while len(result) < size:
head = pointers[head] + offset
result.append(head)
return np.array(result, dtype=np.uint64).astype(np.int64)
def compute_matches(data: NDArray[HashesType], pointers: dict[np.int64, np.int64], label: str) -> MatchesType:
"""
Find matches from the computed hashes.
Returns a list (matches) and a dict (assigned)
"""
# First check
if not len(data):
return [], {}
# Data sorting
# ------------
# This sorting allows grouping by hash then direction
# We'll start from the bottom to prioritize longer lists
# Mergesort specifies Timsort, which is faster for almost-sorted data
data.sort(order=('size', 'hash', 'direction'), kind='mergesort')
# Data filtering
# --------------
# Take only duplicate values. See how np.unique is implemented to get how this works.
non_unique_mask:NDArray[bool] = np.concatenate([~np.diff(data['hash']).astype(bool), [False]])
non_unique_mask |= np.roll(non_unique_mask, 1)
data = data[non_unique_mask]
logging.info(f'{label}: {data.size:,} non-unique hashes')
assigned = {}
matches = []
# Data transformation for elaboration
unique_hash_indices = get_unique_indices(data['hash'])
chained_unique_indices = itertools.chain(unique_hash_indices, [None])
paired_unique_indices = list(pairwise(chained_unique_indices))
# We start from the bottom to give priority to longest chains
for first_index, second_index in paired_unique_indices[::-1]:
# This group contains all elements having the same (size, hash) pair
group = data[first_index:second_index]
assert len(set(group['hash'])) == 1
assert len(set(group['size'])) == 1
# Index discriminating between the two directions
changing_direction_index = np.searchsorted(group['direction'], 1)
# Hashes in just a single direction: no possible matches
if not (0 < changing_direction_index < group.size):
continue
# Get forward referencing pointers
forward = get_parameters_boundaries(group[:changing_direction_index], assigned)
if len(forward) == 0:
continue
# Get backward referencing pointers
# We use a dictionary because it preserves insertion order (by right boundary)
backward = dict(get_parameters_boundaries(group[changing_direction_index:], assigned))
if len(backward) == 0:
continue
# Get forward pointing chain
chain = compute_chain(pointers, *forward[0][0])
diff = np.diff(chain)
min_diff = np.min(np.diff(np.sort(chain)))
assert min_diff > 0
for (forward_head, forward_offset, forward_size), (forward_left, forward_right) in forward:
# The object must be in the interval:
# min(backward_left, forward_left) <= object < max(backward_right, forward_right)
# having the interval <= min_diff
# These thresholds are respectively minimum and maximum values for backward_left and backward_right
min_threshold = forward_right - min_diff
max_threshold = forward_left + min_diff
candidates, to_delete = [], []
for backward_parameters, (backward_left, backward_right) in backward.items():
# Since min_threshold depends on forward_right, backward_parameters wont' match any more
if backward_left < min_threshold:
to_delete.append(backward_parameters) # we can't delete from bwd right now, since we're iterating on it
continue
# Because of the sorting, we won't find any more matches with the forward chain
if backward_right > max_threshold:
break
candidates.append(backward_parameters)
for backward_parameters in to_delete:
del backward[backward_parameters]
if not candidates:
continue
forward_chain = compute_chain(pointers, forward_head, forward_offset, forward_size)
# Hash collision check
assert np.array_equal(np.diff(forward_chain), diff)
# Discard already assigned pointers
if any(pointer in assigned for pointer in forward_chain):
continue
# Try to match candidates. Closest ones first
forward_head = forward_chain[0]
sorted_candidates = sorted(candidates, key=lambda params: abs(forward_head - params[0]))
for backward_parameters in sorted_candidates:
backward_head, backward_offset, backward_size = backward_parameters
assert backward_size == forward_size
backward_chain = compute_chain(pointers, backward_head, backward_offset, backward_size)
# Hash collision check
assert np.array_equal(np.diff(backward_chain[::-1]), diff)
# Discard already assigned
if any(pointer in assigned for pointer in backward_chain):
del backward[backward_parameters]
continue
# Discard both direction pointers
assert len(set(forward_chain) & set(backward_chain)) == 0
# Interval size respected
assert forward_chain[0] - backward_chain[-1] <= min_diff
# Check correspondence between pointers (from different directions)
assert (np.diff(forward_chain - backward_chain[::-1]) == 0).all()
# Match found! Let's go!
matches_no = len(matches)
matches.append((forward_chain, forward_offset, backward_chain, backward_offset))
# Assign pointers
for chain in [backward_chain, forward_chain]:
for pointer in chain:
assigned[pointer] = matches_no
# Remove backward parameters
del backward[backward_parameters]
break
return matches, assigned
def search_linear_and_cyclic_matches(graphs:Bag, min_size:int, pointer_set:PointerSet) -> tuple[MatchesType, MatchesType]:
pointers:dict[np.int64, np.int64] = pointer_set.to_dict()
bd_hashes:zip[tuple[NDArray,NDArray]] = zip(*graphs.map(bidirectional_hashes, min_size).compute())
linear, cyclic = [
np.concatenate(arrays)
for arrays in bd_hashes
]
linear: NDArray[HashesType]
cyclic: NDArray[HashesType]
logging.info(f'hashes: {linear.size:,} (linear), {cyclic.size:,} (cyclic)')
return compute_matches(linear, pointers, 'linear'), \
compute_matches(cyclic, pointers, 'cyclic')
if __name__ == '__main__':
arguments = parse_arguments()
results = search_linear_and_cyclic_matches(arguments['graphs'], arguments['min_size'], arguments['pointers'])
for name, (match_list, pointer_to_match) in zip(['linear', 'cycles'], results):
matches_no = len(match_list)
try:
logging.info(f'{name}: {matches_no:,} lists (avg length {len(pointer_to_match) / matches_no:,.2f})')
except ZeroDivisionError:
logging.info('Something was wrong, 0 matches... :(')
compress_pickle.dump(results, os.path.join(arguments['output'], DOUBLY_LINKED_LISTS_FILE))