From ba9d2fb48ceef35009d240e713418e415f68ee9d Mon Sep 17 00:00:00 2001 From: Karchnu Date: Thu, 16 Jul 2020 20:04:13 +0200 Subject: [PATCH] reversed --- src/cached.cr | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/cached.cr b/src/cached.cr index 33a53ba..0bea1a8 100644 --- a/src/cached.cr +++ b/src/cached.cr @@ -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)