tools/media_engagement_preload/make_dafsa.py
def encode_links(children, offsets, current):
  """Encodes a list of children as one, two or three byte offsets."""
  if not children[0]:
    # This is an <end_label> node and no links follow such nodes
    assert len(children) == 1
    return []
  guess = 3 * len(children)
  assert children
  children = sorted(children, key = lambda x: -offsets[id(x)])
  while True:
    offset = current + guess
    buf = []
    for child in children:
      last = len(buf)
      distance = offset - offsets[id(child)]
      assert distance > 0 and distance < (1 << 21)

      if distance < (1 << 6):
        # A 6-bit offset: "s0xxxxxx"
        buf.append(distance)
      elif distance < (1 << 13):
        # A 13-bit offset: "s10xxxxxxxxxxxxx"
        buf.append(0x40 | (distance >> 8))
        buf.append(distance & 0xFF)
      else:
        # A 21-bit offset: "s11xxxxxxxxxxxxxxxxxxxxx"
        buf.append(0x60 | (distance >> 16))
        buf.append((distance >> 8) & 0xFF)
        buf.append(distance & 0xFF)
      # Distance in first link is relative to following record.
      # Distance in other links are relative to previous link.
      offset -= distance
    if len(buf) == guess:
      break
    guess = len(buf)
  # Set most significant bit to mark end of links in this node.
  buf[last] |= (1 << 7)
  buf.reverse()
  return buf
Encodes a list of children as one, two or three byte offsets.
Encodes a list of children as one, two or three byte offsets.
Encodes a list of children as one two or three byte offsets .
def encode_links(children, offsets, current):
  if not children[0]:
    assert len(children) == 1
    return []
  guess = 3 * len(children)
  assert children
  children = sorted(children, key = lambda x: -offsets[id(x)])
  while True:
    offset = current + guess
    buf = []
    for child in children:
      last = len(buf)
      distance = offset - offsets[id(child)]
      assert distance > 0 and distance < (1 << 21)
      if distance < (1 << 6):
        buf.append(distance)
      elif distance < (1 << 13):
        buf.append(0x40 | (distance >> 8))
        buf.append(distance & 0xFF)
      else:
        buf.append(0x60 | (distance >> 16))
        buf.append((distance >> 8) & 0xFF)
        buf.append(distance & 0xFF)
      offset -= distance
    if len(buf) == guess:
      break
    guess = len(buf)
  buf[last] |= (1 << 7)
  buf.reverse()
  return buf
def encode_links ( children , offsets , current ) : if not children [ 0 ] : assert len ( children ) == 1 return [ ] guess = 3 * len ( children ) assert children children = sorted ( children , key = lambda x : - offsets [ id ( x ) ] ) while True : offset = current + guess buf = [ ] for child in children : last = len ( buf ) distance = offset - offsets [ id ( child ) ] assert distance > 0 and distance < ( 1 << 21 ) if distance < ( 1 << 6 ) : buf . append ( distance ) elif distance < ( 1 << 13 ) : buf . append ( 0x40 | ( distance >> 8 ) ) buf . append ( distance & 0xFF ) else : buf . append ( 0x60 | ( distance >> 16 ) ) buf . append ( ( distance >> 8 ) & 0xFF ) buf . append ( distance & 0xFF ) offset -= distance if len ( buf ) == guess : break guess = len ( buf ) buf [ last ] |= ( 1 << 7 ) buf . reverse ( ) return buf
Encodes a list of children as one, two or three byte offsets.
Encodes a list of children as one two or three byte offsets .
"""Encodes a list of children as one, two or three byte offsets.""" # This is an <end_label> node and no links follow such nodes # A 6-bit offset: "s0xxxxxx" # A 13-bit offset: "s10xxxxxxxxxxxxx" # A 21-bit offset: "s11xxxxxxxxxxxxxxxxxxxxx" # Distance in first link is relative to following record. # Distance in other links are relative to previous link. # Set most significant bit to mark end of links in this node.
children
offsets
current