Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lalawue committed Aug 19, 2020
1 parent 9d31bae commit 17cc21f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
30 changes: 22 additions & 8 deletions middle/ffi_avl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct _avl_node {
struct _avl_node *right;
struct _avl_node *parent;
int height;
float key;
double key;
};
struct _avl_root {
struct _avl_node *node;
Expand All @@ -32,12 +32,12 @@ _M.__index = _M

-- key for node <-> value mapping
local function _nextKey(self)
local bkey = self._bkey + 1e-35
while self._nvmap[bkey] do
bkey = bkey - math.random()
local key = self._key + 1e-32 -- ignore significant digit
while self._nvmap[key] do
key = key + math.random()
end
self._bkey = bkey
return bkey
self._key = key
return key
end

local function _calloc(str)
Expand Down Expand Up @@ -422,6 +422,20 @@ function _M:remove(value)
return value
end

function _M:clear()
if self._count <= 0 then
return
end
for _, n in pairs(self._vnmap) do
C.free(n)
end
self._root.node = nil
self._vnmap = {} -- value to node
self._nvmap = {} -- node to value
self._count = 0 -- total count
self._key = 0
end

function _M:count()
return self._count
end
Expand All @@ -434,8 +448,8 @@ local function _new(compare_func)
ins._vnmap = {} -- value to node
ins._nvmap = {} -- node to value
ins._count = 0 -- total count
ins._compare = compare_func -- compare function
ins._bkey = 0
ins._key = 0
ins._compare = compare_func -- compare function
ffi.gc(
ins._root,
function(root)
Expand Down
55 changes: 47 additions & 8 deletions middle/ffi_list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ffi.cdef [[
struct _cnode {
struct _cnode *prev;
struct _cnode *next;
float key;
double key;
};
struct _chead {
struct _cnode *head;
Expand All @@ -32,12 +32,12 @@ local _M = {}
_M.__index = _M

local function _nextKey(self)
local bkey = self._bkey + 1e-35
while self._nvmap[self._bkey] do
bkey = bkey - math.random()
local key = self._key + 1e-32 -- ignore significant digit
while self._nvmap[key] do
key = key + math.random()
end
self._bkey = bkey
return bkey
self._key = key
return key
end

local function _calloc(str)
Expand Down Expand Up @@ -147,6 +147,28 @@ function _M:remove(value)
return _remove_node(self, self._vnmap[value])
end

function _M:next(value)
if value == nil then
return nil
end
local node = self._vnmap[value]
if node == nil or node.next == nil then
return nil
end
return self._nvmap[node.next.key]
end

function _M:prev(value)
if value == nil then
return nil
end
local node = self._vnmap[value]
if node == nil or node.prev == nil then
return nil
end
return self._nvmap[node.prev.key]
end

function _M:range(from, to)
from = from or 1
to = to or self._count
Expand Down Expand Up @@ -189,6 +211,21 @@ function _M:walk()
end
end

function _M:clear()
if self._count <= 0 then
return
end
for _, n in pairs(self._vnmap) do
C.free(n)
end
self._root.head = nil
self._root.tail = nil
self._vnmap = {} -- value to node
self._nvmap = {} -- node to value
self._count = 0
self._key = 0 -- for key generation
end

function _M:count()
return self._count
end
Expand All @@ -197,10 +234,12 @@ end
local function _new()
local ins = setmetatable({}, _M)
ins._root = _calloc("struct _chead")
ins._count = 0
ins._root.head = nil
ins._root.tail = nil
ins._vnmap = {} -- value to node
ins._nvmap = {} -- node to value
ins._bkey = 0 -- for key generation
ins._count = 0
ins._key = 0 -- for key generation
ffi.gc(
ins._root,
function(root)
Expand Down

0 comments on commit 17cc21f

Please sign in to comment.