Skip to content

Commit

Permalink
remove hat trie
Browse files Browse the repository at this point in the history
  • Loading branch information
chenkovsky committed Mar 15, 2018
1 parent 1e0c3e2 commit 04ceb72
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 411 deletions.
9 changes: 9 additions & 0 deletions spec/array_hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,14 @@ describe Aha do
end

it "array hash io" do
hash = Aha::ArrayHash(2).new
v1 = Bytes.new [3_u8, 4_u8].to_unsafe, 2
v2 = Bytes.new [5_u8, 6_u8].to_unsafe, 2
v3 = Bytes.new [7_u8, 8_u8].to_unsafe, 2
hash["abc"] = v1
hash["cde"] = v2
hash["abcd"] = v3
hash.save "aha.bin"
hash = Aha::ArrayHash(2).load "aha.bin"
end
end
20 changes: 0 additions & 20 deletions spec/hat_spec.cr

This file was deleted.

2 changes: 1 addition & 1 deletion src/aha/ac.cr
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ module Aha

def self.load(path)
File.open(path, "rb") do |f|
return AC.from_io f, Aha::ByteFormat
return self.from_io f, Aha::ByteFormat
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/aha/array_hash.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Aha
# an open address hash table
struct ArrayHash(N) # N is the byte num of value
class ArrayHash(N) # N is the byte num of value
struct KV
@key : Bytes
@value : Bytes
Expand Down Expand Up @@ -74,7 +74,7 @@ module Aha
if padding != 4
(0...padding).each { |_| UInt8.from_io io, format }
end
return self.new(n, m, max_m, slot_sizes, slots)
return self.new(n, m, max_m, slot_sizes, slots.to_unsafe)
end

def initialize(@n, @m, @max_m, @slot_sizes, @slots)
Expand Down Expand Up @@ -241,7 +241,7 @@ module Aha
return ret
end

private def try_get(key : Bytes | Array(UInt8))
private def try_get(key : Bytes | Array(UInt8)) : UInt8*
get_key(key, false)
end

Expand Down Expand Up @@ -313,7 +313,7 @@ module Aha

def self.load(path)
File.open(path, "rb") do |f|
return ArrayHash.from_io f, Aha::ByteFormat
return self.from_io f, Aha::ByteFormat
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/aha/cedar.cr
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ module Aha

def self.load(path)
File.open(path, "rb") do |f|
return Cedar.from_io f, Aha::ByteFormat
return self.from_io f, Aha::ByteFormat
end
end
end
Expand Down
42 changes: 42 additions & 0 deletions src/aha/elias_fano.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Aha
# 用于存储单调递增的整数序列, 能够对序列进行压缩
struct EliasFano(T)
@bytes : UInt8*
@length : UInt64
@u : T

def self.round_up(val : UInt64, den : UInt64) : UInt64
val = val == 0 ? den : val
return (val % den == 0) ? val : val + (den - (val % den))
end

# Returns the number of lower bits required to encode each element in an
# array of size length with maximum value u
def self.get_l(u : UInt32, length : UInt32)
x = round_up(u.to_u64, length.to_u64) / length
return (sizeof(T) << 3) - Aha.leading_zero(x - 1)
end

def initialize(ptr : T*, @length : UInt64)
@u = ptr[length - 1]
l = self.class.get_l u, length
@bytes = Pointer(UInt8).malloc(compressed_size(@u, @length))
low_bits_offset = 0
high_bits_offset = round_up l * @length, 8
prev = 0
(0...@length).each do |i|
# @TODO
end
end

def [](idx : UInt64) : T
end

def self.compressed_size(u, length)
l = get_l u, length
num_low_bits = round_up l * length, 8
num_high_bits = round_up 2 * length, 8
(num_low_bits + num_high_bits) / 8
end
end
end
Empty file added src/aha/elias_fano_trie.cr
Empty file.

0 comments on commit 04ceb72

Please sign in to comment.