コード例 #1
0
ファイル: NestedIterator.cpp プロジェクト: akash2504/leetcode
 void flattenList(vector<NestedInteger> &nestedList){
     for(int i=0;i<nestedList.size();i++){
         if(nestedList[i].isInteger())
            flat.push_back(nestedList[i].getInteger());
         else
            flattenList(nestedList[i].getList());
     }
 }
コード例 #2
0
 void flattenList(vector<NestedInteger> &list) {
   for (auto i : list) {
     if (i.isInteger()) {
       s.push(i);
     } else {
       flattenList(i.getList());
     }
   }
 }
コード例 #3
0
int main(int argc, const char * argv[]) {
    
    
    LinkedList list;
    
    LinkedList secondLevelChildListOne;
    LinkedList secondLevelChildListTwo;
    LinkedList thirdLevelChildListOne;
    LinkedList thirdLevelChildListTwo;
    LinkedList thirdLevelChildListThree;
    LinkedList fourthLevelChildListOne;
    LinkedList fourthLevelChildListTwo;
    //list.insertAfter(NULL, 1);
    //list.insertAfter(list.head, 2);
    //list.insertAfter(list.tail, 4);
    //list.insertAfter(list.head->next, 3);
    
    //list.remove(list.head);
    //list.remove(list.tail);
    //
    // printf("%p\n", (void*)list.head);
    // list.removeHead(NULL);
    //list.removeHead(&list.head);
    
    // make the first level list
    list.insertAfter(NULL, 5);
    list.insertAfter(list.head, 33);
    list.insertAfter(list.head->next, 17);
    list.insertAfter(list.head->next->next, 2);
    list.insertAfter(list.head->next->next->next, 1);
    
    
    
    // make the first child level-2 list
    secondLevelChildListOne.insertAfter(NULL, 6);
    secondLevelChildListOne.insertAfter(secondLevelChildListOne.head, 25);
    secondLevelChildListOne.insertAfter(secondLevelChildListOne.head->next, 6);
    
    list.head->child = secondLevelChildListOne.head;
    
    
    // make the second child level-2 list
    secondLevelChildListTwo.insertAfter(NULL, 2);
    secondLevelChildListTwo.insertAfter(secondLevelChildListTwo.head , 7);
    
    list.head->next->next->next->child = secondLevelChildListTwo.head;

    flattenList(list.head, &list.tail);
    
    list.printAllNodesInList();
    
    unflattenList(list.head, &list.tail);
    
    list.printAllNodesInList();
    return 0;
}
コード例 #4
0
int main ()
{
	/*
	Making the following list:
	1->2->3->4->5
	|        |
	6->7	 8->9
				|
				10
	*/

	struct node *myList_one = newNode(1);
	struct node *myList_two = newNode(2);
	struct node *myList_three = newNode(3);
	struct node *myList_four = newNode(4);
	struct node *myList_five = newNode(5);
	struct node *myList_six = newNode(6);
	struct node *myList_seven = newNode(7);
	struct node *myList_eight = newNode(8);
	struct node *myList_nine = newNode(9);
	struct node *myList_ten = newNode(10);

	myList_one->next = myList_two;
	myList_two->next = myList_three;
	myList_three->next = myList_four;
	myList_four->next = myList_five;
	myList_one->child = myList_six;
	myList_six->next = myList_seven;
	myList_four->child = myList_eight;
	myList_eight->next = myList_nine;
	myList_nine->child = myList_ten;

	myList_one = flattenList(myList_one);
	printList(myList_one);
	return 0;
}
コード例 #5
0
ファイル: MultilevelList.c プロジェクト: RAJU009F/my-work
int main()
	{
	
		struct Node *head = NULL;
		push(&head, 11);
		push(&head, 7);
		push(&head, 12);
		push(&head, 5);
		push(&head, 10);
		
		// struct Node *head1 = NULL;
		
		// push(&head1, 13);
		// push(&head1, 20);
		// push(&head1, 4);
		// struct Node *head2 = NULL;
		// push(&head2, 6);
		// push(&head2, 17);
		// struct Node *head3 = NULL;
		// push(&head3, 8);
		// push(&head3, 9);
		// struct Node *head4 = NULL;
		// push(&head4, 15);
		// push(&head4, 19);
		// struct Node *head5 = NULL;
		// push(&head5, 2);
		// struct Node *head6 = NULL;
		// push(&head6, 16);
		// struct Node *head7 = NULL;
		// push(&head7, 3);
		
		
		
		// head->c = head1;
		// head->next->next->next->c= head2;
		// head2->c =  head3;
		// head3->c = head4;
		// head1->next->c =  head5;
		// head1->next->next->c =  head6;
		// head6->c = head7;
		
		push(&(head->c->next), 101000);
		push(&(head->c->next), 10100);
		push(&(head->c->next), 1010);
		push(&(head->c->next), 101);
		
		push(&(head->next), 2);
		push(&(head->next->c), 200);
		push(&(head->next->c), 2000);
		push(&(head->next->c), 20000);
		push(&(head->next->c), 1200);
		push(&(head->next->c->c), 12030);
		
		push(&(head->next->next), 3);
		push(&(head->next->next->next), 4);
		push(&(head->next->next->next->next), 5);
		push(&(head->next->next->next->next->next), 6);
		push(&(head->next->next->next->next->next->next), 7);
		push(&(head->next->next->next->next->next->next->next), 8232323);
		push(&(head->next->next->next->next->next->next->next->c), 8444);
		push(&(head->next->next->next->next->next->next->next->c), 87777);
		push(&(head->next->next->next->next->next->next->next->c), 877);
		push(&(head->next->next->next->next->next->next->next->c), 8);
		push(&(head->next->next->next->next->next->next->next->c->next), 80000);
		push(&(head->next->next->next->next->next->next->next->c->next->c), 80002);
		push(&(head->next->next->next->next->next->next->next->c->next->c), 80001);
		flattenList(&head);
		printList(head);
	}
コード例 #6
0
void SSLContextManager::addSSLContextConfig(
  const SSLContextConfig& ctxConfig,
  const SSLCacheOptions& cacheOptions,
  const TLSTicketKeySeeds* ticketSeeds,
  const folly::SocketAddress& vipAddress,
  const std::shared_ptr<SSLCacheProvider>& externalCache) {

  unsigned numCerts = 0;
  std::string commonName;
  std::string lastCertPath;
  std::unique_ptr<std::list<std::string>> subjectAltName;
  auto sslCtx = std::make_shared<SSLContext>(ctxConfig.sslVersion);
  for (const auto& cert : ctxConfig.certificates) {
    try {
      sslCtx->loadCertificate(cert.certPath.c_str());
    } catch (const std::exception& ex) {
      // The exception isn't very useful without the certificate path name,
      // so throw a new exception that includes the path to the certificate.
      string msg = folly::to<string>("error loading SSL certificate ",
                                     cert.certPath, ": ",
                                     folly::exceptionStr(ex));
      LOG(ERROR) << msg;
      throw std::runtime_error(msg);
    }

    // Verify that the Common Name and (if present) Subject Alternative Names
    // are the same for all the certs specified for the SSL context.
    numCerts++;
    X509* x509 = getX509(sslCtx->getSSLCtx());
    auto guard = folly::makeGuard([x509] { X509_free(x509); });
    auto cn = SSLUtil::getCommonName(x509);
    if (!cn) {
      throw std::runtime_error(folly::to<string>("Cannot get CN for X509 ",
                                                 cert.certPath));
    }
    auto altName = SSLUtil::getSubjectAltName(x509);
    VLOG(2) << "cert " << cert.certPath << " CN: " << *cn;
    if (altName) {
      altName->sort();
      VLOG(2) << "cert " << cert.certPath << " SAN: " << flattenList(*altName);
    } else {
      VLOG(2) << "cert " << cert.certPath << " SAN: " << "{none}";
    }
    if (numCerts == 1) {
      commonName = *cn;
      subjectAltName = std::move(altName);
    } else {
      if (commonName != *cn) {
        throw std::runtime_error(folly::to<string>("X509 ", cert.certPath,
                                          " does not have same CN as ",
                                          lastCertPath));
      }
      if (altName == nullptr) {
        if (subjectAltName != nullptr) {
          throw std::runtime_error(folly::to<string>("X509 ", cert.certPath,
                                            " does not have same SAN as ",
                                            lastCertPath));
        }
      } else {
        if ((subjectAltName == nullptr) || (*altName != *subjectAltName)) {
          throw std::runtime_error(folly::to<string>("X509 ", cert.certPath,
                                            " does not have same SAN as ",
                                            lastCertPath));
        }
      }
    }
    lastCertPath = cert.certPath;

    // TODO t4438250 - Add ECDSA support to the crypto_ssl offload server
    //                 so we can avoid storing the ECDSA private key in the
    //                 address space of the Internet-facing process.  For
    //                 now, if cert name includes "-EC" to denote elliptic
    //                 curve, we load its private key even if the server as
    //                 a whole has been configured for async crypto.
    if (ctxConfig.isLocalPrivateKey ||
        (cert.certPath.find("-EC") != std::string::npos)) {
      // The private key lives in the same process

      // This needs to be called before loadPrivateKey().
      if (!cert.passwordPath.empty()) {
        auto sslPassword = std::make_shared<PasswordInFile>(cert.passwordPath);
        sslCtx->passwordCollector(sslPassword);
      }

      try {
        sslCtx->loadPrivateKey(cert.keyPath.c_str());
      } catch (const std::exception& ex) {
        // Throw an error that includes the key path, so the user can tell
        // which key had a problem.
        string msg = folly::to<string>("error loading private SSL key ",
                                       cert.keyPath, ": ",
                                       folly::exceptionStr(ex));
        LOG(ERROR) << msg;
        throw std::runtime_error(msg);
      }
    }
  }
  if (!ctxConfig.isLocalPrivateKey) {
    enableAsyncCrypto(sslCtx);
  }

  // Let the server pick the highest performing cipher from among the client's
  // choices.
  //
  // Let's use a unique private key for all DH key exchanges.
  //
  // Because some old implementations choke on empty fragments, most SSL
  // applications disable them (it's part of SSL_OP_ALL).  This
  // will improve performance and decrease write buffer fragmentation.
  sslCtx->setOptions(SSL_OP_CIPHER_SERVER_PREFERENCE |
    SSL_OP_SINGLE_DH_USE |
    SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);

  // Configure SSL ciphers list
  if (!ctxConfig.tls11Ciphers.empty()) {
    // FIXME: create a dummy SSL_CTX for cipher testing purpose? It can
    //        remove the ordering dependency

    // Test to see if the specified TLS1.1 ciphers are valid.  Note that
    // these will be overwritten by the ciphers() call below.
    sslCtx->setCiphersOrThrow(ctxConfig.tls11Ciphers);
  }

  // Important that we do this *after* checking the TLS1.1 ciphers above,
  // since we test their validity by actually setting them.
  sslCtx->ciphers(ctxConfig.sslCiphers);

  // Use a fix DH param
  DH* dh = get_dh2048();
  SSL_CTX_set_tmp_dh(sslCtx->getSSLCtx(), dh);
  DH_free(dh);

  const string& curve = ctxConfig.eccCurveName;
  if (!curve.empty()) {
    set_key_from_curve(sslCtx->getSSLCtx(), curve);
  }

  if (!ctxConfig.clientCAFile.empty()) {
    try {
      sslCtx->setVerificationOption(SSLContext::VERIFY_REQ_CLIENT_CERT);
      sslCtx->loadTrustedCertificates(ctxConfig.clientCAFile.c_str());
      sslCtx->loadClientCAList(ctxConfig.clientCAFile.c_str());
    } catch (const std::exception& ex) {
      string msg = folly::to<string>("error loading client CA",
                                     ctxConfig.clientCAFile, ": ",
                                     folly::exceptionStr(ex));
      LOG(ERROR) << msg;
      throw std::runtime_error(msg);
    }
  }

  // - start - SSL session cache config
  // the internal cache never does what we want (per-thread-per-vip).
  // Disable it.  SSLSessionCacheManager will set it appropriately.
  SSL_CTX_set_session_cache_mode(sslCtx->getSSLCtx(), SSL_SESS_CACHE_OFF);
  SSL_CTX_set_timeout(sslCtx->getSSLCtx(),
                      cacheOptions.sslCacheTimeout.count());
  std::unique_ptr<SSLSessionCacheManager> sessionCacheManager;
  if (ctxConfig.sessionCacheEnabled &&
      cacheOptions.maxSSLCacheSize > 0 &&
      cacheOptions.sslCacheFlushSize > 0) {
    sessionCacheManager =
      folly::make_unique<SSLSessionCacheManager>(
        cacheOptions.maxSSLCacheSize,
        cacheOptions.sslCacheFlushSize,
        sslCtx.get(),
        vipAddress,
        commonName,
        eventBase_,
        stats_,
        externalCache);
  }
  // - end - SSL session cache config

  std::unique_ptr<TLSTicketKeyManager> ticketManager =
    createTicketManagerHelper(sslCtx, ticketSeeds, ctxConfig, stats_);

  // finalize sslCtx setup by the individual features supported by openssl
  ctxSetupByOpensslFeature(sslCtx, ctxConfig);

  try {
    insert(sslCtx,
           std::move(sessionCacheManager),
           std::move(ticketManager),
           ctxConfig.isDefault);
  } catch (const std::exception& ex) {
    string msg = folly::to<string>("Error adding certificate : ",
                                   folly::exceptionStr(ex));
    LOG(ERROR) << msg;
    throw std::runtime_error(msg);
  }

}
コード例 #7
0
 NestedIterator(vector<NestedInteger> &nestedList) {
   //put everything as an integer in the queue
   //if the element in the nested list is an integer put it as it is, otherwise flatten the list and put all
   //the integer element in the stack
   flattenList(nestedList);
 }
コード例 #8
0
ファイル: NestedIterator.cpp プロジェクト: akash2504/leetcode
 NestedIterator(vector<NestedInteger> &nestedList) {
     flattenList(nestedList);
 }