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

Included in:
AnyRange, 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)


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

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)


417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/HDLRuby/std/sequencer.rb', line 417

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+



443
444
445
# File 'lib/HDLRuby/std/sequencer.rb', line 443

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.



449
450
451
# File 'lib/HDLRuby/std/sequencer.rb', line 449

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.



455
456
457
# File 'lib/HDLRuby/std/sequencer.rb', line 455

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).



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

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.



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

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.



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/HDLRuby/std/sequencer.rb', line 551

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.



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

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.



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
671
672
673
674
675
676
677
678
679
# File 'lib/HDLRuby/std/sequencer.rb', line 640

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



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/HDLRuby/std/sequencer.rb', line 682

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.



722
723
724
# File 'lib/HDLRuby/std/sequencer.rb', line 722

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

#seach_nexts(num, &ruby_block) ⇒ Object

Iterator on the +num+ next elements. NOTE:

  • Stop iteration when the end of the range is reached or when there are no elements left
  • This is not a method from Ruby but one specific for hardware where creating a array is very expensive.


1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
# File 'lib/HDLRuby/std/sequencer.rb', line 1695

def seach_nexts(num,&ruby_block)
    # # No block given, returns a new enumerator.
    # unless ruby_block then
    #     res = SEnumeratorWrapper.new(self,:seach_nexts,num)
    #     res.size = num
    #     return res
    # end
    # # A block is given, iterate.
    # enum = self.seach
    # # Create a counter. 
    # count = nil
    # zero = nil
    # one = nil
    # HDLRuby::High.cur_system.open do
    #     if num.respond_to?(:width) then
    #         count = [num.width].inner(HDLRuby.uniq_name(:"snexts_count"))
    #     else
    #         count = num.to_expr.type.inner(HDLRuby.uniq_name(:"snexts_count"))
    #     end
    #     zero = _b0
    #     one  = _b1
    # end
    # count <= num
    # SequencerT.current.swhile(count > zero) do
    #     ruby_block.call(enum.snext)
    #     count <= count - one
    # end
    zero = nil
    one = nil
    HDLRuby::High.cur_system.open do
        zero = _b0.as(num.to_expr.type)
        one = _b1.as(num.to_expr.type)
    end
    subE = SEnumeratorSub.new(self,zero..num-one)
    if ruby_block then
        # A block is given, iterate immediatly.
        subE.seach(&ruby_block)
    else
        # No block given, return the new sub iterator.
        return subE
    end
end

#seach_range(rng, &ruby_block) ⇒ Object

Iterator on each of the elements in range +rng+. NOTE:

  • Stop iteration when the end of the range is reached or when there are no elements left
  • This is not a method from Ruby but one specific for hardware where creating a array is very expensive.


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

def seach_range(rng,&ruby_block)
    return self.seach.seach_range(rng,&ruby_block)
end

#seach_slice(n, &ruby_block) ⇒ Object

HW implementation of the Ruby each_slice



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
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/HDLRuby/std/sequencer.rb', line 727

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.



773
774
775
# File 'lib/HDLRuby/std/sequencer.rb', line 773

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.



778
779
780
# File 'lib/HDLRuby/std/sequencer.rb', line 778

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.



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

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.



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/HDLRuby/std/sequencer.rb', line 834

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.



875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/HDLRuby/std/sequencer.rb', line 875

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



487
488
489
# File 'lib/HDLRuby/std/sequencer.rb', line 487

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.



893
894
895
# File 'lib/HDLRuby/std/sequencer.rb', line 893

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.



899
900
901
# File 'lib/HDLRuby/std/sequencer.rb', line 899

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.



905
906
907
# File 'lib/HDLRuby/std/sequencer.rb', line 905

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)


910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
# File 'lib/HDLRuby/std/sequencer.rb', line 910

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.



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

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.



982
983
984
# File 'lib/HDLRuby/std/sequencer.rb', line 982

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.



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/HDLRuby/std/sequencer.rb', line 463

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.



987
988
989
990
991
992
993
994
995
996
997
998
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
# File 'lib/HDLRuby/std/sequencer.rb', line 987

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.



1061
1062
1063
1064
1065
1066
1067
1068
1069
# File 'lib/HDLRuby/std/sequencer.rb', line 1061

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.



1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'lib/HDLRuby/std/sequencer.rb', line 1072

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.



1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/HDLRuby/std/sequencer.rb', line 1146

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.



1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
# File 'lib/HDLRuby/std/sequencer.rb', line 1157

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.



1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
# File 'lib/HDLRuby/std/sequencer.rb', line 1173

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)


1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'lib/HDLRuby/std/sequencer.rb', line 1190

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)


1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
# File 'lib/HDLRuby/std/sequencer.rb', line 1217

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.



1244
1245
1246
# File 'lib/HDLRuby/std/sequencer.rb', line 1244

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

#sreject(&ruby_block) ⇒ Object

HW implementatiob of the Ruby reject.



1249
1250
1251
# File 'lib/HDLRuby/std/sequencer.rb', line 1249

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.



1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'lib/HDLRuby/std/sequencer.rb', line 1254

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.



800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/HDLRuby/std/sequencer.rb', line 800

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.



1277
1278
1279
# File 'lib/HDLRuby/std/sequencer.rb', line 1277

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.



1283
1284
1285
# File 'lib/HDLRuby/std/sequencer.rb', line 1283

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.



1289
1290
1291
# File 'lib/HDLRuby/std/sequencer.rb', line 1289

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

#ssort(&ruby_block) ⇒ Object

HW implementation of the Ruby sort.



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
1428
1429
1430
1431
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
# File 'lib/HDLRuby/std/sequencer.rb', line 1388

def ssort(&ruby_block)
    enum = self.seach
    n = enum.size
    # Declare the result signal.
    res = nil
    flg = nil
    siz = nil
    HDLRuby::High.cur_system.open do
        res = enum.type[-n].inner(HDLRuby.uniq_name(:"sort_res"))
    end
    # Only one element?
    if n == 1 then
        # Just copy to the result and end here.
        res[0] <= enum.snext
        return res
    end
    tmp = []
    idxF = nil; idxM = nil; idxO = nil
    HDLRuby::High.cur_system.open do
        # More elements, need to declare intermediate arrays.
        ((n-1).width).times do
            tmp << enum.type[-n].inner(HDLRuby.uniq_name(:"sort_tmp"))
        end
        # The result will be the last of the intermediate arrays.
        tmp << res
    end
    # Fills the first temporary array.
    enum.seach_with_index { |e,i| tmp[0][i] <= e }
    # Is there only 2 elements?
    if n == 2 then
        if ruby_block then
            cond = ruby_block.call(tmp[0][0],tmp[0][1]) < 0
        else
            cond = tmp[0][0] < tmp[0][1]
        end
        # Just look for the min and the max.
        hif(cond) do
            res[0] <= tmp[0][0]
            res[1] <= tmp[0][1]
        end
        helse do
            res[1] <= tmp[0][0]
            res[0] <= tmp[0][1]
        end
        return res
    end
    # Performs the sort using a merge-based algorithm.
    breadth = 1; i = 0
    # while(breadth*2 < n)
    while(breadth < n)
        pos = 0; last = 0
        while(pos+breadth < n)
            last = [n-1,pos+breadth*2-1].min
            ssort_merge(tmp[i], tmp[i+1], pos, pos+breadth,last,&ruby_block)
            pos = pos + breadth * 2
        end
        # Copy the remaining elements if any
        # puts "n=#{n} breadth=#{breadth} last=#{last} n-last-1=#{n-last-1}"
        if last < n-1 then
            (n-last-1).stimes do |j|
                tmp[i+1][last+1+j] <= tmp[i][last+1+j]
            end
        end
        # Next step
        # SequencerT.current.step
        breadth = breadth * 2
        i += 1
    end
    # # Last merge if the array size was not a power of 2.
    # if (breadth*2 != n) then
    #     ssort_merge(tmp[-2],tmp[-1],0,breadth,n-1,&ruby_block)
    #     # SequencerT.current.step
    # end
    return res
end

#ssort_by(&ruby_block) ⇒ Object

HW implementation of the Ruby sort.



1465
1466
1467
1468
1469
1470
1471
1472
1473
# File 'lib/HDLRuby/std/sequencer.rb', line 1465

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

#ssort_merge(arI, arO, first, middle, last, &ruby_block) ⇒ Object

Merge two arrays in order, for ssort only.



1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
# File 'lib/HDLRuby/std/sequencer.rb', line 1345

def ssort_merge(arI, arO, first, middle, last, &ruby_block)
    # puts "first=#{first} middle=#{middle} last=#{last}"
    # Declare and initialize the indexes and
    # the ending flag.
    idF = nil; idM = nil; idO = nil
    flg = nil
    HDLRuby::High.cur_system.open do
        typ = [(last+1).width]
        idF = typ.inner(HDLRuby.uniq_name(:"sort_idF"))
        idM = typ.inner(HDLRuby.uniq_name(:"sort_idM"))
        idO = typ.inner(HDLRuby.uniq_name(:"sort_idO"))
        flg = inner(HDLRuby.uniq_name(:"sort_flg"))
    end
    idF <= first; idM <= middle; idO <= first
    flg <= 0
    SequencerT.current.swhile((flg == 0) & (idO < middle*2)) do
        if ruby_block then
            cond = ruby_block.call(arI[idF],arI[idM]) < 0
        else
            cond = arI[idF] < arI[idM]
        end
        hif((idF >= middle) & (idM > last)) { flg <= 1 }
        helsif (idF >= middle) do
            arO[idO] <= arI[idM]
            idM <= idM + 1
        end
        helsif(idM > last) do
            arO[idO] <= arI[idF]
            idF <= idF + 1
        end
        helsif(cond) do
            arO[idO] <= arI[idF]
            idF <= idF + 1
        end
        helse do
            arO[idO] <= arI[idM]
            idM <= idM + 1
        end
        idO <= idO + 1
    end
end

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

HW implementation of the Ruby sum.



1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File 'lib/HDLRuby/std/sequencer.rb', line 1476

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 sum
            res <= res + enum.snext
        end
    end
    return res
end

#stake(n) ⇒ Object

The HW implementation of the Ruby take.



1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
# File 'lib/HDLRuby/std/sequencer.rb', line 1506

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.



1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
# File 'lib/HDLRuby/std/sequencer.rb', line 1521

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.



1558
1559
1560
# File 'lib/HDLRuby/std/sequencer.rb', line 1558

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

#sto_aObject

HW implementation of the Ruby to_a.



783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/HDLRuby/std/sequencer.rb', line 783

def sto_a
    # Declares the resulting vector.
    enum = self.seach
    res  = nil
    # size = enum.size.to_value
    HDLRuby::High.cur_system.open do
        # res = enum.type[-enum.size].inner(HDLRuby.uniq_name(:"to_a_res"))
        res = enum.type[-enum.size.to_i].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.



1564
1565
1566
# File 'lib/HDLRuby/std/sequencer.rb', line 1564

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

#suniq(&ruby_block) ⇒ Object

HW implementation of the Ruby uniq.



1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
# File 'lib/HDLRuby/std/sequencer.rb', line 1569

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.



1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
# File 'lib/HDLRuby/std/sequencer.rb', line 1625

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