本文整理汇总了C++中TIntH::Len方法的典型用法代码示例。如果您正苦于以下问题:C++ TIntH::Len方法的具体用法?C++ TIntH::Len怎么用?C++ TIntH::Len使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TIntH
的用法示例。
在下文中一共展示了TIntH::Len方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetGroupFarnessCentr
double GetGroupFarnessCentr(const PUNGraph& Graph, const TIntH& GroupNodes) {
TIntH* NDistH = new TIntH[GroupNodes.Len()];
for (int i = 0; i<GroupNodes.Len(); i++){
NDistH[i](Graph->GetNodes());
TSnap::GetShortPath<PUNGraph>(Graph, GroupNodes.GetDat(i), NDistH[i], true, TInt::Mx);
}
int min, dist, sum = 0, len = 0;
for (PUNGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
if (NDistH[0].IsKey(NI.GetId()))
min = NDistH[0].GetDat(NI.GetId());
else
min = -1;
for (int j = 1; j<GroupNodes.Len(); j++){
if (NDistH[j].IsKey(NI.GetId()))
dist = NDistH[j].GetDat(NI.GetId());
else
dist = -1;
if ((dist < min && dist != -1) || (dist > min && min == -1))
min = dist;
}
if (min>0){
sum += min;
len++;
}
}
if (len > 0) { return sum / double(len); }
else { return 0.0; }
}
示例2: BurnExpFire
// burn each link independently (forward with FwdBurnProb, backward with BckBurnProb)
void TForestFire::BurnExpFire() {
const double OldFwdBurnProb = FwdBurnProb;
const double OldBckBurnProb = BckBurnProb;
const int NInfect = InfectNIdV.Len();
const TNGraph& G = *Graph;
TIntH BurnedNIdH; // burned nodes
TIntV BurningNIdV = InfectNIdV; // currently burning nodes
TIntV NewBurnedNIdV; // nodes newly burned in current step
bool HasAliveNbrs; // has unburned neighbors
int NBurned = NInfect, NDiedFire=0;
for (int i = 0; i < InfectNIdV.Len(); i++) {
BurnedNIdH.AddDat(InfectNIdV[i]); }
NBurnedTmV.Clr(false); NBurningTmV.Clr(false); NewBurnedTmV.Clr(false);
for (int time = 0; ; time++) {
NewBurnedNIdV.Clr(false);
// for each burning node
for (int node = 0; node < BurningNIdV.Len(); node++) {
const int& BurningNId = BurningNIdV[node];
const TNGraph::TNodeI Node = G.GetNI(BurningNId);
HasAliveNbrs = false;
NDiedFire = 0;
// burn forward links (out-links)
for (int e = 0; e < Node.GetOutDeg(); e++) {
const int OutNId = Node.GetOutNId(e);
if (! BurnedNIdH.IsKey(OutNId)) { // not yet burned
HasAliveNbrs = true;
if (Rnd.GetUniDev() < FwdBurnProb) {
BurnedNIdH.AddDat(OutNId); NewBurnedNIdV.Add(OutNId); NBurned++; }
}
}
// burn backward links (in-links)
if (BckBurnProb > 0.0) {
for (int e = 0; e < Node.GetInDeg(); e++) {
const int InNId = Node.GetInNId(e);
if (! BurnedNIdH.IsKey(InNId)) { // not yet burned
HasAliveNbrs = true;
if (Rnd.GetUniDev() < BckBurnProb) {
BurnedNIdH.AddDat(InNId); NewBurnedNIdV.Add(InNId); NBurned++; }
}
}
}
if (! HasAliveNbrs) { NDiedFire++; }
}
NBurnedTmV.Add(NBurned);
NBurningTmV.Add(BurningNIdV.Len() - NDiedFire);
NewBurnedTmV.Add(NewBurnedNIdV.Len());
//BurningNIdV.AddV(NewBurnedNIdV); // node is burning eternally
BurningNIdV.Swap(NewBurnedNIdV); // node is burning just 1 time step
if (BurningNIdV.Empty()) break;
FwdBurnProb = FwdBurnProb * ProbDecay;
BckBurnProb = BckBurnProb * ProbDecay;
}
BurnedNIdV.Gen(BurnedNIdH.Len(), 0);
for (int i = 0; i < BurnedNIdH.Len(); i++) {
BurnedNIdV.Add(BurnedNIdH.GetKey(i)); }
FwdBurnProb = OldFwdBurnProb;
BckBurnProb = OldBckBurnProb;
}
示例3: GetGroupDegreeCentr0
double GetGroupDegreeCentr0(const PUNGraph& Graph, const TIntH& GroupNodes) {
int deg;
TIntH NN;
for (int i = 0; i<GroupNodes.Len(); i++) {
deg = Graph->GetNI(GroupNodes.GetDat(i)).GetDeg();
for (int j = 0; j < deg; j++) {
if (GroupNodes.IsKey(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j)) == 0)
NN.AddDat(Graph->GetNI(GroupNodes.GetDat(i)).GetNbrNId(j), GroupNodes.GetDat(i));
}
}
return (double)NN.Len();
}
示例4: Add
void TFtrGenToken::Add(const TStr& Val, TIntFltKdV& SpV, int& Offset) const {
// step (1): tokenize
TStrV TokenStrV; GetTokenV(Val, TokenStrV);
// step (2): aggregate token counts
TIntH TokenFqH;
for (int TokenStrN = 0; TokenStrN < TokenStrV.Len(); TokenStrN++) {
const TStr& TokenStr = TokenStrV[TokenStrN];
if (TokenH.IsKey(TokenStr)) {
const int TokenId = TokenH.GetKeyId(TokenStr);
TokenFqH.AddDat(TokenId)++;
}
}
// step (3): make a sparse vector out of it
TIntFltKdV ValSpV(TokenFqH.Len(), 0);
int KeyId = TokenFqH.FFirstKeyId();
while (TokenFqH.FNextKeyId(KeyId)) {
const int TokenId = TokenFqH.GetKey(KeyId);
const int TokenFq = TokenFqH[KeyId];
const int TokenDocFq = TokenH[TokenId];
const double IDF = log(double(Docs) / double(TokenDocFq));
ValSpV.Add(TIntFltKd(TokenId, double(TokenFq) * IDF));
}
ValSpV.Sort(); TLinAlg::NormalizeL1(ValSpV);
// step (4): add the sparse vector to the final feature vector
for (int ValSpN = 0; ValSpN < ValSpV.Len(); ValSpN++) {
const int Key = ValSpV[ValSpN].Key + Offset;
const double Dat = ValSpV[ValSpN].Dat;
SpV.Add(TIntFltKd(Key, Dat));
}
Offset += TokenH.Len();
}
示例5: DocStrToWIdV
//////////////////////////////////////////////////////////////////////////
// String-To-Words
void TStrParser::DocStrToWIdV(const TStr& _DocStr, TIntV& WordIdV, const bool& Stemm) {
TStr DocStr = _DocStr.GetUc(); // to upper case
TStrV WordV; DocStr.SplitOnWs(WordV); int WordN = WordV.Len();
WordIdV.Reserve(WordN, 0);
PStemmer Stemmer = TStemmer::New(stmtPorter);
TIntH WordsInDoc;
for (int WordC = 0; WordC < WordN; WordC++) {
TStr WordStr;
if (Stemm) {
WordStr = Stemmer->GetStem(WordV[WordC]);
} else {
WordStr = WordV[WordC];
}
int WId = GetWId(WordStr);
if (WId == -1) {
WId = WordToIdH.AddKey(WordStr);
WordToIdH[WId] = 0;
}
WordIdV.Add(WId);
// is it first time we see this word in this doc?
if (!WordsInDoc.IsKey(WId)) WordsInDoc.AddKey(WId);
}
//do some statistics for DF
DocsParsed++;
for (int i = 0, l = WordsInDoc.Len(); i < l; i++)
WordToIdH[WordsInDoc.GetKey(i)]++;
Assert(WordV.Len() == WordIdV.Len());
}
示例6: RunSICascade2
// simulate SI model cascade using infection probability Beta until the cascade stops or reaches size MxCascSz
PNGraph RunSICascade2(PUNGraph G, const double& Beta, const int& MxCascSz, TIntH& NIdInfTmH) {
PNGraph Casc = TNGraph::New();
const int StartNId = G->GetRndNId();
Casc->AddNode(StartNId);
NIdInfTmH.AddDat(StartNId, NIdInfTmH.Len());
TIntQ Q; Q.Push(StartNId);
while (! Q.Empty()) {
const TUNGraph::TNodeI NI = G->GetNI(Q.Top()); Q.Pop();
for (int i = 0; i < NI.GetOutDeg(); i++) {
if (TInt::Rnd.GetUniDev() < Beta && ! NIdInfTmH.IsKey(NI.GetOutNId(i))) {
Casc->AddNode(NI.GetOutNId(i));
NIdInfTmH.AddDat(NI.GetOutNId(i), NIdInfTmH.Len());
Casc->AddEdge(NI.GetId(), NI.GetOutNId(i));
if (Casc->GetNodes() == MxCascSz) { return Casc; }
Q.Push(NI.GetOutNId(i));
}
}
}
return Casc;
}
示例7: GetGroupDegreeCentr
double GetGroupDegreeCentr(const PUNGraph& Graph, const PUNGraph& Group) {
int deg;
TIntH NN;
for (TUNGraph::TNodeI NI = Group->BegNI(); NI < Group->EndNI(); NI++) {
deg = Graph->GetNI(NI.GetId()).GetDeg();
for (int i = 0; i<deg; i++) {
if (Group->IsNode(Graph->GetNI(NI.GetId()).GetNbrNId(i)) == 0)
NN.AddDat(Graph->GetNI(NI.GetId()).GetNbrNId(i), NI.GetId());
}
}
return (double)NN.Len();
}
示例8: RunSICascade
// simulate SI model cascade using infection probability Beta until the cascade reaches size CascSz
PNGraph RunSICascade(PUNGraph G, const double& Beta, const int& CascSz, TIntH& NIdInfTmH) {
PNGraph Casc = TNGraph::New();
const int StartId = G->GetRndNId();
Casc->AddNode(StartId);
NIdInfTmH.AddDat(StartId, NIdInfTmH.Len());
for (int X = 0; X < 10*CascSz; X++) {
TIntV CascNIdV; Casc->GetNIdV(CascNIdV);
for (int n = 0; n < CascNIdV.Len(); n++) {
const TUNGraph::TNodeI NI = G->GetNI(CascNIdV[n]);
for (int i = 0; i < NI.GetOutDeg(); i++) {
if (Casc->IsNode(NI.GetOutNId(i))) { continue; }
if (TInt::Rnd.GetUniDev() < Beta) {
Casc->AddNode(NI.GetOutNId(i));
NIdInfTmH.AddDat(NI.GetOutNId(i), NIdInfTmH.Len());
Casc->AddEdge(NI.GetId(), NI.GetOutNId(i));
if (Casc->GetNodes() == CascSz) { return Casc; }
}
}
}
}
return Casc;
}
示例9: GetAddIdFreqs
void TStrFeatureSpace::GetAddIdFreqs(const TStrV& Features, TVec<TKeyDat<TStrFSSize, TInt> >& IdFreqs) {
TIntH Freqs;
for (int i = 0; i < Features.Len(); i++) {
int Id = GetAddId(Features[i]);
TInt Freq = 0;
if (Freqs.IsKeyGetDat(Id, Freq)) {
Freqs.AddDat(Id, Freq + 1);
} else {
Freqs.AddDat(Id, 1);
}
}
IdFreqs.Gen(Freqs.Len());
for (int i = 0; i < Freqs.Len(); i++) {
TInt Key, Freq;
Freqs.GetKeyDat(i, Key, Freq);
IdFreqs[i].Key = Key;
IdFreqs[i].Dat = Freq;
}
IdFreqs.Sort();
}
示例10: GetIdFreqs
void TStrFeatureSpace::GetIdFreqs(const TVec<const char *>& Features, TVec<TKeyDat<TStrFSSize, TInt> >& IdFreqs) const {
TIntH Freqs;
for (int i = 0; i < Features.Len(); i++) {
TInt Id;
if (GetIfExistsId(Features[i], Id)) {
TInt Freq = 0;
if (Freqs.IsKeyGetDat(Id, Freq)) {
Freqs.AddDat(Id, Freq + 1);
} else {
Freqs.AddDat(Id, 1);
}
}
}
IdFreqs.Gen(Freqs.Len());
for (int i = 0; i < Freqs.Len(); i++) {
TInt Key, Freq;
Freqs.GetKeyDat(i, Key, Freq);
IdFreqs[i].Key = Key;
IdFreqs[i].Dat = Freq;
}
IdFreqs.Sort();
}
示例11: GetSupport
int TTrawling::GetSupport(const TIntV& ItemSet) {
int Cnt = 0;
/*for (int s = 0; s < NIdSetH.Len(); s++) {
if (IsIn(NIdSetH[s], ItemSet)) { Cnt++; }
}*/
TIntH SetCntH;
for (int i = 0; i < ItemSet.Len(); i++) {
const TIntV& NIdV = SetNIdH.GetDat(ItemSet[i]);
for (int n = 0; n < NIdV.Len(); n++) {
SetCntH.AddDat(NIdV[n]) += 1;
}
}
for (int i = 0; i < SetCntH.Len(); i++) {
if (SetCntH[i]==ItemSet.Len()) { Cnt++; }
}
return Cnt;
}
示例12: UF
int TTop2FriendNet::GetTop2WccSz(const double ProbPick2nd) const {
TUnionFind UF(Top1UF);
for (int n = 0; n < Top2NIdH.Len(); n++) {
if (TInt::Rnd.GetUniDev() <= ProbPick2nd && Top2NIdH[n].Val2 != -1) {
UF.Union(Top2NIdH.GetKey(n), Top2NIdH[n].Val2);
}
}
TIntH CcSzH;
for (int i = 0; i < UF.Len(); i++) {
CcSzH.AddDat(UF.Find(UF.GetKeyI(i))) += 1;
}
int MxCcSz = -1;
for (int c = 0; c < CcSzH.Len(); c++) {
if (CcSzH[c] > MxCcSz) { MxCcSz = CcSzH[c]; }
}
return MxCcSz;
}
示例13: AddFtr
void TBagOfWords::AddFtr(const TStrV& TokenStrV, TIntFltKdV& SpV) const {
// aggregate token counts
TIntH TermFqH;
TStrV NgramStrV;
GenerateNgrams(TokenStrV, NgramStrV);
for (int TokenStrN = 0; TokenStrN < NgramStrV.Len(); TokenStrN++) {
const TStr& TokenStr = NgramStrV[TokenStrN];
// get token ID
const int TokenId = IsHashing() ?
(TokenStr.GetHashTrick() % HashDim) : // hashing
TokenSet.GetKeyId(TokenStr); // vocabulary
// add if known token
if (TokenId != -1) {
TermFqH.AddDat(TokenId)++;
}
}
// make a sparse vector out of it
SpV.Gen(TermFqH.Len(), 0);
int KeyId = TermFqH.FFirstKeyId();
while (TermFqH.FNextKeyId(KeyId)) {
const int TermId = TermFqH.GetKey(KeyId);
double TermVal = 1.0;
if (IsTf()) { TermVal *= double(TermFqH[KeyId]); }
if (IsIdf()) {
if (ForgetP) {
const double DocFq = double(DocFqV[TermId]) + OldDocFqV[TermId];
if (DocFq > 0.1) { TermVal *= log((double(Docs) + OldDocs) / DocFq); }
} else {
TermVal *= log(double(Docs) / double(DocFqV[TermId]));
}
}
SpV.Add(TIntFltKd(TermId, TermVal));
}
SpV.Sort();
// step (4): normalize the vector if so required
if (IsNormalize()) { TLinAlg::Normalize(SpV); }
}
示例14: FastCorePeripheryGC
int FastCorePeripheryGC(PUNGraph& Graph, TIntIntH& out) {
TIntH GroupNodes; // buildup cpntainer of group nodes
int *NNodes = new int[Graph->GetNodes()]; // container of neighbouring nodes
int NNodes_br = 0;
TIntIntH nodes;
TIntIntH nodesIds;
double Z=0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { // Calculate and store the degrees of each node.
int deg = NI.GetDeg();
int id = NI.GetId();
Z += deg;
nodes.AddDat(id,deg);
}
Z = Z/2;
nodes.SortByDat(false); // Then sort the nodes in descending order of degree, to get a list of nodes {v1, v2, . . . , vn}.
int br1=0;
for (THashKeyDatI<TInt,TInt> NI = nodes.BegI(); NI < nodes.EndI(); NI++) {
nodesIds.AddDat(NI.GetKey(),NI.GetKey());
br1++;
}
double Zbest = 99999900000000000;
//int kbest;
//int olddeg;
int br=0;
for (int k=0; k<nodes.Len(); k++) {
if (k<nodes.Len()-1) {
if (nodes[k]==nodes[k+1]) { // go into same deg mode
int kmin=-2;
int knew=-1;
while (kmin < 999999 && kmin !=-1 ) {
int kind=-1;
knew=k;
kmin=999999;
while(nodes[k]==nodes[knew] && knew < nodes.Len()-1) {
int inter = Intersect(Graph->GetNI(nodesIds[knew]),NNodes,NNodes_br);
int deg = nodes[knew];
//if (((((nodes.Len()-NNodes_br)*(nodes.Len()-NNodes_br)))-(nodes.Len()-NNodes_br))/2<(((br*br)-br)/2))
if ((deg-inter)<kmin && !GroupNodes.IsKey(nodesIds[knew]))
{
kmin = deg-inter;
kind = knew;
}
knew++;
}
if (kind!=-1) {
br++;
Z = Z + br - 1 - nodes[kind];
if (Z < (Zbest)) { // or <=
//if (olddeg>nodes[kind])
//olddeg = nodes[kind];
Zbest = Z;
//kbest = br;
int w = nodes[kind];
int id = nodesIds[kind];
GroupNodes.AddDat(id,w);
NNodes[NNodes_br] = id;
NNodes_br++;
}
else {
break;
}
}
}
k=knew-1;
}
else {
br++;
Z = Z + br - 1 - nodes[k];
if (Z < (Zbest)) { // or <=
//if (olddeg>nodes[k])
//olddeg = nodes[k];
Zbest = Z;
//kbest = br;
int w = nodes[k];
int id = nodesIds[k];
GroupNodes.AddDat(id,w);
NNodes[NNodes_br] = id;
NNodes_br++;
}
}
}
else {
br++;
Z = Z + br - 1 - nodes[k];
if (Z < Zbest) { // or <=
//if (olddeg>nodes[k])
//.........这里部分代码省略.........
示例15: LoadTxt
// load from allactors.zip that was prepared by Brad Malin in 2005
PImdbNet TImdbNet::LoadTxt(const TStr& ActorFNm) {
PImdbNet Net = TImdbNet::New();
TStrV ColV;
char line [2024];
int NLines=0, DupEdge=0, Year, Position, ActorNId, MovieNId;
TIntH ActorNIdH;
THash<TIntPr, TInt> MovieNIdH;
FILE *F = fopen(ActorFNm.CStr(), "rt"); fgets(line, 2024, F);
while (! feof(F)) {
memset(line, 0, 2024);
fgets(line, 2024, F);
if (strlen(line) == 0) break;
TStr(line).SplitOnAllCh('|', ColV, false); IAssert(ColV.Len() == 7);
const int NameStrId = Net->AddStr(ColV[0].GetTrunc().GetLc()+" "+ColV[1].GetTrunc().GetLc());
const int MovieStrId = Net->AddStr(ColV[2].GetTrunc().GetLc());
TStr YearStr = ColV[3].GetTrunc();
if (YearStr.Len() > 4) YearStr = YearStr.GetSubStr(0, 3);
Year = 1; YearStr.IsInt(Year);
const TMovieTy MovieTy = TImdbNet::GetMovieTy(ColV[4]);
Position = TInt::Mx; ColV[5].GetTrunc().IsInt(Position);
IAssert(ColV[6].GetTrunc()[0] == 'M' || ColV[6].GetTrunc()[0]=='F');
const bool IsMale = ColV[6].GetTrunc()[0] == 'M';
// create nodes
if (ActorNIdH.IsKey(NameStrId)) {
ActorNId = ActorNIdH.GetDat(NameStrId); }
else {
ActorNId = Net->AddNode(-1, TImdbNode(NameStrId, Year, Position, IsMale));
ActorNIdH.AddDat(NameStrId, ActorNId);
}
if (MovieNIdH.IsKey(TIntPr(MovieStrId, Year))) {
MovieNId = MovieNIdH.GetDat(TIntPr(MovieStrId, Year)); }
else {
MovieNId = Net->AddNode(-1, TImdbNode(NameStrId, Year, MovieTy));
MovieNIdH.AddDat(TIntPr(MovieStrId, Year), MovieNId);
}
if (! Net->IsEdge(ActorNId, MovieNId)) {
Net->AddEdge(ActorNId, MovieNId); }
else { DupEdge++; }
if (++NLines % 100000 == 0) printf("\r %dk ", NLines/1000);
}
fclose(F);
printf("duplicate edges: %d\n", DupEdge);
printf("nodes: %d\n", Net->GetNodes());
printf("edges: %d\n", Net->GetEdges());
printf("actors: %d\n", ActorNIdH.Len());
printf("movies: %d\n", MovieNIdH.Len());
// set the actor year to the year of his first movie
int NUpdates=0;
for (TNet::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
if (NI().IsActor()) {
int MinYear = NI().GetYear();
for (int e = 0; e < NI.GetOutDeg(); e++) {
const TImdbNode& NodeDat = Net->GetNDat(NI.GetOutNId(e));
if (NodeDat.IsMovie()) MinYear = TMath::Mn(MinYear, NodeDat.GetYear());
}
if (NI().Year != MinYear) NUpdates++;
NI().Year = MinYear;
}
}
printf("updated actor times: %d\n", NUpdates);
return Net;
}