def _resize_region(self, start, end, new_size):
    """
    Resize slice self[start:end] so that it has size new_size.
    """
    old_size = end - start
    if new_size == old_size:
      return
    elif new_size > old_size:
      delta = new_size - old_size
      self.length += delta
      add_bytes = (self.length + 7) // 8 - len(self.data)
      self.data.extend(array.array('B', [0] * add_bytes))
      for i in xrange(self.length-1, start+new_size-1, -1):
        self._set_element(i, self._get_element(i - delta))
    elif new_size < old_size:
      delta = old_size - new_size
      for i in xrange(start+new_size, self.length-delta):
        self._set_element(i, self._get_element(i + delta))
      self.length -= delta
      del_bytes = len(self.data) - (self.length + 7) // 8
      assert del_bytes <= len(self.data)
      del self.data[len(self.data)-del_bytes:]

    Resize slice self[start:end] so that it has size new_size.
    
recipes/Python/440658_Memory_compacted_list_of_bits/recipe-440658.py