reversed
parent
370e0eca99
commit
ba9d2fb48c
|
@ -3,20 +3,20 @@ require "json"
|
|||
|
||||
require "./dodb.cr"
|
||||
|
||||
#class Hash(K,V)
|
||||
# def reverse
|
||||
# rev = Array(Tuple(K,V)).new
|
||||
# keys = Array(K).new
|
||||
# each_key do |k|
|
||||
# keys << k
|
||||
# end
|
||||
# keys.reverse.each do |k|
|
||||
# rev << {k, self.[k]}
|
||||
# end
|
||||
#
|
||||
# rev
|
||||
# end
|
||||
#end
|
||||
class Hash(K,V)
|
||||
def reverse
|
||||
rev = Array(Tuple(K,V)).new
|
||||
keys = Array(K).new
|
||||
each_key do |k|
|
||||
keys << k
|
||||
end
|
||||
keys.reverse.each do |k|
|
||||
rev << {k, self.[k]}
|
||||
end
|
||||
|
||||
rev
|
||||
end
|
||||
end
|
||||
|
||||
class DODB::DataBase::Cached(V)
|
||||
@indexers = [] of Indexer(V)
|
||||
|
@ -88,8 +88,7 @@ class DODB::DataBase::Cached(V)
|
|||
# Can be useful for making dumps or to restore a database, however.
|
||||
def each_with_index(reversed : Bool = false, start_offset = 0, end_offset : Int32? = nil)
|
||||
i = -1 # do not trust key to be the right index
|
||||
#(reversed ? @data.reverse : @data).each do |index, v|
|
||||
@data.each do |index, v|
|
||||
(reversed ? @data.reverse : @data).each do |index, v|
|
||||
i += 1
|
||||
next if start_offset > i
|
||||
break unless end_offset.nil? || i < end_offset
|
||||
|
@ -108,7 +107,11 @@ class DODB::DataBase::Cached(V)
|
|||
end
|
||||
|
||||
def to_a(reversed : Bool = false, start_offset = 0, end_offset : Int32? = nil)
|
||||
@data.values
|
||||
res = Array(V).new
|
||||
each(reversed, start_offset, end_offset) do |v|
|
||||
res << v
|
||||
end
|
||||
res
|
||||
end
|
||||
|
||||
def to_h(reversed : Bool = false, start_offset = 0, end_offset : Int32? = nil)
|
||||
|
|
Loading…
Reference in New Issue