Class: Array

Inherits:
Object show all
Defined in:
lib/framework/bsearch.rb,
lib/framework/builtinME.rb,
lib/extensions/mspec/mspec/pp.rb,
lib/extensions/rexml/rexml/xpath_parser.rb,
lib/extensions/rhoxml/rexml/xpath_parser.rb

Direct Known Subclasses

PDF::Writer::StateStack

Instance Method Summary collapse

Instance Method Details

#bsearch_first(range = 0 ... self.length, &block) ⇒ Object Also known as: bsearch

This method searches the FIRST occurrence which satisfies a condition given by a block in binary fashion and return the index of the first occurrence. Return nil if not found.



67
68
69
70
71
72
73
74
# File 'lib/framework/bsearch.rb', line 67

def bsearch_first (range = 0 ... self.length, &block)
  boundary = bsearch_lower_boundary(range, &block)
  if boundary >= self.length || yield(self[boundary]) != 0
    return nil
  else
    return boundary
  end
end

#bsearch_last(range = 0 ... self.length, &block) ⇒ Object

This method searches the LAST occurrence which satisfies a condition given by a block in binary fashion and return the index of the last occurrence. Return nil if not found.



100
101
102
103
104
105
106
107
108
109
# File 'lib/framework/bsearch.rb', line 100

def bsearch_last (range = 0 ... self.length, &block)
  # `- 1' for canceling `lower + 1' in bsearch_upper_boundary.
  boundary = bsearch_upper_boundary(range, &block) - 1

  if (boundary <= -1 || yield(self[boundary]) != 0)
    return nil
  else
    return boundary
  end
end

#bsearch_lower_boundary(range = 0 ... self.length, &block) ⇒ Object

Return the lower boundary. (inside)



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/framework/bsearch.rb', line 48

def bsearch_lower_boundary (range = 0 ... self.length, &block)
  lower  = range.first() -1
  upper = if range.exclude_end? then range.last else range.last + 1 end
  while lower + 1 != upper
    mid = ((lower + upper) / 2).to_i # for working with mathn.rb (Rational)
    if yield(self[mid]) < 0
	lower = mid
    else
	upper = mid
    end
  end
  return upper
end

#bsearch_range(range = 0 ... self.length, &block) ⇒ Object

Return the search result as a Range object.



114
115
116
117
118
# File 'lib/framework/bsearch.rb', line 114

def bsearch_range (range = 0 ... self.length, &block)
  lower = bsearch_lower_boundary(range, &block)
  upper = bsearch_upper_boundary(range, &block)
  return lower ... upper
end

#bsearch_upper_boundary(range = 0 ... self.length, &block) ⇒ Object

Return the upper boundary. (outside)



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/framework/bsearch.rb', line 81

def bsearch_upper_boundary (range = 0 ... self.length, &block)
  lower  = range.first() -1
  upper = if range.exclude_end? then range.last else range.last + 1 end
  while lower + 1 != upper
    mid = ((lower + upper) / 2).to_i # for working with mathn.rb (Rational)
    if yield(self[mid]) <= 0
	lower = mid
    else
	upper = mid
    end
  end
  return lower + 1 # outside of the matching range.
end

#count(*args, &block) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/framework/builtinME.rb', line 283

def count(*args, &block)
    
    if !args || args.length() == 0
        n = 0
        if !block_given?
            return self.length()
        else
            self.each do |item|                
                n += 1 if yield(item)
            end
        end
        
        return n
    else
        n = 0
        self.each do |item|                
            n += 1 if item == args[0]
        end
        
        return n
    end    
end

#dcloneObject



22
23
24
25
26
27
# File 'lib/extensions/rexml/rexml/xpath_parser.rb', line 22

def dclone
  klone = self.clone
  klone.clear
  self.each{|v| klone << v.dclone}
  klone
end

#inspectObject Also known as: to_s



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/framework/builtinME.rb', line 268

def inspect
    str = "["
    is_first = true
    self.each() { |x|
        if (!is_first)
            str << ", "
        end
        is_first = false
        str << x.inspect
    }
    str << "]"
end

#join(sepString = "") ⇒ Object

def to_a

    self
end


249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/framework/builtinME.rb', line 249

def join(sepString="")
    #return to_s if sepString.nil? || sepString == ""
		
    result = ""
    return result if length==0
    
    sepString = "" if sepString.nil?
    
    (length - 1).times do |index|
        result += (self[index].to_s) + sepString
    end
    result += self[length - 1].to_s if length != 0
    result
end

#pretty_print(q) ⇒ Object



705
706
707
708
709
710
711
# File 'lib/extensions/mspec/mspec/pp.rb', line 705

def pretty_print(q)
  q.group(1, '[', ']') {
    q.seplist(self) {|v|
      q.pp v
    }
  }
end

#pretty_print_cycle(q) ⇒ Object



713
714
715
# File 'lib/extensions/mspec/mspec/pp.rb', line 713

def pretty_print_cycle(q)
  q.text(empty? ? '[]' : '[...]')
end

#rejectObject



235
236
237
238
239
240
241
242
243
# File 'lib/framework/builtinME.rb', line 235

def reject
  a = []
  each {|x|
    if !yield x
      a << x
    end
  }
  a
end