Example #1
0
TEST(TStr, Search) {
	TStr Str = "abcdaaba";
	int Len = Str.Len();
	EXPECT_EQ(Str.CountCh('a'), 4);
	EXPECT_EQ(Str.CountCh('b'), 2);
	EXPECT_EQ(Str.CountCh('e'), 0);

	EXPECT_TRUE(Str.IsChIn('a'));
	EXPECT_TRUE(Str.IsChIn('b'));
	EXPECT_FALSE(Str.IsChIn('e'));	

	EXPECT_TRUE(Str.IsStrIn(Str));
	EXPECT_TRUE(Str.IsStrIn(""));
	EXPECT_TRUE(Str.IsStrIn("bcd"));
	EXPECT_TRUE(Str.IsStrIn("ab"));
	EXPECT_FALSE(Str.IsStrIn("eba"));


	EXPECT_EQ(Str.CountCh('a', 1), 3);
	EXPECT_ANY_THROW(Str.CountCh('a', 10));
	EXPECT_EQ(Str.CountCh('b', 2), 1);
	EXPECT_EQ(Str.CountCh('e', 1), 0);

	EXPECT_EQ(Str.SearchCh('a'), 0);
	EXPECT_EQ(Str.SearchCh('b'), 1);
	EXPECT_EQ(Str.SearchCh('e'), -1);

	EXPECT_EQ(Str.SearchCh('a', 1), 4);
	EXPECT_EQ(Str.SearchCh('b', 2), 6);
	EXPECT_EQ(Str.SearchCh('e', 1), -1);

	EXPECT_EQ(Str.SearchChBack('a'), Len - 1);
	EXPECT_EQ(Str.SearchChBack('b'), Len - 2);
	EXPECT_EQ(Str.SearchChBack('e'), -1);

	EXPECT_EQ(Str.SearchChBack('a', Len - 2), Len - 3);
	EXPECT_EQ(Str.SearchChBack('b', Len - 3), 1);;
	EXPECT_EQ(Str.SearchChBack('e', 3), -1);	

	EXPECT_EQ(Str.SearchStr("a"), 0);
	EXPECT_EQ(Str.SearchStr("b"), 1);
	EXPECT_EQ(Str.SearchStr("e"), -1);
	EXPECT_EQ(Str.SearchStr(""), 0);

	EXPECT_EQ(Str.SearchStr("a", 1), 4);
	EXPECT_EQ(Str.SearchStr("b", 2), 6);
	EXPECT_EQ(Str.SearchStr("e", 1), -1);
}
Example #2
0
File: util.cpp Project: pikma/Snap
// <last_name>_<first name innitial>
TStr TStrUtil::GetStdName(TStr AuthorName) {
    TStr StdName;
    AuthorName.ToLc();
    AuthorName.ChangeChAll('\n', ' ');
    AuthorName.ChangeChAll('.', ' ');
    // if there is a number in the name, remove it and everything after it
    int i, pos = 0;
    while (pos<AuthorName.Len() && (AuthorName[pos]!='#' && !TCh::IsNum(AuthorName[pos]))) {
        pos++;
    }
    if (pos < AuthorName.Len()) {
        AuthorName = AuthorName.GetSubStr(0, pos-1).ToTrunc();
    }
    if (AuthorName.Empty()) {
        return TStr::GetNullStr();
    }

    // replace everything after '('
    int b = AuthorName.SearchCh('(');
    if (b != -1) {
        AuthorName = AuthorName.GetSubStr(0, b-1).ToTrunc();
    }
    // skip if contains ')'
    if (AuthorName .SearchCh(')')!=-1) {
        return TStr::GetNullStr();
    }
    // skip if it is not a name
    if (AuthorName .SearchStr("figures")!=-1 || AuthorName .SearchStr("macros")!=-1
            || AuthorName .SearchStr("univ")!=-1 || AuthorName .SearchStr("institute")!=-1) {
        return TStr::GetNullStr();
    }
    // remove all non-letters (latex tags, ...)
    TChA NewName;
    for (i = 0; i < AuthorName.Len(); i++) {
        const char Ch = AuthorName[i];
        if (TCh::IsAlpha(Ch) || TCh::IsWs(Ch) || Ch=='-') {
            NewName += Ch;
        }
    }
    StdName = NewName;
    StdName.ToTrunc();
    TStrV AuthNmV;
    StdName.SplitOnWs(AuthNmV);
    // too short -- not a name
    if (! AuthNmV.Empty() && AuthNmV.Last() == "jr") AuthNmV.DelLast();
    if (AuthNmV.Len() < 2) return TStr::GetNullStr();

    const TStr LastNm = AuthNmV.Last();
    if (! TCh::IsAlpha(LastNm[0]) || LastNm.Len() == 1) return TStr::GetNullStr();

    IAssert(isalpha(AuthNmV[0][0]));
    return TStr::Fmt("%s_%c", LastNm.CStr(), AuthNmV[0][0]);
}
Example #3
0
TStr TFile::GetUniqueFNm(const TStr& FNm){
  // <name>.#.txt --> <name>.<num>.txt
  int Cnt=1; int ch;
  TStr NewFNm; TStr TmpFNm=FNm;
  if (FNm.SearchCh('#') == -1) {
    for (ch = FNm.Len()-1; ch >= 0; ch--) if (FNm[ch] == '.') break;
    if (ch != -1) TmpFNm.InsStr(ch, ".#");
    else TmpFNm += ".#";
  }
  forever{
    NewFNm=TmpFNm;
	NewFNm.ChangeStr("#", TStr::Fmt("%03d", Cnt)); Cnt++;
    if (!TFile::Exists(NewFNm)){break;}
  }
  return NewFNm;
}
Example #4
0
int main(int argc, char* argv[]) {
  Env = TEnv(argc, argv, TNotify::StdNotify);
  Env.PrepArgs(TStr::Fmt("GraphInfo. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
  TExeTm ExeTm;
  Try
  const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "graph.txt", "Input graph (one edge per line, tab/space separated)");
  const TBool IsDir = Env.GetIfArgPrefixBool("-d:", true, "Directed graph");
  const TStr OutFNm = Env.GetIfArgPrefixStr("-o:", "graph", "Output file prefix");
  const TStr Desc = Env.GetIfArgPrefixStr("-t:", "", "Title (description)");
  const TStr Plot = Env.GetIfArgPrefixStr("-p:", "cdhwsCvV", "What statistics to plot string:"
    "\n\tc: cummulative degree distribution"
    "\n\td: degree distribution"
    "\n\th: hop plot (diameter)"
    "\n\tw: distribution of weakly connected components"
    "\n\ts: distribution of strongly connected components"
    "\n\tC: clustering coefficient"
    "\n\tv: singular values"
    "\n\tV: left and right singular vector\n\t");
  bool PlotDD, PlotCDD, PlotHop, PlotWcc, PlotScc, PlotSVal, PlotSVec, PlotClustCf;
  PlotDD = Plot.SearchCh('d') != -1;
  PlotCDD = Plot.SearchCh('c') != -1;
  PlotHop = Plot.SearchCh('h') != -1;
  PlotWcc = Plot.SearchCh('w') != -1;
  PlotScc = Plot.SearchCh('s') != -1;
  PlotClustCf = Plot.SearchCh('C') != -1;
  PlotSVal = Plot.SearchCh('v') != -1;
  PlotSVec = Plot.SearchCh('V') != -1;
  if (Env.IsEndOfRun()) { return 0; }
  //PNGraph G = TGGen<PNGraph>::GenRMat(1000, 3000, 0.40, 0.25, 0.2);
  //G->SaveEdgeList("graph.txt", "RMat graph (a:0.40, b:0.25, c:0.20)");
  printf("Loading...");
  PNGraph Graph = TSnap::LoadEdgeList<PNGraph>(InFNm);
  if (! IsDir) { TSnap::MakeUnDir(Graph); }
  printf("nodes:%d  edges:%d\nCreating plots...\n", Graph->GetNodes(), Graph->GetEdges());
  const int Vals = Graph->GetNodes()/2 > 200 ? 200 : Graph->GetNodes()/2;
  if (PlotDD) {
    TSnap::PlotOutDegDistr(Graph, OutFNm, Desc, false, false);
    TSnap::PlotInDegDistr(Graph, OutFNm, Desc, false, false);
  }
  if (PlotCDD) {
    TSnap::PlotOutDegDistr(Graph, OutFNm, Desc, true, false);
    TSnap::PlotInDegDistr(Graph, OutFNm, Desc, true, false);
  }
  if (PlotHop) {
    TSnap::PlotHops(Graph, OutFNm, Desc, false, 32);
  }
  if (PlotWcc) {
    TSnap::PlotWccDistr(Graph, OutFNm, Desc);
  }
  if (PlotScc) {
    TSnap::PlotSccDistr(Graph, OutFNm, Desc);
  }
  if (PlotClustCf) {
    TSnap::PlotClustCf(Graph, OutFNm, Desc);
  }
  if (PlotSVal) {
    TSnap::PlotSngValRank(Graph, Vals, OutFNm, Desc);
  }
  if(PlotSVec) {
    TSnap::PlotSngVec(Graph, OutFNm, Desc);
  }
  Catch
  printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
  return 0;
}