C++ Dictionaries and tables


 
Thread Tools Search this Thread
Top Forums Programming C++ Dictionaries and tables
# 1  
Old 11-02-2014
C++ Dictionaries and tables

I was looking at this code from a programming book:

Code:
#include <time.h>
#include <iostream>
#include <string>
#include <deque>
#include <map>
#include <vector>
#include <cstdlib>

using namespace std;

const int  NPREF = 2;
const char NONWORD[] = "\n";    // cannot appear as real line: we remove newlines
const int  MAXGEN = 10000; // maximum words generated

typedef deque<string> Prefix;

map<Prefix, vector<string> > statetab; // prefix -> suffixes

void            build(Prefix&, istream&);
void            generate(int nwords);
void            add(Prefix&, const string&);

// markov main: markov-chain random text generation
int main(void)
{
        int     nwords = MAXGEN;
        Prefix prefix;  // current input prefix

        srand(time(NULL));
        for (int i = 0; i < NPREF; i++)
                add(prefix, NONWORD);
        build(prefix, cin);
        add(prefix, NONWORD);
        generate(nwords);
        return 0;
}

// build: read input words, build state table
void build(Prefix& prefix, istream& in)
{
        string buf;

        while (in >> buf)
                add(prefix, buf);
}

// add: add word to suffix deque, update prefix
void add(Prefix& prefix, const string& s)
{
        if (prefix.size() == NPREF) {
                statetab[prefix].push_back(s);
                prefix.pop_front();
        }
        prefix.push_back(s);
}

// generate: produce output, one word per line
void generate(int nwords)
{
        Prefix prefix;
        int i;

        for (i = 0; i < NPREF; i++)
                add(prefix, NONWORD);
        for (i = 0; i < nwords; i++) {
                vector<string>& suf = statetab[prefix];
                const string& w = suf[rand() % suf.size()];
                if (w == NONWORD)
                        break;
                cout << w << "\n";
                prefix.pop_front();     // advance
                prefix.push_back(w);
        }
}

I was wondering how the prefixes were stored and why not use a vector instead?
# 2  
Old 11-05-2014
Maybe this? One can greatly compress constant string storage if you use the end of longer constant strings (null terminated) for the shorter ones. Increased cache hits, too. You have to do any NLS translation first. For instance "XYZ" + 1 = "YZ". If you define strings using pointer and length, you can use any part of any string, but the cost of all those control bits ruins the economy. It's a bit like compression through context. To compile sort all strings by reversed characters in reverse, so "XYZ" and "YZ" lines become "ZYX and "ZY" lines, flop them back and find line 2 in line 1.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two tables

I have defined a set of global variables at the beginning of my script as below: #ideal values export ITEM1=SUCCESS export ITEM2=FAILURE export ITEM3=UNAVAILABLE export ITEM5=FAILURE export ITEM6=SUCCESS now I have a shell script function which returns a value in below format. ITEM1... (1 Reply)
Discussion started by: ctrld
1 Replies

2. Shell Programming and Scripting

Selecting tables

Hi, Can anyone help me that, How to see the table fields in Oracle database through shell script(ksh). I have tried with the following: sqlplus -s $user/$passwd@$sid << SQL >> session.log select * from Testtab.sql I'm not able to see anything.. Thanks (4 Replies)
Discussion started by: zxcjggu708
4 Replies

3. UNIX for Dummies Questions & Answers

Mysqldump certain tables

Hi, I have to upload part of my database periodically when i make changes to product data etc. However I only want to upload certain tables. We suffer from bandwidth chock here, so i want to write a couple of separate scripts that upload parts of the database that changed. The database is large... (5 Replies)
Discussion started by: timgolding
5 Replies

4. IP Networking

IP tables - ip forward to another ip

Hi all, Now my need is: This should forward each client to 1.11 and 1.12 as per each request. I mean : First request should go to : http://192.168.1.10:8080/MySite Second request should go to : http://192.168.1.11:8081/MySite Third request should go to ... (1 Reply)
Discussion started by: linuxadmin
1 Replies

5. Shell Programming and Scripting

Tables and borders

when i do this: cat HITS i get the following displayed: sport.hits:87.114.172.31 Thu Sep 28 22:45:12 GMT 2006 how do i put this information into a bordered table? so it will output like this: ...........File /... (9 Replies)
Discussion started by: amatuer_lee_3
9 Replies

6. Shell Programming and Scripting

comparing two tables

I am comparing two table structure in different databases,Put into 2 txt files , when comparing if column sequnce and data type is not matching ,it has to display that info else Table structure is ok. wrote shell script ,its not working .I am getting "Table structure is not ok" even if both... (1 Reply)
Discussion started by: akil
1 Replies

7. Shell Programming and Scripting

tables in scripts

Hi , I have two tables with same length t1 and t2, I want to cretae a new third table where i put the difference between the elements of t2 and t1, t3= t1 - t2 t3= t1 - t2 I am new to scripts, any help please? thanks (7 Replies)
Discussion started by: Celine19
7 Replies

8. Shell Programming and Scripting

Converting tables of row data into columns of tables

I am trying to transpose tables listed in the format into format. Any help would be greatly appreciated. Input: test_data_1 1 2 90% 4 3 91% 5 4 90% 6 5 90% 9 6 90% test_data_2 3 5 92% 5 4 92% 7 3 93% 9 2 92% 1 1 92% ... Output:... (7 Replies)
Discussion started by: justthisguy
7 Replies
Login or Register to Ask a Question