Jakowlew

barr

Mar 11th, 2018
175
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local barr = {}
  2.  
  3. function barr:new(chunk)
  4.   local obj = {
  5.     data = {},
  6.     chunk = {},
  7.     chunkSize = 0,
  8.     maxChunkSize = 64,
  9.     size = 0,
  10.     cellCapacity = 0
  11.   }
  12.  
  13.   if type(chunk) == "number" then
  14.     if chunk > obj.maxChunkSize then return nil end
  15.     obj.chunk[1] = chunk
  16.     obj.chunkSize = chunk
  17.   elseif type(chunk) == "table" then
  18.     local size = 0
  19.    
  20.     for i = 1, #chunk do
  21.       if tonumber(chunk[i]) == nil then return nil end
  22.       size = size + chunk[i]
  23.     end
  24.    
  25.     if size > obj.maxChunkSize then return nil end
  26.     obj.chunk = chunk
  27.     obj.chunkSize = size
  28.   else
  29.     return nil
  30.   end
  31.  
  32.   obj.cellCapacity = obj.maxChunkSize // obj.chunkSize
  33.  
  34.   self.__index = self
  35.   return setmetatable(obj, self)
  36. end
  37.  
  38. -- Calculates integer power
  39. function barr:pow(x, y)
  40.   if y == 0 then return 1 end
  41.   local oldX = x
  42.   for i = 0, y - 2 do
  43.     x = x * oldX
  44.   end
  45.   return x
  46. end
  47.  
  48. -- Calculates power of 2
  49. function barr:pow2(x)
  50.   return 1 << x
  51. end
  52.  
  53. function barr:write(chunk)
  54.   if chunk == nil then return nil end
  55.  
  56.   if self.size % self.cellCapacity == 0 then
  57.     self.data[#self.data + 1] = 0
  58.   end
  59.  
  60.   -- Calculate bit before write position
  61.   local startBit = self.maxChunkSize - self.size % self.cellCapacity * self.chunkSize
  62.   local cell = 0
  63.  
  64.   for i = 1, #chunk do
  65.     -- Clear non used bits
  66.     chunk[i] = chunk[i] & (barr:pow2(self.chunk[i]) - 1)
  67.     -- Shift bits to the right place
  68.     chunk[i] = chunk[i] << (startBit - self.chunk[i])
  69.     -- Add data to cell
  70.     cell = cell | chunk[i]
  71.     -- Add written bits
  72.     startBit = startBit - self.chunk[i]
  73.   end
  74.  
  75.   self.data[#self.data] = self.data[#self.data] | cell
  76.   self.size = self.size + 1
  77. end
  78.  
  79. function barr:read(pos)
  80.   if pos == nil or pos < 0 or pos >= self.size then return nil end
  81.  
  82.   -- Get needed cell
  83.   local cell = self.data[pos // self.cellCapacity + 1]
  84.   -- Prepare chunk
  85.   local chunk = {}
  86.   -- Calculate bit before write position
  87.   local startBit = self.maxChunkSize - pos % self.cellCapacity * self.chunkSize
  88.  
  89.   for i = 1, #self.chunk do
  90.     -- Clear non used bits
  91.     chunk[i] = barr:pow2(self.chunk[i]) - 1
  92.     -- Shift bits to the stored place
  93.     chunk[i] = chunk[i] << (startBit - self.chunk[i])
  94.     -- Add data to chunk
  95.     chunk[i] = cell & chunk[i]
  96.     -- Shift bits back
  97.     chunk[i] = chunk[i] >> (startBit - self.chunk[i])
  98.     -- Add written bits
  99.     startBit = startBit - self.chunk[i]
  100.   end
  101.  
  102.   return chunk
  103. end
  104.  
  105. function barr:writeArray(array)
  106.   for i = 1, #array do
  107.     self:write(array[i])
  108.   end
  109. end
  110.  
  111. function barr:readArray(pos, n)
  112.   local t = {}
  113.   for i = pos, n - 1 do
  114.     t[i+1] = self:read(i)
  115.   end
  116.   return t
  117. end
  118.  
  119. --[[
  120. function barr:writeLinearArray(array)
  121.   for i = 1, #array, #self.chunk do
  122.     local t = {}
  123.     for j = 1, #self.chunk do
  124.       t[i] = array[i+j]
  125.     end
  126.     self:write(t)
  127.   end
  128. end
  129.  
  130. function barr:readLinearArray(pos, n)
  131.  
  132. end
  133. --
  134.  
  135. a = barr:new(8)
  136. a:writeLinearArray({1, 2, 3})
  137. --a:write({2})
  138. --a:write({3})
  139.  
  140. b = barr:new(8)
  141. b:write({4})
  142. b:write({5})
  143. b:write({6})
  144.  
  145. t = a:readArray(0, 1)
  146. print(t[1][1])
  147. ]]
  148.  
  149. return barr
RAW Paste Data