T2 Prim<T1,T2>::min_spanning_tree(T1 source)
{
	SetKeyVal setKeyVal;

	// initialize: insert all elements in the set V-X
	for (T1 vertex=0; vertex != graph_.nVertex(); ++vertex)
	{
		T2 val = std::numeric_limits<T2>::max();
		if (vertex == source)
		{
			val = 0;
		}
		
		KeyVal keyVal(vertex, val);
		setKeyVal.insert(keyVal);
		// reset visited flag
		vecVisited_[vertex] = false;
		vecVal_[vertex] = val;
	}

	while (!setKeyVal.empty())
	{
		SetKeyVal::iterator beginIt = setKeyVal.begin();
		T1 curVertex = (*beginIt).key;
		vecVisited_[curVertex] = true;
		setKeyVal.erase(beginIt);

		// update val of the neighboring vertices
		std::vector<Edge<T1,T2>> vecOutEdges = graph_.outEdges(curVertex);
		for (std::vector<Edge<T1,T2>>::iterator edgeIt = vecOutEdges.begin(); edgeIt != vecOutEdges.end(); ++edgeIt)
		{
			T1 nextVertex = (*edgeIt).target();
			if (vecVisited_[nextVertex])
			{
				continue;
			}
			// If not visited, check if we need to update its val
			T2 weight = (*edgeIt).weight();
			if (weight < vecVal_[nextVertex])
			{
				//as an alternate to update we remove and insert back with updated weight
				SetKeyVal::iterator it = setKeyVal.find(KeyVal(nextVertex,vecVal_[nextVertex]));
				assert((it != setKeyVal.end()) && "element not present in set");
				setKeyVal.erase(it);
				setKeyVal.insert(KeyVal(nextVertex,weight));
				vecVal_[nextVertex] = weight;
			}
		}
	}

	// http://stackoverflow.com/questions/3221812/sum-of-elements-in-a-stdvector
	T2 sumVal = 0;
	for (std::vector<T2>::iterator valIt = vecVal_.begin(); valIt != vecVal_.end(); ++valIt)
	{
		sumVal += (*valIt);
	}

	return sumVal;
}
Example #2
0
int ConfParser::parse_rec( ConfBlock* currBlock,
                           int level,
                           int nLine,
                           ifstream& configFile,
                           const string& configFileName ) {

  // Read in config file linewise
  string l;
  while ( getline( configFile, l ) ) {

    ++nLine;

    // Remove leading and trailing whitespace
    trim( l );
    // If the line contained only whitspace we can skip it
    if ( l.empty() ) continue;

    // Ignore lines beginning with # or // as comments
    if ( starts_with( l, "#") || starts_with( l, "//" ) ) continue;

    // Check whether it is the end of a block
    if ( l.length() == 1 && l[0] == '}' ) {

      // Syntax error if we are at level 0
      if ( level == 0 ) {
        throw BadSyntax( configFileName, nLine,
            "Found closing block at outermost level" );
      }

      return nLine;
    }

    cmatch m;
    regex blockBegin( "^(\\w+)\\s*\\{$" );
    regex keyVal( "^(\\w+)\\s+(.*);" );

    // Check whether it is the beginning of a new block
    if ( regex_match( l.c_str(), m, blockBegin ) ) {

#ifdef DEBUG
        cout << "Adding Block " << m[1] << " at level " << level << " (line " << nLine << ")" << endl;
#endif

      nLine = parse_rec( &currBlock->addChild( m[1] ), level + 1, nLine, configFile, configFileName );

    // Check whether it is a key / value pair
    } else if ( regex_match( l.c_str(), m, keyVal ) ) {

      currBlock->addParam( m[1], m[2] );

    // Else we have a malformed expression and throw an exception
    } else {

      throw BadSyntax( configFileName, nLine, "Malformed expression" );

    }

  }

  // check if we are at outermost level again at the end
  if ( level != 0 )
    throw BadSyntax( configFileName, nLine, "Unexpected end of configuration file" );

  return nLine;
}
Example #3
0
already_AddRefed<IDBRequest>
IDBCursor::Update(JSContext* aCx, JS::Handle<JS::Value> aValue,
                  ErrorResult& aRv)
{
  AssertIsOnOwningThread();

  if (!mTransaction->IsOpen()) {
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
    return nullptr;
  }

  if (!mHaveValue || mType == Type_ObjectStoreKey || mType == Type_IndexKey) {
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
    return nullptr;
  }

  if (!mTransaction->IsWriteAllowed()) {
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_READ_ONLY_ERR);
    return nullptr;
  }

  MOZ_ASSERT(mType == Type_ObjectStore || mType == Type_Index);
  MOZ_ASSERT(!mKey.IsUnset());
  MOZ_ASSERT_IF(mType == Type_Index, !mPrimaryKey.IsUnset());

  IDBObjectStore* objectStore;
  if (mType == Type_ObjectStore) {
    objectStore = mSourceObjectStore;
  } else {
    objectStore = mSourceIndex->ObjectStore();
  }

  MOZ_ASSERT(objectStore);

  const Key& primaryKey = (mType == Type_ObjectStore) ? mKey : mPrimaryKey;

  nsRefPtr<IDBRequest> request;

  if (objectStore->HasValidKeyPath()) {
    // Make sure the object given has the correct keyPath value set on it.
    const KeyPath& keyPath = objectStore->GetKeyPath();
    Key key;

    aRv = keyPath.ExtractKey(aCx, aValue, key);
    if (aRv.Failed()) {
      return nullptr;
    }

    if (key != primaryKey) {
      aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
      return nullptr;
    }

    request = objectStore->AddOrPut(aCx,
                                    aValue,
                                    /* aKey */ JS::UndefinedHandleValue,
                                    /* aOverwrite */ true,
                                    /* aFromCursor */ true,
                                    aRv);
    if (aRv.Failed()) {
      return nullptr;
    }
  }
  else {
    JS::Rooted<JS::Value> keyVal(aCx);
    aRv = primaryKey.ToJSVal(aCx, &keyVal);
    if (aRv.Failed()) {
      return nullptr;
    }

    request = objectStore->AddOrPut(aCx,
                                    aValue,
                                    keyVal,
                                    /* aOverwrite */ true,
                                    /* aFromCursor */ true,
                                    aRv);
    if (aRv.Failed()) {
      return nullptr;
    }
  }

  request->SetSource(this);

  if (mType == Type_ObjectStore) {
    IDB_LOG_MARK("IndexedDB %s: Child  Transaction[%lld] Request[%llu]: "
                   "database(%s).transaction(%s).objectStore(%s)."
                   "cursor(%s).update(%s)",
                 "IndexedDB %s: C T[%lld] R[%llu]: IDBCursor.update()",
                 IDB_LOG_ID_STRING(),
                 mTransaction->LoggingSerialNumber(),
                 request->LoggingSerialNumber(),
                 IDB_LOG_STRINGIFY(mTransaction->Database()),
                 IDB_LOG_STRINGIFY(mTransaction),
                 IDB_LOG_STRINGIFY(objectStore),
                 IDB_LOG_STRINGIFY(mDirection),
                 IDB_LOG_STRINGIFY(objectStore, primaryKey));
  } else {
    IDB_LOG_MARK("IndexedDB %s: Child  Transaction[%lld] Request[%llu]: "
                   "database(%s).transaction(%s).objectStore(%s)."
                   "index(%s).cursor(%s).update(%s)",
                 "IndexedDB %s: C T[%lld] R[%llu]: IDBCursor.update()",
                 IDB_LOG_ID_STRING(),
                 mTransaction->LoggingSerialNumber(),
                 request->LoggingSerialNumber(),
                 IDB_LOG_STRINGIFY(mTransaction->Database()),
                 IDB_LOG_STRINGIFY(mTransaction),
                 IDB_LOG_STRINGIFY(objectStore),
                 IDB_LOG_STRINGIFY(mSourceIndex),
                 IDB_LOG_STRINGIFY(mDirection),
                 IDB_LOG_STRINGIFY(objectStore, primaryKey));
  }

  return request.forget();
}