parent
12bb7adf3a
commit
c1b3b9a9d6
|
@ -75,15 +75,15 @@ vim.cmd("nnoremap ; :")
|
|||
-- })
|
||||
-- vim.keymap.set('n', ';', ':', {noremap = true, silent = true})
|
||||
|
||||
vim.api.nvim_set_keymap('i', 'HH', '<Home>', {
|
||||
silent = true,
|
||||
noremap = true
|
||||
})
|
||||
--vim.api.nvim_set_keymap('i', 'HH', '<Home>', {
|
||||
-- silent = true,
|
||||
-- noremap = true
|
||||
--})
|
||||
|
||||
vim.api.nvim_set_keymap('i', 'LL', '<End>', {
|
||||
silent = true,
|
||||
noremap = true
|
||||
})
|
||||
-- vim.api.nvim_set_keymap('i', 'LL', '<End>', {
|
||||
-- silent = true,
|
||||
-- noremap = true
|
||||
-- })
|
||||
|
||||
-- autocmd BufWinEnter quickfix nnoremap <silent> <buffer>
|
||||
-- \ q :cclose<cr>:lclose<cr>
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
typedef struct Node {
|
||||
int data;
|
||||
void *npx;
|
||||
} Node;
|
||||
|
||||
|
||||
typedef struct XorList {
|
||||
Node *head;
|
||||
Node *tail;
|
||||
} XorList;
|
||||
|
||||
|
||||
int insert(XorList *list, int data) {
|
||||
Node *p;
|
||||
p = (Node *)malloc(sizeof(Node));
|
||||
p->data = data;
|
||||
p->npx = NULL;
|
||||
|
||||
if (list == NULL) {
|
||||
list = (XorList *)malloc(sizeof(XorList));
|
||||
}
|
||||
|
||||
if (list->head == NULL) {
|
||||
list->head = p;
|
||||
list->tail = p;
|
||||
} else {
|
||||
// TODO: calc npx
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
# coding=utf-8
|
||||
|
||||
import ctypes
|
||||
|
||||
|
||||
NULL = 0
|
||||
|
||||
|
||||
class Node:
|
||||
|
||||
|
||||
def __init__(self, data, npx=NULL) -> None:
|
||||
self.data = data
|
||||
# npx means XOR of next and prev node
|
||||
self.npx = npx
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "Node(data={}, npx={}, id={})".format(self.data, self.npx, id(self))
|
||||
|
||||
|
||||
class XorList:
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.head = None
|
||||
self.tail = None
|
||||
self.__nodes = []
|
||||
|
||||
def xor(self, a, b):
|
||||
"""
|
||||
Return XORed value of node reference
|
||||
"""
|
||||
return a ^ b
|
||||
|
||||
def insert(self, data):
|
||||
node = Node(data)
|
||||
node_id = id(node)
|
||||
# New node npx is XOR of current head and NULL
|
||||
node.npx = id(self.head)
|
||||
# node.npx = self.xor(node_id, id(self.head))
|
||||
|
||||
if self.head is not None:
|
||||
old_head_npx = self.head.npx
|
||||
self.head.npx = self.xor(node_id, old_head_npx)
|
||||
else:
|
||||
self.tail = node
|
||||
|
||||
self.__nodes.append(node)
|
||||
self.head = node
|
||||
|
||||
def forward(self):
|
||||
print("forward visit list")
|
||||
prev_id = NULL
|
||||
curr_node = self.head
|
||||
|
||||
if curr_node is None:
|
||||
print("Empty linked_list")
|
||||
return
|
||||
|
||||
while curr_node is not None:
|
||||
print("Node: {}".format(curr_node))
|
||||
# npx = prev ^ next
|
||||
# prev ^ npx = prev ^ prev ^ next = next
|
||||
# next ^ npx = next ^ prev ^ next = prev
|
||||
next_id = self.xor(prev_id, curr_node.npx)
|
||||
prev_id = id(curr_node)
|
||||
curr_node = self.__type_cast(next_id)
|
||||
|
||||
def backward(self):
|
||||
print("backward visit list")
|
||||
rear_id = NULL
|
||||
curr_node = self.tail
|
||||
|
||||
if curr_node is None:
|
||||
print("Empty list")
|
||||
return
|
||||
|
||||
while curr_node is not None:
|
||||
print("Node: {}".format(curr_node))
|
||||
# TODO: Fix core dump
|
||||
prev_id = self.xor(rear_id, curr_node.npx)
|
||||
rear_id = id(curr_node)
|
||||
curr_node = self.__type_cast(prev_id)
|
||||
|
||||
def __type_cast(self, obj_id):
|
||||
"""
|
||||
Returns a new instance of type points to the same memory
|
||||
"""
|
||||
return ctypes.cast(obj_id, ctypes.py_object).value
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
xor_list = XorList()
|
||||
|
||||
xor_list.insert(10)
|
||||
xor_list.insert(20)
|
||||
xor_list.insert(30)
|
||||
xor_list.insert(40)
|
||||
xor_list.insert(50)
|
||||
|
||||
xor_list.forward()
|
||||
xor_list.backward()
|
||||
|
Loading…
Reference in New Issue