github.com/onflow/atree@v0.6.0/flag.go (about)

     1  /*
     2   * Atree - Scalable Arrays and Ordered Maps
     3   *
     4   * Copyright 2021 Dapper Labs, Inc.
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package atree
    20  
    21  type slabType int
    22  
    23  const (
    24  	slabTypeUndefined slabType = iota
    25  	slabArray
    26  	slabMap
    27  	slabStorable
    28  )
    29  
    30  type slabArrayType int
    31  
    32  const (
    33  	slabArrayUndefined slabArrayType = iota
    34  	slabArrayData
    35  	slabArrayMeta
    36  	slabLargeImmutableArray
    37  	slabBasicArray
    38  )
    39  
    40  type slabMapType int
    41  
    42  const (
    43  	slabMapUndefined slabMapType = iota
    44  	slabMapData
    45  	slabMapMeta
    46  	slabMapLargeEntry
    47  	slabMapCollisionGroup
    48  )
    49  
    50  const (
    51  	// Slab flags: 3 high bits
    52  	maskSlabRoot        byte = 0b100_00000
    53  	maskSlabHasPointers byte = 0b010_00000
    54  	maskSlabAnySize     byte = 0b001_00000
    55  
    56  	// Array flags: 3 low bits (4th and 5th bits are 0)
    57  	maskArrayData byte = 0b000_00000
    58  	maskArrayMeta byte = 0b000_00001
    59  	// maskLargeImmutableArray byte = 0b000_00010 // not used for now
    60  	maskBasicArray byte = 0b000_00011 // used for benchmarking
    61  
    62  	// Map flags: 3 low bits (4th bit is 0, 5th bit is 1)
    63  	maskMapData byte = 0b000_01000
    64  	maskMapMeta byte = 0b000_01001
    65  	// maskLargeMapEntry  byte = 0b000_01010 // not used for now
    66  	maskCollisionGroup byte = 0b000_01011
    67  
    68  	// Storable flags: 3 low bits (4th bit is 1, 5th bit is 1)
    69  	maskStorable byte = 0b000_11111
    70  )
    71  
    72  func setRoot(f byte) byte {
    73  	return f | maskSlabRoot
    74  }
    75  
    76  func setHasPointers(f byte) byte {
    77  	return f | maskSlabHasPointers
    78  }
    79  
    80  func setNoSizeLimit(f byte) byte {
    81  	return f | maskSlabAnySize
    82  }
    83  
    84  func isRoot(f byte) bool {
    85  	return f&maskSlabRoot > 0
    86  }
    87  
    88  func hasPointers(f byte) bool {
    89  	return f&maskSlabHasPointers > 0
    90  }
    91  
    92  func hasSizeLimit(f byte) bool {
    93  	return f&maskSlabAnySize == 0
    94  }
    95  
    96  func getSlabType(f byte) slabType {
    97  	// Extract 4th and 5th bits for slab type.
    98  	dataType := (f & byte(0b000_11000)) >> 3
    99  	switch dataType {
   100  	case 0:
   101  		// 4th and 5th bits are 0.
   102  		return slabArray
   103  	case 1:
   104  		// 4th bit is 0 and 5th bit is 1.
   105  		return slabMap
   106  	case 3:
   107  		// 4th and 5th bit are 1.
   108  		return slabStorable
   109  	default:
   110  		return slabTypeUndefined
   111  	}
   112  }
   113  
   114  func getSlabArrayType(f byte) slabArrayType {
   115  	if getSlabType(f) != slabArray {
   116  		return slabArrayUndefined
   117  	}
   118  
   119  	// Extract 3 low bits for slab array type.
   120  	dataType := (f & byte(0b000_00111))
   121  	switch dataType {
   122  	case 0:
   123  		return slabArrayData
   124  	case 1:
   125  		return slabArrayMeta
   126  	case 2:
   127  		return slabLargeImmutableArray
   128  	case 3:
   129  		return slabBasicArray
   130  	default:
   131  		return slabArrayUndefined
   132  	}
   133  }
   134  
   135  func getSlabMapType(f byte) slabMapType {
   136  	if getSlabType(f) != slabMap {
   137  		return slabMapUndefined
   138  	}
   139  
   140  	// Extract 3 low bits for slab map type.
   141  	dataType := (f & byte(0b000_00111))
   142  	switch dataType {
   143  	case 0:
   144  		return slabMapData
   145  	case 1:
   146  		return slabMapMeta
   147  	case 2:
   148  		return slabMapLargeEntry
   149  	case 3:
   150  		return slabMapCollisionGroup
   151  	default:
   152  		return slabMapUndefined
   153  	}
   154  }