add absolute comparison function
This commit is contained in:
parent
fb94827e79
commit
401b8e8085
1 changed files with 19 additions and 1 deletions
|
@ -19,7 +19,7 @@ class Bdb::Simple
|
|||
@db = Bdb::Db.new
|
||||
@db.flags = Bdb::DB_DUPSORT if dup?
|
||||
@db.btree_compare = lambda do |db, key1, key2|
|
||||
Marshal.load(key1) <=> Marshal.load(key2)
|
||||
compare_absolute(Marshal.load(key1), Marshal.load(key2))
|
||||
end
|
||||
@db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
|
||||
end
|
||||
|
@ -84,4 +84,22 @@ class Bdb::Simple
|
|||
db.close(0)
|
||||
@db = nil
|
||||
end
|
||||
|
||||
def self.compare_absolute(left, right)
|
||||
if left.is_a?(Array) and right.is_a?(Array)
|
||||
left.zip(right) do |l,r|
|
||||
comp = compare_absolute(l, r)
|
||||
return comp unless comp == 0
|
||||
end
|
||||
left.size == right.size ? 0 : -1
|
||||
elsif left.kind_of?(right.class) or right.kind_of?(left.class)
|
||||
left <=> right rescue 0
|
||||
elsif left.is_a?(NilClass)
|
||||
-1
|
||||
elsif right.is_a?(NilClass)
|
||||
1
|
||||
else
|
||||
right.class.name <=> left.class.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue