File: sorted_num_array.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (222 lines) | stat: -rw-r--r-- 4,491 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#
# bio/util/restriction_enzyme/sorted_num_array.rb - Internal data storage for Bio::RestrictionEnzyme::Range::SequenceRange
#
# Copyright::   Copyright (C) 2011
#               Naohisa Goto <ng@bioruby.org>
# License::     The Ruby License
#

module Bio

require 'bio/util/restriction_enzyme' unless const_defined?(:RestrictionEnzyme)

class RestrictionEnzyme

  # a class to store sorted numerics.
  #
  # Bio::RestrictionEnzyme internal use only.
  # Please do not create the instance outside Bio::RestrictionEnzyme.
  class SortedNumArray

    # Same usage as Array.[]
    def self.[](*args)
      a = self.new
      args.each do |elem|
        a.push elem
      end
      a
    end

    # Creates a new object
    def initialize
      @hash = {}
      #clear_cache
    end

    # initialize copy
    def initialize_copy(other)
      super(other)
      @hash = @hash.dup
    end

    # sets internal hash object
    def internal_data_hash=(h)
      #clear_cache
      @hash = h
      self
    end
    protected :internal_data_hash=

    # gets internal hash object
    def internal_data_hash
      @hash
    end
    protected :internal_data_hash

    #---
    ## clear the internal cache
    #def clear_cache
    #  @sorted_keys = nil
    #end
    #protected :clear_cache
    #+++

    # sorted keys
    def sorted_keys
      #@sorted_keys ||= @hash.keys.sort
      #@sorted_keys
      @hash.keys.sort
    end
    private :sorted_keys

    # adds a new element
    def push_element(n)
      #return if @hash.has_key?(n) #already existed; do nothing
      @hash.store(n, true)
      #if @sorted_keys then
      #  if thelast = @sorted_keys[-1] and n > thelast then
      #    @sorted_keys.push n
      #  else
      #    clear_cache
      #  end
      #end
      nil
    end
    private :push_element

    # adds a new element in the beginning of the array 
    def unshift_element(n)
      #return if @hash.has_key?(n) #already existed; do nothing
      @hash.store(n, true)
      #if @sorted_keys then
      #  if thefirst = @sorted_keys[0] and n < thefirst then
      #    @sorted_keys.unshift n
      #  else
      #    clear_cache
      #  end
      #end
      nil
    end
    private :unshift_element

    # Same usage as Array#[]
    def [](*arg)
      #$stderr.puts "SortedNumArray#[]"
      sorted_keys[*arg]
    end

    # Not implemented
    def []=(*arg)
      raise NotImplementedError, 'SortedNumArray#[]= is not implemented.'
    end

    # Same usage as Array#each
    def each(&block)
      sorted_keys.each(&block)
    end

    # Same usage as Array#reverse_each
    def reverse_each(&block)
      sorted_keys.reverse_each(&block)
    end

    # Same usage as Array#+, but accepts only the same classes instance.
    def +(other)
      unless other.is_a?(self.class) then
        raise TypeError, 'unsupported data type'
      end
      new_hash = @hash.merge(other.internal_data_hash)
      result = self.class.new
      result.internal_data_hash = new_hash
      result
    end

    # Same usage as Array#==
    def ==(other)
      if r = super(other) then
        r
      elsif other.is_a?(self.class) then
        other.internal_data_hash == @hash
      else
        false
      end
    end

    # Same usage as Array#concat
    def concat(ary)
      ary.each { |elem| push_element(elem) }
      self
    end

    # Same usage as Array#push
    def push(*args)
      args.each do |elem|
        push_element(elem)
      end
      self
    end

    # Same usage as Array#unshift
    def unshift(*arg)
      arg.reverse_each do |elem|
        unshift_element(elem)
      end
      self
    end

    # Same usage as Array#<<
    def <<(elem)
      push_element(elem)
      self
    end

    # Same usage as Array#include?
    def include?(elem)
      @hash.has_key?(elem)
    end

    # Same usage as Array#first
    def first
      sorted_keys.first
    end

    # Same usage as Array#last
    def last
      sorted_keys.last
    end

    # Same usage as Array#size
    def size
      @hash.size
    end
    alias length size

    # Same usage as Array#delete
    def delete(elem)
      #clear_cache
      @hash.delete(elem) ? elem : nil
    end

    # Does nothing
    def sort!(&block)
      # does nothing
      self
    end

    # Does nothing
    def uniq!
      # does nothing
      self
    end

    # Converts to an array
    def to_a
      #sorted_keys.dup
      sorted_keys
    end
  end #class SortedNumArray

end #class RestrictionEnzyme
end #module Bio