Module: HDLRuby::High::Std::SEnumerable

Included in:
SEnumerator
Defined in:
lib/HDLRuby/std/sequencer.rb

Overview

Module adding functionalities to object including the +seach+ method.

Instance Method Summary collapse

Instance Method Details

#sall?(arg = nil, &ruby_block) ⇒ Boolean

Tell if all the elements respect a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/HDLRuby/std/sequencer.rb', line 319

def sall?(arg = nil,&ruby_block)
    # Declare the result signal.
    res = nil
    HDLRuby::High.cur_system.open do
        res = bit.inner(HDLRuby.uniq_name(:"all_cond"))
    end
    # Initialize the result.
    res <= 1
    # Performs the computation.
    if arg then
        # Compare elements to arg.
        self.seach do |elem|
            res <= res & (elem == arg)
        end
    elsif ruby_block then
        # Use the ruby block.
        self.seach do |elem|
            res <= res & ruby_block.call(elem)
        end
    else
        raise "Ruby nil does not have any meaning in HW."
    end
    res
end

#sany?(arg = nil, &ruby_block) ⇒ Boolean

Tell if any of the elements respects a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/HDLRuby/std/sequencer.rb', line 346

def sany?(arg = nil,&ruby_block)
    # Declare the result signal.
    res = nil
    HDLRuby::High.cur_system.open do
        res = bit.inner(HDLRuby.uniq_name(:"any_cond"))
    end
    # Initialize the result.
    res <= 0
    # Performs the computation.
    if arg then
        # Compare elements to arg.
        self.seach do |elem|
            res <= res | (elem == arg)
        end
    elsif ruby_block then
        # Use the ruby block.
        self.seach do |elem|
            res <= res | ruby_block.call(elem)
        end
    else
        raise "Ruby nil does not have any meaning in HW."
    end
    res
end

#schain(arg) ⇒ Object

Returns an SEnumerator generated from current enumerable and +arg+



372
373
374
# File 'lib/HDLRuby/std/sequencer.rb', line 372

def schain(arg)
    return self.seach + arg
end

#schunk(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby chunk. NOTE: to do, or may be not.



378
379
380
# File 'lib/HDLRuby/std/sequencer.rb', line 378

def schunk(*args,&ruby_block)
    raise "schunk is not supported yet."
end

#schunk_while(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby chunk_while. NOTE: to do, or may be not.



384
385
386
# File 'lib/HDLRuby/std/sequencer.rb', line 384

def schunk_while(*args,&ruby_block)
    raise "schunk_while is not supported yet."
end

#scompactObject

HW implementation of the Ruby compact, but remove 0 values instead on nil (since nil that does not have any meaning in HW).



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
# File 'lib/HDLRuby/std/sequencer.rb', line 422

def scompact
    # Generate the vector to put the result in.
    # The declares the resulting vector and index.
    res = nil
    idx = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"compact_vec"))
        idx = [enum.size.width].inner(HDLRuby.uniq_name(:"compact_idx"))
    end
    # And do the iteration.
    idx <= 0
    enum.seach do |elem|
        HDLRuby::High.top_user.hif(elem != 0) do
            res[idx] <= elem
            idx <= idx + 1
        end
    end
    SequencerT.current.swhile(idx < enum.size) do
        res[idx] <= 0
        idx <= idx + 1
    end
    # Return the resulting vector.
    return res
end

#scount(obj = nil, &ruby_block) ⇒ Object

WH implementation of the Ruby count.



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
# File 'lib/HDLRuby/std/sequencer.rb', line 450

def scount(obj = nil, &ruby_block)
    # Generate the counter result signal.
    cnt = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        cnt = [enum.size.width].inner(HDLRuby.uniq_name(:"count_idx"))
    end
    # Do the counting.
    cnt <= 0
    # Is obj present?
    if obj then
        # Yes, count the occurences of obj.
        enum.seach do |elem|
            HDLRuby::High.top_user.hif(obj == elem) { cnt <= cnt + 1 }
        end
    elsif ruby_block
        # No, but there is a ruby block, use its result for counting.
        enum.seach do |elem|
            HDLRuby::High.top_user.hif(ruby_block.call(elem)) do
                cnt <= cnt + 1
            end
        end
    else
        # No, the result is simply the number of elements.
        cnt <= enum.size
    end
    return cnt
end

#scycle(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby cycle.



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/HDLRuby/std/sequencer.rb', line 480

def scycle(n = nil,&ruby_block)
    # No block given? Generate a new wrapper enumerator for scycle.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:scycle,n)
    end
    this = self
    # Is n nil?
    if n == nil then
        # Yes, infinite loop.
        SequencerT.current.sloop do
            this.seach(&ruby_block)
        end
    else
        # Finite loop.
        (0..(n-1)).seach do
            this.seach(&ruby_block)
        end
    end
end

#sdrop(n) ⇒ Object

HW implementation of the Ruby drop.



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
# File 'lib/HDLRuby/std/sequencer.rb', line 538

def sdrop(n)
    # Generate the vector to put the result in.
    # The declares the resulting vector and index.
    res = nil
    idx = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        # res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"drop_vec"))
        res = enum.type[-enum.size+n].inner(HDLRuby.uniq_name(:"drop_vec"))
        # idx = [enum.size.width].inner(HDLRuby.uniq_name(:"drop_idx"))
    end
    # And do the iteration.
    # idx <= 0
    # enum.seach.with_index do |elem,i|
    #     HDLRuby::High.top_user.hif(i >= n) do
    #         res[idx] <= elem
    #         idx <= idx + 1
    #     end
    # end
    # SequencerT.current.swhile(idx < enum.size) do
    #     res[idx] <= 0
    #     idx <= idx + 1
    # end
    (enum.size-n).stimes do |i|
        res[i] <= enum.access(i+n)
    end
    # Return the resulting vector.
    return res
end

#sdrop_while(&ruby_block) ⇒ Object

HW implementation of the Ruby drop_while.



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
# File 'lib/HDLRuby/std/sequencer.rb', line 569

def sdrop_while(&ruby_block)
    # No block given? Generate a new wrapper enumerator for sdrop_while.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:sdrop_while)
    end
    # A block is given.
    # Generate the vector to put the result in.
    # The declares the resulting vector, index and drop flag.
    res = nil
    idx = nil
    flg = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"drop_vec"))
        idx = [enum.size.width].inner(HDLRuby.uniq_name(:"drop_idx"))
        flg = bit.inner(HDLRuby.uniq_name(:"drop_flg"))
    end
    # And do the iteration.
    # First drop and fill from current enumerable elements.
    idx <= 0
    flg <= 1
    enum.seach.with_index do |elem,i|
        HDLRuby::High.top_user.hif(flg == 1) do
            HDLRuby::High.top_user.hif(ruby_block.call(elem) == 0) do
                flg <= 0
            end
        end
        HDLRuby::High.top_user.hif(flg == 0) do
            res[idx] <= elem
            idx <= idx + 1
        end
    end
    # Finally, end with zeros.
    SequencerT.current.swhile(idx < enum.size) do
        res[idx] <= 0
        idx <= idx + 1
    end
    # Return the resulting vector.
    return res
end

#seach_cons(n, &ruby_block) ⇒ Object

HW implementation of the Ruby each_cons



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
# File 'lib/HDLRuby/std/sequencer.rb', line 611

def seach_cons(n,&ruby_block)
    # No block given? Generate a new wrapper enumerator for seach_cons.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:seach_cons)
    end
    # A block is given.
    # Declares the indexes and the buffer for cosecutive elements.
    enum = self.seach
    idx  = nil
    buf  = nil
    HDLRuby::High.cur_system.open do
        idx = [enum.size.width].inner(HDLRuby.uniq_name(:"each_cons_idx"))
        buf = n.times.map do |i|
            [enum.type].inner(HDLRuby.uniq_name(:"each_cons_buf#{i}"))
        end
    end
    # And do the iteration.
    this = self
    # Initialize the buffer.
    n.times do |i|
        buf[i] <= enum.access(i)
        SequencerT.current.step
    end
    # Do the first iteration.
    ruby_block.call(*buf)
    # Do the remaining iteration.
    idx <= n
    SequencerT.current.swhile(idx < enum.size) do
        # Shifts the buffer (in parallel)
        buf.each_cons(2) { |a0,a1| a0 <= a1 }
        # Adds the new element.
        buf[-1] <= enum.access(idx)
        idx <= idx + 1
        # Executes the block.
        ruby_block.call(*buf)
    end
end

#seach_entry(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby each_entry. NOTE: to do, or may be not.



651
652
653
# File 'lib/HDLRuby/std/sequencer.rb', line 651

def seach_entry(*args,&ruby_block)
    raise "seach_entry is not supported yet."
end

#seach_slice(n, &ruby_block) ⇒ Object

HW implementation of the Ruby each_slice



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/HDLRuby/std/sequencer.rb', line 656

def seach_slice(n,&ruby_block)
    # No block given? Generate a new wrapper enumerator for seach_slice.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:seach_slice)
    end
    # A block is given.
    # Declares the indexes and the buffer for consecutive elements.
    enum = self.seach
    idx  = nil
    buf  = nil
    HDLRuby::High.cur_system.open do
        idx = [(enum.size+n).width].inner(HDLRuby.uniq_name(:"each_slice_idx"))
        buf = n.times.map do |i|
            [enum.type].inner(HDLRuby.uniq_name(:"each_slice_buf#{i}"))
        end
    end
    # And do the iteration.
    this = self
    # Adjust n if too large.
    n = enum.size if n > enum.size
    # Initialize the buffer.
    n.times do |i|
        buf[i] <= enum.access(i)
        SequencerT.current.step
    end
    # Do the first iteration.
    ruby_block.call(*buf)
    # Do the remaining iteration.
    idx <= n
    SequencerT.current.swhile(idx < enum.size) do
        # Gets the new element.
        n.times do |i|
            sif(idx+i < enum.size) do
                buf[i] <= enum.access(idx+i)
            end
            selse do
                buf[i] <= 0
            end
        end
        idx <= idx + n
        # Executes the block.
        ruby_block.call(*buf)
    end
end

#seach_with_index(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby each_with_index.



702
703
704
# File 'lib/HDLRuby/std/sequencer.rb', line 702

def seach_with_index(*args,&ruby_block)
    self.seach.with_index(*args,&ruby_block)
end

#seach_with_object(obj, &ruby_block) ⇒ Object

HW implementation of the Ruby each_with_object.



707
708
709
# File 'lib/HDLRuby/std/sequencer.rb', line 707

def seach_with_object(obj,&ruby_block)
    self.seach.with_object(obj,&ruby_block)
end

#sfind(if_none_proc, &ruby_block) ⇒ Object

HW implementation of the Ruby find. NOTE: contrary to Ruby, if_none_proc is mandatory since there is no nil in HW. Moreover, the argument can also be a value.



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
# File 'lib/HDLRuby/std/sequencer.rb', line 503

def sfind(if_none_proc, &ruby_block)
    # No block given? Generate a new wrapper enumerator for sfind.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:sfind,if_none_proc)
    end
    # Generate the found result signal and flag signals.
    found = nil
    flag = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        found = enum.type.inner(HDLRuby.uniq_name(:"find_found"))
        flag = bit.inner(HDLRuby.uniq_name(:"find_flag"))
    end
    # Look for the element.
    flag <= 0
    enum.srewind
    SequencerT.current.swhile((flag == 0) & (enum.snext?)) do
        found <= enum.snext
        hif(ruby_block.call(found)) do
            # Found, save the element and raise the flag.
            flag <= 1
        end
    end
    HDLRuby::High.top_user.hif(~flag) do
        # Not found, execute the none block.
        if if_none_proc.respond_to?(:call) then
            found <= f_none_proc.call
        else
            found <= if_none_proc
        end
    end
    found
end

#sfind_index(obj = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby find_index.



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/HDLRuby/std/sequencer.rb', line 761

def sfind_index(obj = nil, &ruby_block)
    # No block given nor obj? Generate a new wrapper enumerator for
    # sfind.
    if !ruby_block && !obj then
        return SEnumeratorWrapper.new(self,:sfind,if_none_proc)
    end
    # Generate the index result signal and flag signals.
    idx  = nil
    flag = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        idx = signed[enum.size.width+1].inner(HDLRuby.uniq_name(:"find_idx"))
        flag = bit.inner(HDLRuby.uniq_name(:"find_flag"))
    end
    # Look for the element.
    flag <= 0
    idx <= 0
    enum.srewind
    SequencerT.current.swhile((flag == 0) & (enum.snext?)) do
        if (obj) then
            # There is obj case.
            HDLRuby::High.top_user.hif(enum.snext == obj) do
                # Found, save the element and raise the flag.
                flag <= 1
            end
        else
            # There is a block case.
            HDLRuby::High.top_user.hif(ruby_block.call(enum.snext)) do
                # Found, save the element and raise the flag.
                flag <= 1
            end
        end
        HDLRuby::High.top_user.helse do
            idx <= idx + 1
        end
    end
    HDLRuby::High.top_user.hif(flag ==0) { idx <= -1 }
    return idx
end

#sfirst(n = 1) ⇒ Object

HW implementation of the Ruby first.



802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/HDLRuby/std/sequencer.rb', line 802

def sfirst(n=1)
    # Generate the vector to put the result in.
    # The declares the resulting vector and index.
    res = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[-n].inner(HDLRuby.uniq_name(:"first_vec"))
    end
    # And do the iteration.
    n.stimes do |i|
        res[i] <= enum.access(i)
    end
    # Return the resulting vector.
    return res
end

#sflat_map(&ruby_block) ⇒ Object

HW implementation of the Ruby flat_map. NOTE: actually due to the way HDLRuby handles vectors, should work like smap



416
417
418
# File 'lib/HDLRuby/std/sequencer.rb', line 416

def sflat_map(&ruby_block)
    return smap(&ruby_block)
end

#sgrep(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby grep. NOTE: to do, or may be not.



820
821
822
# File 'lib/HDLRuby/std/sequencer.rb', line 820

def sgrep(*args,&ruby_block)
    raise "sgrep is not supported yet."
end

#sgrep_v(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby grep_v. NOTE: to do, or may be not.



826
827
828
# File 'lib/HDLRuby/std/sequencer.rb', line 826

def sgrep_v(*args,&ruby_block)
    raise "sgrep_v is not supported yet."
end

#sgroup_by(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby group_by. NOTE: to do, or may be not.



832
833
834
# File 'lib/HDLRuby/std/sequencer.rb', line 832

def sgroup_by(*args,&ruby_block)
    raise "sgroup_by is not supported yet."
end

#sinclude?(obj) ⇒ Boolean

HW implementation of the Ruby include?

Returns:

  • (Boolean)


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'lib/HDLRuby/std/sequencer.rb', line 837

def sinclude?(obj)
    # Generate the result signal.
    res  = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = bit.inner(HDLRuby.uniq_name(:"include_res"))
    end
    # Look for the element.
    res <= 0
    enum.srewind
    SequencerT.current.swhile((res == 0) & (enum.snext?)) do
        # There is obj case.
        HDLRuby::High.top_user.hif(enum.snext == obj) do
            # Found, save the element and raise the flag.
            res <= 1
        end
    end
    return res
end

#sinject(*args, &ruby_block) ⇒ Object Also known as: sreduce

HW implementation of the Ruby inject.



858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/HDLRuby/std/sequencer.rb', line 858

def sinject(*args,&ruby_block)
    init = nil
    symbol = nil
    # Process the arguments.
    if args.size > 2 then
        raise ArgumentError.new("wrong number of arguments (given #{args.size} expected 0..2)")
    elsif args.size == 2 then
        # Initial value and symbol given case.
        init, symbol = args
    elsif args.size == 1 && ruby_block then
        # Initial value and block given case.
        init = args[0]
    elsif args.size == 1 then
        # Symbol given case.
        symbol = args[0]
    end
    enum = self.seach
    # Define the computation type: from the initial value if any,
    # otherwise from the enum.
    typ = init ? init.to_expr.type : enum.type
    # Generate the result signal.
    res  = nil
    HDLRuby::High.cur_system.open do
        res = typ.inner(HDLRuby.uniq_name(:"inject_res"))
    end
    # Start the initialization
    enum.srewind
    # Is there an initial value?
    if (init) then
        # Yes, start with it.
        res <= init
    else
        # No, start with the first element of the enumerator.
        res <= 0
        SequencerT.current.sif(!enum.snext?) { res <= enum.snext }
    end
    SequencerT.current.swhile(enum.snext?) do
        # Do the accumulation.
        if (symbol) then
            res <= res.send(symbol,enum.snext)
        else
            res <= ruby_block.call(res,enum.snext)
        end
    end
    return res
end

#slazy(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby lazy. NOTE: to do, or may be not.



909
910
911
# File 'lib/HDLRuby/std/sequencer.rb', line 909

def slazy(*args,&ruby_block)
    raise "slazy is not supported yet."
end

#smap(&ruby_block) ⇒ Object

Returns a vector containing the execution result of the given block on each element. If no block is given, return an SEnumerator. NOTE: be carful that the resulting vector can become huge if there are many element.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/HDLRuby/std/sequencer.rb', line 392

def smap(&ruby_block)
    # No block given? Generate a new wrapper enumerator for smap.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:smap)
    end
    # A block given? Fill the vector it with the computation result.
    # Generate the vector to put the result in.
    # The declares the resulting vector.
    res = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"map_vec"))
    end
    # And do the iteration.
    enum.with_index do |elem,idx|
        res[idx] <= ruby_block.call(elem)
    end
    # Return the resulting vector.
    return res
end

#smax(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby max.



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'lib/HDLRuby/std/sequencer.rb', line 914

def smax(n = nil, &ruby_block)
    # Process the arguments.
    n = 1 unless n
    enum = self.seach
    # Declare the result signal the flag and the result array size index
    # used for implementing the algorithm (shift-based sorting) in
    # case of multiple max.
    res  = nil
    flg = nil
    idx = nil
    HDLRuby::High.cur_system.open do
        if n == 1 then
            res = enum.type.inner(HDLRuby.uniq_name(:"max_res"))
            # No flg nor idx!
        else
            res = enum.type[-n].inner(HDLRuby.uniq_name(:"max_res"))
            flg = bit.inner(HDLRuby.uniq_name(:"max_flg"))
            idx = bit[n.width].inner(HDLRuby.uniq_name(:"max_idx"))
        end
    end
    enum.srewind
    if n == 1 then
        # Single max case, initialize res with the first element(s)
        res <= enum.type.min
        SequencerT.current.sif(enum.snext?) { res <= enum.snext }
    else
        # Multiple max case, initialize the resulting array size index.
        idx <= 0
    end
    # Do the iteration.
    SequencerT.current.swhile(enum.snext?) do
        if n == 1 then
            # Single max case.
            elem = enum.snext
            if ruby_block then
                hif(ruby_block.call(res,elem) < 0) { res <= elem }
            else
                hif(res < elem) { res <= elem }
            end
        else
            # Multiple max case.
            SequencerT.current.sif(enum.snext?) do
                elem = enum.snext
                flg <= 1
                n.times do |i|
                    # Compute the comparison between the result element
                    # at i and the enum element.
                    if ruby_block then
                        cond = ruby_block.call(res[i],elem) < 0
                    else
                        cond = res[i] < elem
                    end
                    # If flg is 0, elem is already set as max, skip.
                    # If the result array size index is equal to i, then
                    # put the element whatever the comparison is since
                    # the place is still empty.
                    hif(flg & (cond | (idx == i))) do
                        # A new max is found, shift res from i.
                        ((i+1)..(n-1)).reverse_each { |j| res[j] <= res[j-1] }
                        # An set the new max in current position.
                        res[i] <= elem
                        # For now skip.
                        flg <= 0
                    end
                end
                # Note: when idx >= n, the resulting array is full
                hif(idx < n) { idx <= idx + 1 }
            end
        end
    end
    return res
end

#smax_by(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby max_by.



988
989
990
991
992
993
994
995
996
# File 'lib/HDLRuby/std/sequencer.rb', line 988

def smax_by(n = nil, &ruby_block)
    # No block given? Generate a new wrapper enumerator for smax_by.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:smax_by,n)
    end
    # A block is given, use smax with a proc that applies ruby_block
    # before comparing.
    return smax(n) { |a,b| ruby_block.call(a) <=> ruby_block.call(b) }
end

#smin(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby min.



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/HDLRuby/std/sequencer.rb', line 999

def smin(n = nil, &ruby_block)
    # Process the arguments.
    n = 1 unless n
    enum = self.seach
    # Declare the result signal the flag and the result array size index
    # used for implementing the algorithm (shift-based sorting) in
    # case of multiple min.
    res  = nil
    flg = nil
    idx = nil
    HDLRuby::High.cur_system.open do
        if n == 1 then
            res = enum.type.inner(HDLRuby.uniq_name(:"min_res"))
            # No flg nor idx!
        else
            res = enum.type[-n].inner(HDLRuby.uniq_name(:"min_res"))
            flg = bit.inner(HDLRuby.uniq_name(:"min_flg"))
            idx = bit[n.width].inner(HDLRuby.uniq_name(:"min_idx"))
        end
    end
    enum.srewind
    if n == 1 then
        # Single min case, initialize res with the first element(s)
        res <= enum.type.max
        SequencerT.current.sif(enum.snext?) { res <= enum.snext }
    else
        # Multiple min case, initialize the resulting array size index.
        idx <= 0
    end
    # Do the iteration.
    SequencerT.current.swhile(enum.snext?) do
        if n == 1 then
            # Single min case.
            elem = enum.snext
            if ruby_block then
                hif(ruby_block.call(res,elem) > 0) { res <= elem }
            else
                hif(res > elem) { res <= elem }
            end
        else
            # Multiple min case.
            SequencerT.current.sif(enum.snext?) do
                elem = enum.snext
                flg <= 1
                n.times do |i|
                    # Compute the comparison between the result element
                    # at i and the enum element.
                    if ruby_block then
                        cond = ruby_block.call(res[i],elem) > 0
                    else
                        cond = res[i] > elem
                    end
                    # If flg is 0, elem is already set as min, skip.
                    # If the result array size index is equal to i, then
                    # put the element whatever the comparison is since
                    # the place is still empty.
                    hif(flg & (cond | (idx == i))) do
                        # A new min is found, shift res from i.
                        ((i+1)..(n-1)).reverse_each { |j| res[j] <= res[j-1] }
                        # An set the new min in current position.
                        res[i] <= elem
                        # For now skip.
                        flg <= 0
                    end
                end
                # Note: when idx >= n, the resulting array is full
                hif(idx < n) { idx <= idx + 1 }
            end
        end
    end
    return res
end

#smin_by(n = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby min_by.



1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/HDLRuby/std/sequencer.rb', line 1073

def smin_by(n = nil, &ruby_block)
    # No block given? Generate a new wrapper enumerator for smin_by.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:smin_by,n)
    end
    # A block is given, use smin with a proc that applies ruby_block
    # before comparing.
    return smin(n) { |a,b| ruby_block.call(a) <=> ruby_block.call(b) }
end

#sminmax(&ruby_block) ⇒ Object

HW implementation of the Ruby minmax.



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/HDLRuby/std/sequencer.rb', line 1084

def sminmax(&ruby_block)
    # Generate the result signal.
    res  = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[2].inner(HDLRuby.uniq_name(:"minmax_res"))
    end
    # Computes the min.
    res[0] <= enum.smin(&ruby_block)
    # Computes the max.
    res[1] <= enum.smax(&ruby_block)
    # Return the result.
    return res
end

#sminmax_by(&ruby_block) ⇒ Object

HW implementation of the Ruby minmax_by.



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
# File 'lib/HDLRuby/std/sequencer.rb', line 1100

def sminmax_by(&ruby_block)
    # Generate the result signal.
    res  = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[2].inner(HDLRuby.uniq_name(:"minmax_res"))
    end
    # Computes the min.
    res[0] <= enum.smin_by(&ruby_block)
    # Computes the max.
    res[1] <= enum.smax_by(&ruby_block)
    # Return the result.
    return res
end

#snone?(arg = nil, &ruby_block) ⇒ Boolean

Tell if none of the elements respects a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
# File 'lib/HDLRuby/std/sequencer.rb', line 1117

def snone?(arg = nil,&ruby_block)
    # Declare the result signal.
    res = nil
    HDLRuby::High.cur_system.open do
        res = bit.inner(HDLRuby.uniq_name(:"none_cond"))
    end
    # Initialize the result.
    res <= 1
    # Performs the computation.
    if arg then
        # Compare elements to arg.
        self.seach do |elem|
            res <= res & (elem != arg)
        end
    elsif ruby_block then
        # Use the ruby block.
        self.seach do |elem|
            res <= res & ~ruby_block.call(elem)
        end
    else
        raise "Ruby nil does not have any meaning in HW."
    end
    res
end

#sone?(arg = nil, &ruby_block) ⇒ Boolean

Tell if one and only one of the elements respects a given criterion given either as +arg+ or as block.

Returns:

  • (Boolean)


1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
# File 'lib/HDLRuby/std/sequencer.rb', line 1144

def sone?(arg = nil,&ruby_block)
    # Declare the result signal.
    res = nil
    HDLRuby::High.cur_system.open do
        res = bit.inner(HDLRuby.uniq_name(:"one_cond"))
    end
    # Initialize the result.
    res <= 0
    # Performs the computation.
    if arg then
        # Compare elements to arg.
        self.seach do |elem|
            res <= res ^ (elem == arg)
        end
    elsif ruby_block then
        # Use the ruby block.
        self.seach do |elem|
            res <= res ^ ruby_block.call(elem)
        end
    else
        raise "Ruby nil does not have any meaning in HW."
    end
    res
end

#spartition(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby partition. NOTE: to do, or may be not.



1171
1172
1173
# File 'lib/HDLRuby/std/sequencer.rb', line 1171

def spartition(*args,&ruby_block)
    raise "spartition is not supported yet."
end

#sreject(&ruby_block) ⇒ Object

HW implementatiob of the Ruby reject.



1176
1177
1178
# File 'lib/HDLRuby/std/sequencer.rb', line 1176

def sreject(&ruby_block)
    return sselect {|elem| ~ruby_block.call(elem) }
end

#sreverse_each(*args, &ruby_block) ⇒ Object

HW implementatiob of the Ruby reverse_each.



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
# File 'lib/HDLRuby/std/sequencer.rb', line 1181

def sreverse_each(*args,&ruby_block)
    # No block given? Generate a new wrapper enumerator for 
    # sreverse_each.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:sreverse_each,*args)
    end
    # A block is given.
    # Declares the index.
    enum = self.seach
    idx = nil
    HDLRuby::High.cur_system.open do
        idx = bit[enum.size.width].inner(HDLRuby.uniq_name(:"reverse_idx"))
    end
    # Do the iteration.
    idx <= enum.size
    SequencerT.current.swhile(idx > 0) do
        idx <= idx - 1
        ruby_block.call(*args,enum.access(idx))
    end
end

#sselect(&ruby_block) ⇒ Object

HW implementation of the Ruby select.



727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/HDLRuby/std/sequencer.rb', line 727

def sselect(&ruby_block)
    # No block given? Generate a new wrapper enumerator for sselect.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:sselect)
    end
    # A block is given.
    # Generate the vector to put the result in.
    # The declares the resulting vector and index.
    res = nil
    idx = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"select_vec"))
        idx = [enum.size.width].inner(HDLRuby.uniq_name(:"select_idx"))
    end
    # And do the iteration.
    # First select and fill from current enumerable elements.
    idx <= 0
    enum.seach do |elem|
        HDLRuby::High.top_user.hif(ruby_block.call(elem) == 1) do
            res[idx] <= elem
            idx <= idx + 1
        end
    end
    # Finally, end with zeros.
    SequencerT.current.swhile(idx < enum.size) do
        res[idx] <= 0
        idx <= idx + 1
    end
    # Return the resulting vector.
    return res
end

#sslice_after(pattern = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby slice_after. NOTE: to do, or may be not.



1204
1205
1206
# File 'lib/HDLRuby/std/sequencer.rb', line 1204

def sslice_after(pattern = nil,&ruby_block)
    raise "sslice_after is not supported yet."
end

#sslice_before(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby slice_before. NOTE: to do, or may be not.



1210
1211
1212
# File 'lib/HDLRuby/std/sequencer.rb', line 1210

def sslice_before(*args,&ruby_block)
    raise "sslice_before is not supported yet."
end

#sslice_when(*args, &ruby_block) ⇒ Object

HW implementation of the Ruby slice_when. NOTE: to do, or may be not.



1216
1217
1218
# File 'lib/HDLRuby/std/sequencer.rb', line 1216

def sslice_when(*args,&ruby_block)
    raise "sslice_before is not supported yet."
end

#ssort(&ruby_block) ⇒ Object

HW implementation of the Ruby sort.



1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
# File 'lib/HDLRuby/std/sequencer.rb', line 1221

def ssort(&ruby_block)
    enum = self.seach
    n = enum.size
    # Declare the result signal the flag and the result array size index
    # used for implementing the algorithm (shift-based sorting).
    res = nil
    flg = nil
    idx = nil
    HDLRuby::High.cur_system.open do
        res = enum.type[-n].inner(HDLRuby.uniq_name(:"sort_res"))
        flg = bit.inner(HDLRuby.uniq_name(:"sort_flg"))
        idx = bit[n.width].inner(HDLRuby.uniq_name(:"sort_idx"))
    end
    # Performs the sort using a shift-based algorithm (also used in 
    # smin).
    enum.srewind
    # Do the iteration.
    idx <= 0
    SequencerT.current.swhile(enum.snext?) do
        # Multiple min case.
        SequencerT.current.sif(enum.snext?) do
            elem = enum.snext
            flg <= 1
            n.times do |i|
                # Compute the comparison between the result element at i
                # and the enum element.
                if ruby_block then
                    cond = ruby_block.call(res[i],elem) > 0
                else
                    cond = res[i] > elem
                end
                # If flg is 0, elem is already set as min, skip.
                # If the result array size index is equal to i, then
                # put the element whatever the comparison is since the
                # place is still empty.
                hif(flg & (cond | (idx == i))) do
                    # A new min is found, shift res from i.
                    ((i+1)..(n-1)).reverse_each { |j| res[j] <= res[j-1] }
                    # An set the new min in current position.
                    res[i] <= elem
                    # For now skip.
                    flg <= 0
                end
            end
            idx <= idx + 1
        end
    end
    return res
end

#ssort_by(&ruby_block) ⇒ Object

HW implementation of the Ruby sort.



1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/HDLRuby/std/sequencer.rb', line 1272

def ssort_by(&ruby_block)
    # No block given? Generate a new wrapper enumerator for smin_by.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:ssort_by,n)
    end
    # A block is given, use smin with a proc that applies ruby_block
    # before comparing.
    return ssort { |a,b| ruby_block.call(a) <=> ruby_block.call(b) }
end

#ssum(initial_value = nil, &ruby_block) ⇒ Object

HW implementation of the Ruby sum.



1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
# File 'lib/HDLRuby/std/sequencer.rb', line 1283

def ssum(initial_value = nil,&ruby_block)
    enum = self.seach
    # Define the computation type: from the initial value if any,
    # otherwise from the enum.
    typ = initial_value ? initial_value.to_expr.type : enum.type
    # Ensures there is an initial value.
    initial_value = 0.to_expr.as(typ) unless initial_value
    # Generate the result signal.
    res  = nil
    HDLRuby::High.cur_system.open do
        res = typ.inner(HDLRuby.uniq_name(:"sum_res"))
    end
    # Start the initialization
    enum.srewind
    # Yes, start with the initial value.
    res <= initial_value
    SequencerT.current.swhile(enum.snext?) do
        # Do the accumulation.
        if (ruby_block) then
            # There is a ruby block, use it to process the element first.
            res <= res + ruby_block.call(enum.snext)
        else
            # No ruby block, just do the su,
            res <= res + enum.snext
        end
    end
    return res
end

#stake(n) ⇒ Object

The HW implementation of the Ruby take.



1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
# File 'lib/HDLRuby/std/sequencer.rb', line 1313

def stake(n)
    enum = self.seach
    # Generate the result signal.
    res  = nil
    HDLRuby::High.cur_system.open do
        res = enum.type[-n].inner(HDLRuby.uniq_name(:"sum_res"))
    end
    # Take the n first elements.
    n.stimes do |i|
        res[i] <= enum.access(i)
    end
    return res
end

#stake_while(&ruby_block) ⇒ Object

The HW implementation of the Ruby take_while.



1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
# File 'lib/HDLRuby/std/sequencer.rb', line 1328

def stake_while(&ruby_block)
    # No block given? Generate a new wrapper enumerator for sdrop_while.
    if !ruby_block then
        return SEnumeratorWrapper.new(self,:stake_while)
    end
    # A block is given.
    # Generate the vector to put the result in.
    # The declares the resulting vector and take flag.
    res = nil
    flg = nil
    enum = self.seach
    HDLRuby::High.cur_system.open do
        res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"take_vec"))
        flg = bit.inner(HDLRuby.uniq_name(:"take_flg"))
    end
    # And do the iteration.
    # First fill from current enumerable elements.
    flg <= 1
    enum.seach.with_index do |elem,i|
        HDLRuby::High.top_user.hif(flg == 1) do
            HDLRuby::High.top_user.hif(ruby_block.call(elem) == 0) do
                flg <= 0
            end
        end
        HDLRuby::High.top_user.hif(flg == 1) do
            res[i] <= elem
        end
        HDLRuby::High.top_user.helse do
            res[i] <= 0
        end
    end
    # Return the resulting vector.
    return res
end

#stally(h = nil) ⇒ Object

HW implementation of the Ruby tally. NOTE: to do, or may be not.



1365
1366
1367
# File 'lib/HDLRuby/std/sequencer.rb', line 1365

def stally(h = nil)
    raise "stally is not supported yet."
end

#sto_aObject

HW implementation of the Ruby to_a.



712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/HDLRuby/std/sequencer.rb', line 712

def sto_a
    # Declares the resulting vector.
    enum = self.seach
    res  = nil
    HDLRuby::High.cur_system.open do
        res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"to_a_res"))
    end
    # Fills it.
    self.seach_with_index do |elem,i|
        res[i] <= elem
    end
    return res
end

#sto_h(h = nil) ⇒ Object

HW implementation of the Ruby to_h. NOTE: to do, or may be not.



1371
1372
1373
# File 'lib/HDLRuby/std/sequencer.rb', line 1371

def sto_h(h = nil)
    raise "sto_h is not supported yet."
end

#suniq(&ruby_block) ⇒ Object

HW implementation of the Ruby uniq.



1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
# File 'lib/HDLRuby/std/sequencer.rb', line 1376

def suniq(&ruby_block)
    enum = self.seach
    n = enum.size
    # Declare the result signal the flag and the result array size index
    # used for implementing the algorithm (shift-based sorting).
    res = nil
    flg = nil
    idx = nil
    HDLRuby::High.cur_system.open do
        res = enum.type[-n].inner(HDLRuby.uniq_name(:"suniq_res"))
        flg = bit.inner(HDLRuby.uniq_name(:"suniq_flg"))
        idx = bit[n.width].inner(HDLRuby.uniq_name(:"suniq_idx"))
    end
    enum.srewind
    # Do the iteration.
    idx <= 0
    SequencerT.current.swhile(enum.snext?) do
        # Multiple min case.
        SequencerT.current.sif(enum.snext?) do
            elem = enum.snext
            flg <= 1
            n.times do |i|
                # Compute the comparison between the result element at i
                # and the enum element.
                hif(i < idx) do
                    if ruby_block then
                        flg <= (flg & 
                                (ruby_block.call(res[i]) != ruby_block.call(elem)))
                    else
                        flg <= (flg & (res[i] != elem))
                    end
                end
                # If flg is 1 the element is new, if it is the right
                # position, add it to the result.
                hif((idx == i) & flg) do
                    # An set the new min in current position.
                    res[i] <= elem
                    # For next position now.
                    idx <= idx + 1
                    # Stop here for current element.
                    flg <= 0
                end
            end
        end
    end
    # Fills the remaining location with 0.
    SequencerT.current.swhile(idx < enum.size) do
        res[idx] <= 0
        idx <= idx + 1
    end
    return res
end

#szip(obj, &ruby_block) ⇒ Object

HW implementation of the Ruby zip. NOTE: for now szip is deactivated untile tuples are properly handled by HDLRuby.



1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
# File 'lib/HDLRuby/std/sequencer.rb', line 1432

def szip(obj,&ruby_block)
    res = nil
    l0,r0,l1,r1 = nil,nil,nil,nil
    idx = nil
    enum0 = self.seach
    enum1 = obj.seach
    # Compute the minimal and maximal iteration sizes of both
    # enumerables.
    size_min = [enum0.size,enum1.size].min
    size_max = [enum0.size,enum1.size].max
    HDLRuby::High.cur_system.open do
        # If there is no ruby_block, szip generates a resulting vector
        # and its access indexes.
        unless ruby_block then
            res = bit[enum0.type.width+enum1.type.width][-size_max].inner(HDLRuby.uniq_name(:"zip_res"))
            l0 = enum0.type.width+enum1.type.width - 1
            r0 = enum1.type.width
            l1 = r0-1
            r1 = 0
        end
        # Generate the index.
        idx = [size_max.width].inner(HDLRuby.uniq_name(:"zip_idx"))
    end
    # Do the iteration.
    enum0.srewind
    enum1.srewind
    # As long as there is enough elements.
    idx <= 0
    SequencerT.current.swhile(idx < size_min) do
        # Generate the access to the elements.
        elem0 = enum0.snext
        elem1 = enum1.snext
        if ruby_block then
            # A ruby block is given, applies it directly on the elements.
            ruby_block.call(elem0,elem1)
        else
            # No ruby block, put the access results into res.
            # res[idx][l0..r0] <= elem0
            # res[idx][l1..r1] <= elem1
            res[idx] <= [elem0,elem1]
        end
        idx <= idx + 1
    end
    # For the remaining iteration use zeros for the smaller enumerable.
    SequencerT.current.swhile(idx < size_max) do
        # Generate the access to the elements.
        elem0 = enum0.size < size_max ? 0 : enum0.snext
        elem1 = enum1.size < size_max ? 0 : enum1.snext
        if ruby_block then
            # A ruby block is given, applies it directly on the elements.
            ruby_block.call(elem0,elem1)
        else
            # No ruby block, put the access results into res.
            # res[idx][l0..r0] <= elem0
            # res[idx][l1..r1] <= elem1
            res[idx] <= [elem0,elem1]
        end
        idx <= idx + 1
    end
    unless ruby_block then
        return res
    end
end