Class: Oneliner::SuperString
- Inherits:
-
String
- Object
- String
- Oneliner::SuperString
- Defined in:
- lib/oneliner/superstring.rb
Overview
A String subclass providing Online Code functionality.
Constant Summary collapse
- E =
0.01- Q =
3- F =
(Math.log((E ** 2) / 4) / Math.log(1 - (E / 2))).abs.to_i
- P =
[0, (1 - ((1 + (1 / F)) / (1 + E)))]
Instance Method Summary collapse
-
#decode!(chunk) ⇒ Object
Will try to decode this SuperString using the given
chunkand all formerly given chunks. -
#decode_done? ⇒ Boolean
Returns whether decoding is done.
-
#encode(requested_size) ⇒ Object
Will return a String containing
requested_sizenr of bytes as an online coded chunk of blocks for this SuperString.
Instance Method Details
#decode!(chunk) ⇒ Object
Will try to decode this SuperString using the given chunk and all formerly given chunks.
Returns whether decoding is done.
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 |
# File 'lib/oneliner/superstring.rb', line 67 def decode!(chunk) raise "#{chunk.inspect} is too small for metadata (8 bytes)" unless chunk.size > 7 @decode_done = nil context = Context.new requested_size = chunk[0..3].unpack("i").first ensure_decode_format(requested_size) the_seed = chunk[4..7].unpack("i").first chunk_data = build_chunk_data(chunk, context, the_seed) @known_nr_of_blocks += chunk_data.size @known_chunks.unshift(chunk_data) if @known_nr_of_blocks > @nr_of_blocks nil while do_decode(context) if decode_done? self.replace(compact(@blocks[0...@nr_of_data_blocks])[0...requested_size]) return true else return false end else return false end end |
#decode_done? ⇒ Boolean
Returns whether decoding is done.
104 105 106 107 108 109 110 111 112 |
# File 'lib/oneliner/superstring.rb', line 104 def decode_done? return false unless defined?(@decode_done) && defined?(@nr_of_data_blocks) unless @decode_done data = @blocks[0...@nr_of_data_blocks] @decode_done = data.compact.size == data.size end return @decode_done end |
#encode(requested_size) ⇒ Object
Will return a String containing requested_size nr of bytes as an online coded chunk of blocks for this SuperString.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/oneliner/superstring.rb', line 40 def encode(requested_size) raise "requested size is too small for metadata (8 bytes)" unless requested_size > 7 ensure_encode_format srand seed = rand(Context::INT_MAX) context = Context.new context.seed(seed) rval = [self.size].pack("i") rval << [seed].pack("i") blocks = [] wanted_blocks = ((requested_size - 8) * 8) / @block_size 1.upto(wanted_blocks) do |n| blocks << generate_check_block(context, n - 1) end return rval + compact(blocks) end |