Class: XRange

Inherits:
ValueAdd show all
Defined in:
lib/primitive_wrapper.rb

Instance Method Summary collapse

Methods inherited from ValueAdd

bestow_methods, capture_base_methods

Methods inherited from Value

#!=, #ensure_valid, #freeze, freeze_raise?, ignore_on_freeze, #initialize, #inspect, #prim_value, raise_on_freeze, #replace, #to_s, #to_wrapper, #type, #type_of?, #unwrap, #val, #val=, #wrapped?, #~

Constructor Details

This class inherits a constructor from Value

Instance Method Details

#==(other) ⇒ Object



1002
1003
1004
# File 'lib/primitive_wrapper.rb', line 1002

def ==(other)
  @value.eql? (~other)
end

#===(other) ⇒ Object



998
999
1000
# File 'lib/primitive_wrapper.rb', line 998

def ===(other)
  @value.include? other
end

#countObject



973
974
975
# File 'lib/primitive_wrapper.rb', line 973

def count
  size
end

#cover?(val) ⇒ Boolean

means val>=start and val<=end … different than include

Returns:

  • (Boolean)


979
980
981
# File 'lib/primitive_wrapper.rb', line 979

def cover?(val)  # means val>=start and val<=end ... different than include
  @value.reorder.include? val
end

#each(&block) ⇒ Object



1076
1077
1078
# File 'lib/primitive_wrapper.rb', line 1076

def each(&block)
  self.send(:step, 1, &block)
end

#eql?Boolean

Returns:

  • (Boolean)


995
996
997
# File 'lib/primitive_wrapper.rb', line 995

def eql?
  @value.eql? (~other)
end

#include?(val) ⇒ Boolean

account for reversed ordering

Returns:

  • (Boolean)


976
977
978
# File 'lib/primitive_wrapper.rb', line 976

def include?(val)  # account for reversed ordering
  @value.reorder.include? val
end

#max(*n, &block) ⇒ Object



989
990
991
# File 'lib/primitive_wrapper.rb', line 989

def max(*n, &block)
  @value.reorder.send(:max, *n, &block)  
end

#member?(val) ⇒ Boolean

same as include?

Returns:

  • (Boolean)


982
983
984
# File 'lib/primitive_wrapper.rb', line 982

def member?(val) # same as include?
  @value.reorder.include? val
end

#min(*n, &block) ⇒ Object



992
993
994
# File 'lib/primitive_wrapper.rb', line 992

def min(*n, &block)
  @value.reorder.send(:min, *n, &block)  
end

#re_range(ng) ⇒ Object



924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/primitive_wrapper.rb', line 924

def re_range(ng) 
  return self if ng.nil?
  return self if ng <= 0
  return self unless first.kind_of? Integer    
  t_first = first
  t_last = last
  if first.negative?
    t_first = ng+first
  end
  if last.negative?
    t_last = ng+last
  end
  if exclude_end?
    XRange.new (t_first...t_last)
  else
    XRange.new (t_first..t_last)
  end
end

#re_range!(ng) ⇒ Object



942
943
944
# File 'lib/primitive_wrapper.rb', line 942

def re_range!(ng)
  replace re_range(ng)
end

#reorder!Object



907
908
909
910
# File 'lib/primitive_wrapper.rb', line 907

def reorder!
  @value = @value.reorder
  self
end

#reverseObject



904
905
906
# File 'lib/primitive_wrapper.rb', line 904

def reverse
  @value.reverse.to_xr
end

#reverse!Object



900
901
902
903
# File 'lib/primitive_wrapper.rb', line 900

def reverse!
  @value = @value.reverse
  self
end

#reverse_each(&block) ⇒ Object



1080
1081
1082
# File 'lib/primitive_wrapper.rb', line 1080

def reverse_each(&block)
  self.send(:reverse_step, 1, &block)
end

#reverse_step(n = 1) ⇒ Object



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
# File 'lib/primitive_wrapper.rb', line 1041

def reverse_step(n=1)
  n = 1 if n<= 0
  return Enumerator.enumerate_yields(self, :reverse_step, n) unless block_given?
  curr = last.pw_copy
  if exclude_end?
    if reversed?
      loop do
        break if curr >= first
        yield curr
        n.times { |t| curr = curr.succ }
      end
    else
      loop do
        break if curr <= first
        yield curr
        n.times { |t| curr = curr.pred }
      end
    end
  else
    if reversed?
      loop do
        yield curr
        break if curr >= first
        n.times { |t| curr = curr.succ }
      end
    else
      loop do
        yield curr
        break if curr <= first
        n.times { |t| curr = curr.pred }
      end
    end
  end    
end

#simplifyObject



915
916
917
# File 'lib/primitive_wrapper.rb', line 915

def simplify
  @value.simplify.to_xr
end

#simplify!Object



911
912
913
914
# File 'lib/primitive_wrapper.rb', line 911

def simplify!
  @value = @value.simplify
  self
end

#sizeObject

getting the wrong count … off by 1



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
# File 'lib/primitive_wrapper.rb', line 945

def size   # getting the wrong count ... off by 1
  dirty = false
  sz = @value.size
  if sz.nil?
    dirty = true
  elsif sz==0
    dirty = true
  end
  unless dirty
    return sz
  end
  if first.respond_to? :lcm  # Covers both Integer and Int
    sz = reversed? ? @value.reverse.size : @value.size
    return sz
  end
  range = @value.reorder
  cnt = 0
  # curr = range.first.dup rescue range.first
  curr = range.first.pw_copy
  stop = range.last
  loop do
    cnt += 1  # 5..5 is count of 1
    break if curr==stop
    curr.succ!
  end
  cnt -= 1 if exclude_end?
  return cnt  
end

#step(n = 1) ⇒ Object



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
# File 'lib/primitive_wrapper.rb', line 1006

def step(n=1)
  n = 1 if n<= 0
  return Enumerator.enumerate_yields(self, :step, n) unless block_given?
  curr = first.pw_copy
  if exclude_end?
    if reversed?
      loop do
        break if curr <= last
        yield curr
        n.times { |t| curr = curr.pred }
      end
    else
      loop do
        break if curr >= last
        yield curr
        n.times { |t| curr = curr.succ }
      end
    end
  else
    if reversed?
      loop do
        yield curr
        break if curr <= last
        n.times { |t| curr = curr.pred }
      end
    else
      loop do
        yield curr
        break if curr >= last
        n.times { |t| curr = curr.succ }
      end
    end
  end    
end

#to_aObject



985
986
987
988
# File 'lib/primitive_wrapper.rb', line 985

def to_a
  dat = @value.reorder.to_a
  return reversed? ? dat.reverse : dat    
end

#to_rangeObject

add similar to other wrappers



921
922
923
# File 'lib/primitive_wrapper.rb', line 921

def to_range  # add similar to other wrappers
  @value
end

#to_xrObject



918
919
920
# File 'lib/primitive_wrapper.rb', line 918

def to_xr
  self
end

#valid_type(prm) ⇒ Object



895
896
897
898
899
# File 'lib/primitive_wrapper.rb', line 895

def valid_type(prm)
  return true if prm.kind_of? Range
  return true if prm.kind_of? XRange
  false
end