Ejemplo n.º 1
0
Node_t* leastCommonAncestor(Node_t* root, Node_t* node1, Node_t* node2) {
  // Both node1 and node2 belongs to left side.
  if (isChild(root->left, node1) && 
      isChild(root->left, node2))
    return leastCommonAncestor(root->left, node1, node2);
  // Both node1 and node2 belongs to right side.
  else if(isChild(root->right, node1)&&
          isChild(root->right, node2))
    return leastCommonAncestor(root->right, node1, node2);
  else
    return root;
}
Ejemplo n.º 2
0
String TypeSet::displayName() const
{
    if (m_seenTypes == TypeNothing)
        return emptyString();

    if (m_structureHistory.size() && doesTypeConformTo(TypeObject | TypeNull | TypeUndefined)) {
        String ctorName = leastCommonAncestor(); 

        if (doesTypeConformTo(TypeObject))
            return ctorName;
        if (doesTypeConformTo(TypeObject | TypeNull | TypeUndefined))
            return ctorName + '?';
    }

    // The order of these checks are important. For example, if a value is only a function, it conforms to TypeFunction, but it also conforms to TypeFunction | TypeNull.
    // Therefore, more specific types must be checked first.

    if (doesTypeConformTo(TypeFunction))
        return ASCIILiteral("Function");
    if (doesTypeConformTo(TypeUndefined))
        return ASCIILiteral("Undefined");
    if (doesTypeConformTo(TypeNull))
        return ASCIILiteral("Null");
    if (doesTypeConformTo(TypeBoolean))
        return ASCIILiteral("Boolean");
    if (doesTypeConformTo(TypeMachineInt))
        return ASCIILiteral("Integer");
    if (doesTypeConformTo(TypeNumber | TypeMachineInt))
        return ASCIILiteral("Number");
    if (doesTypeConformTo(TypeString))
        return ASCIILiteral("String");
    if (doesTypeConformTo(TypeSymbol))
        return ASCIILiteral("Symbol");

    if (doesTypeConformTo(TypeNull | TypeUndefined))
        return ASCIILiteral("(?)");

    if (doesTypeConformTo(TypeFunction | TypeNull | TypeUndefined))
        return ASCIILiteral("Function?");
    if (doesTypeConformTo(TypeBoolean | TypeNull | TypeUndefined))
        return ASCIILiteral("Boolean?");
    if (doesTypeConformTo(TypeMachineInt | TypeNull | TypeUndefined))
        return ASCIILiteral("Integer?");
    if (doesTypeConformTo(TypeNumber | TypeMachineInt | TypeNull | TypeUndefined))
        return ASCIILiteral("Number?");
    if (doesTypeConformTo(TypeString | TypeNull | TypeUndefined))
        return ASCIILiteral("String?");
    if (doesTypeConformTo(TypeSymbol | TypeNull | TypeUndefined))
        return ASCIILiteral("Symbol?");
   
    if (doesTypeConformTo(TypeObject | TypeFunction | TypeString))
        return ASCIILiteral("Object");
    if (doesTypeConformTo(TypeObject | TypeFunction | TypeString | TypeNull | TypeUndefined))
        return ASCIILiteral("Object?");

    return ASCIILiteral("(many)");
}
int leastCommonAncestor(node* root, int n1, int n2){
	if(root == NULL || root->data == n1 || root->data == n2)
		return -1;
	if((root->right != NULL && (root->right->data == n1 || root->right->data == n2)){
		return root->data;
	}
	if((root->left != NULL && (root->left->data == n1 || root->right->data == n2)){
		return root->data;
	}
	if(root->data > n1 && root->data < n2){
		return root->data;
	}
	if(root->data >n1 && root->data > n2){
		return leastCommonAncestor(root->left, n1, n2);
	}
	if(root->data <n1 && root->data < n2 ){
		return leastCommonAncestor(root->right, n1, n2);
	}
}
Ejemplo n.º 4
0
//n1<n2
struct node* leastCommonAncestor(struct node* root, int n1, int n2){
  /* If we have reached a leaf node then LCA doesn't exist
     If root->data is equal to any of the inputs then input is
     not valid. For example 20, 22 in the given figure */
  if(root == NULL || root->data == n1 || root->data == n2)
    return NULL; 
 
  /* If any of the input nodes is child of the current node
     we have reached the LCA. For example, in the above figure
     if we want to calculate LCA of 12 and 14, recursion should
     terminate when we reach 8*/
     /*
  if((root->right != NULL) && (root->right->data == n1 || root->right->data == n2))
    return root;
  if((root->left != NULL) &&(root->left->data == n1 || root->left->data == n2))
    return root;    
 */
  if(root->data > n1 && root->data < n2)
    return root;
  if(root->data > n1 && root->data > n2)
    return leastCommonAncestor(root->left, n1, n2);
  if(root->data < n1 && root->data < n2)
    return leastCommonAncestor(root->right, n1, n2);
}
Ejemplo n.º 5
0
String TypeSet::dumpTypes() const
{
    if (m_seenTypes == TypeNothing)
        return ASCIILiteral("(Unreached Statement)");

    StringBuilder seen;

    if (m_seenTypes & TypeFunction)
        seen.appendLiteral("Function ");
    if (m_seenTypes & TypeUndefined)
        seen.appendLiteral("Undefined ");
    if (m_seenTypes & TypeNull)
        seen.appendLiteral("Null ");
    if (m_seenTypes & TypeBoolean)
        seen.appendLiteral("Boolean ");
    if (m_seenTypes & TypeMachineInt)
        seen.appendLiteral("MachineInt ");
    if (m_seenTypes & TypeNumber)
        seen.appendLiteral("Number ");
    if (m_seenTypes & TypeString)
        seen.appendLiteral("String ");
    if (m_seenTypes & TypeObject)
        seen.appendLiteral("Object ");
    if (m_seenTypes & TypeSymbol)
        seen.appendLiteral("Symbol ");

    for (size_t i = 0; i < m_structureHistory.size(); i++) {
        RefPtr<StructureShape> shape = m_structureHistory.at(i);
        seen.append(shape->m_constructorName);
        seen.append(' ');
    }

    if (m_structureHistory.size()) 
        seen.appendLiteral("\nStructures:[ ");
    for (size_t i = 0; i < m_structureHistory.size(); i++) {
        seen.append(m_structureHistory.at(i)->stringRepresentation());
        seen.append(' ');
    }
    if (m_structureHistory.size())
        seen.append(']');

    if (m_structureHistory.size()) {
        seen.appendLiteral("\nLeast Common Ancestor: ");
        seen.append(leastCommonAncestor());
    }

    return seen.toString();
}