Advertisement
lichenran1234

FastInsertDeleteString

Apr 15th, 2021
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. import math
  2.  
  3. class Node(object):
  4.     def __init__(self, val=None):
  5.         self.val = val
  6.         self.next = None
  7.         self.prev = None
  8.  
  9. class NewString(object):
  10.     def __init__(self, capacity):
  11.         self.capacity = capacity
  12.        
  13.         self.rank = int(math.sqrt(capacity))
  14.         if self.rank * self.rank < self.rank:
  15.             self.rank += 1
  16.        
  17.         self.size = 0
  18.         self.data_map = {}
  19.    
  20.     def read(self, index):
  21.         if index >= self.size or index < 0:
  22.             return None
  23.        
  24.         node = self.data_map[index / self.rank]
  25.         for i in range(index % self.rank):
  26.             node = node.next
  27.         return node.val
  28.    
  29.     def insert(self, char, index):
  30.         if index > self.size or index < 0 or self.size == self.capacity:
  31.             return False
  32.        
  33.         self.size += 1
  34.         new_node = Node(char)
  35.  
  36.         # Check if it's the first char.
  37.         if self.size == 1:
  38.             self.data_map[0] = new_node
  39.             return True
  40.        
  41.         # Check if we are inserting at the end.
  42.         if index == self.size - 1:
  43.             last_node = self.data_map[(self.size - 2) / self.rank]
  44.             while last_node.next is not None:
  45.                 last_node = last_node.next
  46.             last_node.next = new_node
  47.             new_node.prev = last_node
  48.             # check if we need a new entry in the map
  49.             if (self.size - 1) % self.rank == 0:
  50.                 self.data_map[(self.size - 1) / self.rank] = new_node
  51.             return True
  52.  
  53.         insert_before = self.data_map[index / self.rank]
  54.         for i in range(index % self.rank):
  55.             insert_before = insert_before.next
  56.         if insert_before.prev is not None:
  57.             new_node.prev = insert_before.prev
  58.             insert_before.prev.next = new_node
  59.         new_node.next = insert_before
  60.         insert_before.prev = new_node
  61.        
  62.         next_map_entry_to_move = (index + self.rank - 1) / self.rank
  63.         last_map_entry = (self.size - 2) / self.rank
  64.         for i in range(next_map_entry_to_move, last_map_entry + 1):
  65.             self.data_map[i] = self.data_map[i].prev
  66.        
  67.         # Check if we need a new entry in the map.
  68.         if (self.size - 1) % self.rank == 0:
  69.             last_node = self.data_map[last_map_entry]
  70.             while last_node.next is not None:
  71.                 last_node = last_node.next
  72.             self.data_map[(self.size - 1) / self.rank] = last_node
  73.         return True
  74.    
  75.     def delete(self, index):
  76.         if index >= self.size or index < 0 or self.size == 0:
  77.             return False
  78.        
  79.         self.size -= 1
  80.        
  81.         # check if we are deleting the only char
  82.         if self.size == 0:
  83.             del self.data_map[0]
  84.             return True
  85.        
  86.         node_to_delete = self.data_map[index / self.rank]
  87.         for i in range(index % self.rank):
  88.             node_to_delete = node_to_delete.next
  89.        
  90.         # check if we need to delete the last entry in the map
  91.         if self.size % self.rank == 0:
  92.             del self.data_map[self.size / self.rank]
  93.        
  94.         # check if we are deleting the last char
  95.         if node_to_delete.next is None:
  96.             node_to_delete.prev.next = None
  97.             return True
  98.        
  99.         node_to_delete.next.prev = node_to_delete.prev
  100.         if node_to_delete.prev is not None:
  101.             node_to_delete.prev.next = node_to_delete.next
  102.         next_map_entry_to_move = (index + self.rank - 1) / self.rank
  103.         last_map_entry = (self.size - 1) / self.rank
  104.         for i in range(next_map_entry_to_move, last_map_entry + 1):
  105.             self.data_map[i] = self.data_map[i].next
  106.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement