Beispiel #1
0
void ReadObjects (const char* name)
{
    CL_ObjectSequence obj_seq;
    CL_BinaryFile file (name);
    long i;
    
    // Read the sequence from the file
    file >> obj_seq; // Not necessarily equivalent to obj_seq.ReadFrom(file)
    cout << "Contents of restored sequence:\n";
    long n = obj_seq.Size();
    for (i = 0; i < n; i++) {
        TestClass* p = (TestClass*) obj_seq [i];
        cout << p->Data().AsPtr() << endl;
    }
    obj_seq.DestroyContents (); // Without this, there's a memory leak!

    // Read back the set
    CL_StringSet aSet;
    file >> aSet;
    n = aSet.Size();
    cout << "Restored set has " << n << " elements:\n";
    CL_StringSetIterator setIter (aSet);
    for (setIter.Reset (); setIter.More(); ) {
        cout << setIter.Next().AsPtr() << endl;
    }


    // And finally, read back the maps
    CL_IntPtrMap map1;
    CL_StringStringMap map2; // Note that there is no builder needed for
                             // non-pointer-based maps
    file >> map1 >> map2;

    cout << "Contents of restored map1:\n";
    CL_IntPtrMapIterator itr1 (map1);
    CL_IntPtrAssoc assoc1;
    for (itr1.Reset(); itr1.More(); ) {
        assoc1 = itr1.Next ();
        cout << assoc1.key << " --> " << 
                ((TestClass*) assoc1.value)->Data().AsPtr() << endl;
    }
    map1.DestroyContents ();



    cout << "Contents of restored map2:\n";
//     CL_StringStringMapIterator itr2 (map2);
//     CL_StringStringAssoc assoc2;
//     for (itr2.Reset(); itr2.More(); ) {
//         assoc2 = itr2.Next ();
//         printf ("'%s' --> '%s'\n", (const char*) assoc2.key,
//                 (const char *) assoc2.value);
//     }
    cout << map2 << endl;

    {
        CL_ObjectSequence sq;
        file >> sq;
        register long n = sq.Size();
        cout << "sq Size: " << n << endl;
        register long i;
        for (i = 0; i < n; i++) {
            CL_String* s = (CL_String*) sq[i];
            cout <<  (s ? s->AsPtr() : "(NULL)") << endl;
        }
        delete sq[0];
        delete sq[1];
    }
}
Beispiel #2
0
main ()
{
    // CL_StringSequence tst (18000); // Testing big sequences
    CL_StringSequence a;
    a.Add ("world!");
    a.Add ("Hello,");
    a.Add ("this is");
    a.Add ("a great day.");
    PrintOut (a);

    a.Insert ("world! 2", 1);
    PrintOut (a);
    a.Insert ("Hello, 2", -1);
    PrintOut (a);
    a.Insert ("this is 2", 2);
    PrintOut (a);
    a.Insert ("a great day. 2", 3);
    PrintOut (a);


    a.Sort();
    PrintOut (a);

#ifndef __GNUC__  // GCC Still has bugs!
    CL_IntegerSet st1(2,4);
    st1.Add (4);
    st1.Add (7);
    st1.Add (8);
    st1.Add (9);
    CL_StringSequence sq = a - st1;
    PrintOut (sq);
#endif
    
    a.ShiftRightAt (2);
    PrintOut (a);

    a[2] = "Some string here";
    PrintOut (a);
    
    a.ShiftLeftAt (2, 2);
    PrintOut (a);

    CL_StringSequence* b = new CL_StringSequence(10);
    *b = a;
    PrintOut (*b);

    *b += a;
    PrintOut (*b);
    
    b->Sort();
    PrintOut (*b);
    
    CL_BinaryFile f ("seqtest.dat", TRUE);
    f << *b; // Testing persistent sequences
    b->MakeEmpty ();
    

    // CL_Builder<CL_String> aBuilder;
    // CL_ObjectSequence a2(0, &aBuilder);
    CL_ObjectSequence a2;
    CL_String* pStrg;
    pStrg = new CL_String ("1"); a2.Add (pStrg);
    pStrg = new CL_String ("2"); a2.Add (pStrg);
    pStrg = new CL_String ("3"); a2.Add (pStrg);
    pStrg = new CL_String ("10"); a2.Add (pStrg);
    pStrg = new CL_String ("20"); a2.Add (pStrg);
    pStrg = new CL_String ("30"); a2.Add (pStrg);
    pStrg = new CL_String ("11"); a2.Add (pStrg);
    pStrg = new CL_String ("22"); a2.Add (pStrg);
    pStrg = new CL_String ("31"); a2.Add (pStrg);
    pStrg = new CL_String ("10"); a2.Add (pStrg);
    printf ("a2's size: %ld\n", a2.Size());

    f << a2;             // Testing persistent sequences
    f.SeekToBegin();
    f >> *b;
    // b->ReadFrom (f);
    
    printf ("Restored b is: \n"); PrintOut (*b);
    delete b;

    long i;
    for (i = 0; i < a2.Size(); i++) {
        printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
    }
    pStrg = (CL_String*) a2.ExtractLeftmost();
    printf ("Leftmost: '%s'\n", pStrg->AsPtr());
    for (i = 0; i < a2.Size(); i++) {
        printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
    }
    delete pStrg;

    pStrg = (CL_String*) a2[4];
    a2.Remove (4);
    delete pStrg;
    for (i = 0; i < a2.Size(); i++) {
        printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
    }
    
    a2.DestroyContents ();
    f >> a2;
    printf ("Restored a2 is:\n");
    for (i = 0; i < a2.Size(); i++) {
        printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
    }
    printf ("------------------------------------\n");
    a2.DestroyContents ();
    
    
    

    CL_IntegerSequence iseq;
    for (i = 0; i < 100; i++) {
        iseq.Add (i);
        iseq.Add (5*i);
    }
    for (i = 5; i < 80; i++)
        iseq.Remove (2);
    iseq.Add (253);
    printf ("iseq size %ld\n", iseq.Size());
    long j;
    for (j = 0; j < iseq.Size(); j++) {
        printf ("iseq[%ld] = %ld\n", j, iseq[j]);
    }
    iseq.Sort();
    printf ("==============================\niseq size %ld\n", iseq.Size());
    for (j = 0; j < iseq.Size(); j++) {
        printf ("iseq[%ld] = %ld\n", j, iseq[j]);
    }
    iseq.Sort();

    return 0;
}