-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseMatrix.rb
670 lines (605 loc) · 13.8 KB
/
SparseMatrix.rb
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
############################################################################
# hash of hashes : faster than anything I've been able to concoct
############################################################################
class SparseMatrix
def initialize
@data = {}
# @rowsPresent = {}
# @colsPresent = {}
end
def []=(a,b,c)
hash = @data[a]
if not hash
hash = {}
@data[a] = hash
end
hash[b] = c
=begin
if c # actual data
@rowsPresent[a] = true
@colsPresent[b] = true
else # clearing out data
if hash.length == 0 # we cleared out the cols for this row
# determine if this col is present any longer
colPres = false
@rowsPresent.each_key do | row |
if @data[row][b]
colPres = true
break;
end
end
if not colPres
@colsPresent[b] = nil
end
# determine if this row has been cleared out too
if @data[a].length == 0
@rowsPresent[a] = nil
end
end
end
=end
end
def [](a,b)
hash = @data[a]
if hash
hash[b]
else
nil
end
end
# example @data = {{2=>{1=>fred}} , {0=>{6=>jim,3=>dave}}
def rows
rowIndex = nil
rowIndices = {}
@data.each_key do |rowIndex|
rowIndices[rowIndex] = true
end
rowIndices.keys.sort
# @rowsPresent.keys.sort
end
def cols
colHash = colIndex = nil
colIndices = {}
@data.each_value do |colHash|
colHash.each_key do | colIndex |
colIndices[colIndex] = true
end
end
colIndices.keys.sort
# @colsPresent.keys.sort
end
def each
row = rowHash = col = nil
@data.each_pair do | row, rowHash |
rowHash.each_key do | col |
yield @data[row][col]
end
end
end
def each_coord
row = rowHash = col = nil
@data.each_pair do | row, rowHash |
rowHash.each_key do | col |
yield(row,col)
end
end
end
end
__END__
############################################################################
# arrays version : for_type afdb 1:37 (hash of hash version above 0:50)
############################################################################
class SparseMatrix
def initialize
@rowIndices = []
@colIndices = []
@values = []
end
def find(r,c)
#[0,0,1,2,3,3,3,4]
#[1,5,3,1,1,2,4,0]
#r = 2 c = 4
rowIndex = colIndex = nil
low = 0
high = @values.length-1
until (high < low) do
curr = (low + high) / 2
rowIndex = @rowIndices[curr]
if r == rowIndex
colIndex = @colIndices[curr]
if c == colIndex
return [curr,true]
elsif c < colIndex
high = curr-1
else # c > colIndex
low = curr+1
end
elsif r < rowIndex
high = curr-1
else # r > rowIndex
low = curr+1
end
end
[low,false]
end
def []=(r,c,value)
result = find(r,c)
index = result[0]
entryPresent = result[1]
if value
if entryPresent # row-col entry was present
@values[index] = value
else # row-col entry not present - so insert
currLen = @values.length
if index == currLen #putting at last spot
@rowIndices << r
@colIndices << c
@values << value
else # inserting into list
size = currLen-index
@rowIndices = @rowIndices[0,index] + [r] + @rowIndices[index,size]
@colIndices = @colIndices[0,index] + [c] + @colIndices[index,size]
@values = @values[0,index] + [value] + @values[index,size]
end
end
else # will be deleting entries if present
if entryPresent
@rowIndices.delete_at(index)
@colIndices.delete_at(index)
@values.delete_at(index)
end
end
end
def [](r,c)
result = find(r,c)
if result[1] # result[1] == entry present
@values[result[0]] # result[0] == index of value
else
nil
end
end
def rows
@rowIndices.uniq
end
def cols
@colIndices.uniq
end
def each
@values.each do | value |
yield value
end
end
def each_coord
@values.each_index do | index |
yield(@rowsIndices[index],@colIndices[index])
end
end
end
############################################################################
# basic Hash indexing scheme : same as hash implementation but slower
############################################################################
class IndexedFrame
def initialize
@hash = {}
end
def each
@hash.each_value do | val |
yield val
end
end
def each_index
@hash.each_key do | key |
yield key
end
end
def addAt(index,obj)
@hash[index] = obj
end
def lookup(index)
@hash[index]
end
def indices # return an array of indices (sorted not important)
@hash.keys
end
end
############################################################################
# binary tree indexing scheme : many many times slower
############################################################################
class Node
def initialize(indx,obj)
@index = indx
@data = obj
end
attr_accessor(:left,:right,:index,:data)
end
class IndexedFrame
def initialize
@root = nil
end
def each
nodeList = []
nodeList.push(@root) if @root
while nodeList.length != 0
currNode = nodeList.pop
yield currNode.data
nodeList.push(currNode.left) if currNode.left
nodeList.push(currNode.right) if currNode.right
end
end
def each_index
nodeList = []
nodeList.push(@root) if @root
while nodeList.length != 0
currNode = nodeList.pop
yield currNode.index
nodeList.push(currNode.left) if currNode.left
nodeList.push(currNode.right) if currNode.right
end
end
def addAt(index,obj)
if not @root
@root = Node.new(index,obj)
else
prev = nil
curr = @root
while curr != nil
nodeIndex = curr.index
if index < nodeIndex
prev = curr
curr = curr.left
elsif index > nodeIndex
prev = curr
curr = curr.right
else
curr.data = obj
return obj
end
end
node = Node.new(index,obj)
# if get here prev points to insert spot
if index < prev.index
prev.left = node
else
prev.right = node
end
end
end
def lookup(index)
curr = @root
while curr != nil
nodeIndex = curr.index
if index < nodeIndex
curr = curr.left
elsif index > nodeIndex
curr = curr.right
else
return curr.data
end
end
nil
end
def indices # return an array of indices (sorted not important)
indexList = []
nodeList = []
nodeList.push(@root) if @root
while nodeList.length != 0
currNode = nodeList.pop
indexList << currNode.index
nodeList.push(currNode.left) if currNode.left
nodeList.push(currNode.right) if currNode.right
end
indexList
end
end
class NumericSparseMatrix
def initialize
@rowFrame = IndexedFrame.new
end
def []=(a,b,c)
colFrame = @rowFrame.lookup(a)
if not colFrame
colFrame = IndexedFrame.new
@rowFrame.addAt(a,colFrame)
end
colFrame.addAt(b,c)
end
def [](a,b)
colFrame = @rowFrame.lookup(a)
return nil if not colFrame
return colFrame.lookup(b)
end
def each
colFrame = obj = nil
@rowFrame.each do | colFrame |
colFrame.each do | obj |
yield obj
end
end
end
def each_coord
row = col = nil
@rowFrame.each_index do | row |
colFrame = @rowFrame.lookup(row)
colFrame.each_index do | col |
yield(row,col)
end
end
end
def rows
@rowFrame.indices.sort
end
def cols
colFrame = nil
colIndices = []
@rowFrame.each do | colFrame |
colIndices += colFrame.indices
end
colIndices.uniq.sort
end
end
if __FILE__ == $0
print "Begin comparison tests\n"
a = SparseMatrix.new
b = NumericSparseMatrix.new
1000.times do | i |
row = rand(i)
col = rand(i)
value = rand(100)
a[row,col] = value
b[row,col] = value
raise "Assign not equal" if a[row,col] != b[row,col]
end
raise ".rows not equal" if a.rows != b.rows
raise ".cols not equal" if a.cols != b.cols
adata = []
a.each do | item |
adata << item
end
bdata = []
b.each do | item |
bdata << item
end
raise "Data wrong" if adata != bdata
aSequence = []
a.each_coord do | row,col |
aSequence << [row,col]
end
bSequence = []
b.each_coord do | row,col |
bSequence << [row,col]
end
raise "Sequence wrong" if aSequence != bSequence
ITERS = 10000
u = Time.now
ITERS.times do | i |
row = rand(i)
col = rand(ITERS-i)
value = 73
a[row,col] = value
a[row,col]
if i == 900
adata = []
a.each do | item |
adata << item
end
aSequence = []
a.each_coord do | row,col |
aSequence << [row,col]
end
end
end
v = Time.now
ITERS.times do | i |
row = rand(i)
col = rand(ITERS-i)
value = 73
b[row,col] = value
b[row,col]
if i == 900
bdata = []
b.each do | item |
bdata << item
end
bSequence = []
b.each_coord do | row,col |
bSequence << [row,col]
end
end
end
w = Time.now
print "A speed: ",v-u,"\n"
print "B speed: ",w-v,"\n"
print "End comparison tests\n"
end
#####################################################################################
#
#class Item
# def initialize(index,item)
# @index = index
# @item = item
# end
# attr_accessor(:index,:item)
#end
#
#class SparseMatrix
# def initialize
# @items = []
# end
#
# def []=(a,b,c)
# columnItem = @items.find { | obj | obj.index == a }
# if columnItem.nil?
# columnItem = Item.new(a,Array.new)
# @items << columnItem
# end
# item = columnItem.item.find { | obj | obj.index == b }
# if item.nil?
# item = Item.new(b,c)
# columnItem.item << item
# end
# item.item = c
# end
#
# def [](a,b)
# columnItem = @items.find { | obj | obj.index == a }
# if columnItem
# item = columnItem.item.find { | obj | obj.index == b }
# item ? item.item : nil
# else
# nil
# end
# end
#
# def rows
# rowIndices = []
# @items.each do | colItem |
# rowIndices << colItem.index
# end
# rowIndices
# end
#
# def cols
# colIndices = []
# @items.each do |colItem|
# colItem.item.each do | item |
# colIndices << item.index
# end
# end
# colIndices.uniq
# end
#
# def each
# @items.each do | colItem |
# colItem.item.each do | item |
# yield @items[colItem.index].item[item.index].item
# end
# end
# end
#
# def each_coord
# @items.each do | colItem |
# colItem.item.each do | item |
# yield(colItem.index,item.index)
# end
# end
# end
#end
#####################################################################################
#
#class BalancedTreeNode
# def initialize(item)
# @left = nil
# @right = nil
# @item = item
# end
#
# attr_accessor(:left,:right,:item)
#end
#
#class BalancedTree
# def initialize
# @root = nil
# end
#
# def add(label,item)
# if @root
# @root = BalancedTreeNode.new(item)
# else
# node = @root
# spot = nil
# while node
# spot = node
# if label < node.label
# node = node.left
# elsif label > node.label
# node = node.right
# else
# raise "Error in BalancedTree.add : trying to add an existing label."
# end
# end
# if label < spot.label
# spot.left = BalancedTreeNode.new(item)
# else
# spot.right = BalancedTreeNode.new(item)
# end
# end
# end
#
# def find(label)
# end
#
# def each_label
# end
#
# def each_value
# end
#
# def [](label)
# end
#end
#
#class SparseMatrix
#
# def initialize
# @items = BalancedTree.new
# end
#
# def []=(a,b,c)
# columnTree = @items.find(a)
# if columnTree.nil?
# columnTree = BalancedTree.new
# @items.add(a,columnTree)
# end
# node = columnTree.find(b)
# if node.nil?
# node = BalancedTreeNode.new(b)
# columnTree.add(b,node)
# end
# node.item = c
# end
#
# def [](a,b)
# columnTree = @items.find(a)
# if columnTree
# node = columnTree.find(b)
# node ? node.item : nil
# else
# nil
# end
# end
#
# def rows
# rowIndices = []
# @items.each_label do |rowIndex|
# rowIndices << rowIndex
# end
# rowIndices
# end
#
# def cols
# colIndices = []
# @items.each_value do |colTree|
# colTree.each_label do | colIndex |
# colIndices << colIndex
# end
# end
# colIndices.uniq
# end
#
# def each
# @items.each_pair do | row, rowTree |
# rowTree.each_label do | col |
# yield @items[row][col]
# end
# end
# end
#
# def each_coord
# @data.each_pair do | row, rowTree |
# rowTree.each_label do | col |
# yield(row,col)
# end
# end
# end
#
#end