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)


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/HDLRuby/std/sequencer.rb', line 375

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)


402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/HDLRuby/std/sequencer.rb', line 402

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+



428
429
430
# File 'lib/HDLRuby/std/sequencer.rb', line 428

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.



434
435
436
# File 'lib/HDLRuby/std/sequencer.rb', line 434

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.



440
441
442
# File 'lib/HDLRuby/std/sequencer.rb', line 440

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



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

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.



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

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.



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/HDLRuby/std/sequencer.rb', line 536

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.



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/HDLRuby/std/sequencer.rb', line 594

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.



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/HDLRuby/std/sequencer.rb', line 625

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



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
700
701
702
703
# File 'lib/HDLRuby/std/sequencer.rb', line 667

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.



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

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.


1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
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
# File 'lib/HDLRuby/std/sequencer.rb', line 1680

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.


369
370
371
# File 'lib/HDLRuby/std/sequencer.rb', line 369

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



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
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
# File 'lib/HDLRuby/std/sequencer.rb', line 712

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.



758
759
760
# File 'lib/HDLRuby/std/sequencer.rb', line 758

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.



763
764
765
# File 'lib/HDLRuby/std/sequencer.rb', line 763

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.



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/HDLRuby/std/sequencer.rb', line 559

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.



819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'lib/HDLRuby/std/sequencer.rb', line 819

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.



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/HDLRuby/std/sequencer.rb', line 860

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



472
473
474
# File 'lib/HDLRuby/std/sequencer.rb', line 472

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.



878
879
880
# File 'lib/HDLRuby/std/sequencer.rb', line 878

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.



884
885
886
# File 'lib/HDLRuby/std/sequencer.rb', line 884

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.



890
891
892
# File 'lib/HDLRuby/std/sequencer.rb', line 890

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)


895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'lib/HDLRuby/std/sequencer.rb', line 895

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.



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

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.



967
968
969
# File 'lib/HDLRuby/std/sequencer.rb', line 967

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.



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/HDLRuby/std/sequencer.rb', line 448

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.



972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
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
# File 'lib/HDLRuby/std/sequencer.rb', line 972

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.



1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/HDLRuby/std/sequencer.rb', line 1046

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.



1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
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
# File 'lib/HDLRuby/std/sequencer.rb', line 1057

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.



1131
1132
1133
1134
1135
1136
1137
1138
1139
# File 'lib/HDLRuby/std/sequencer.rb', line 1131

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.



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

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.



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

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)


1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'lib/HDLRuby/std/sequencer.rb', line 1175

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)


1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
# File 'lib/HDLRuby/std/sequencer.rb', line 1202

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.



1229
1230
1231
# File 'lib/HDLRuby/std/sequencer.rb', line 1229

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

#sreject(&ruby_block) ⇒ Object

HW implementatiob of the Ruby reject.



1234
1235
1236
# File 'lib/HDLRuby/std/sequencer.rb', line 1234

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.



1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'lib/HDLRuby/std/sequencer.rb', line 1239

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.



785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/HDLRuby/std/sequencer.rb', line 785

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.



1262
1263
1264
# File 'lib/HDLRuby/std/sequencer.rb', line 1262

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.



1268
1269
1270
# File 'lib/HDLRuby/std/sequencer.rb', line 1268

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.



1274
1275
1276
# File 'lib/HDLRuby/std/sequencer.rb', line 1274

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

#ssort(&ruby_block) ⇒ Object

HW implementation of the Ruby sort.



1373
1374
1375
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
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
# File 'lib/HDLRuby/std/sequencer.rb', line 1373

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.



1450
1451
1452
1453
1454
1455
1456
1457
1458
# File 'lib/HDLRuby/std/sequencer.rb', line 1450

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.



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
1362
1363
1364
1365
1366
1367
1368
1369
1370
# File 'lib/HDLRuby/std/sequencer.rb', line 1330

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.



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

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.



1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File 'lib/HDLRuby/std/sequencer.rb', line 1491

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.



1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
# File 'lib/HDLRuby/std/sequencer.rb', line 1506

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.



1543
1544
1545
# File 'lib/HDLRuby/std/sequencer.rb', line 1543

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

#sto_aObject

HW implementation of the Ruby to_a.



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/HDLRuby/std/sequencer.rb', line 768

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.



1549
1550
1551
# File 'lib/HDLRuby/std/sequencer.rb', line 1549

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

#suniq(&ruby_block) ⇒ Object

HW implementation of the Ruby uniq.



1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
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
# File 'lib/HDLRuby/std/sequencer.rb', line 1554

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.



1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
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
# File 'lib/HDLRuby/std/sequencer.rb', line 1610

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