File size: 20,727 Bytes
d5062c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
#ifndef BTREE_H
#define BTREE_H
#define LNEG -100000000
#include <iostream>
#include <fstream>
using namespace std;
namespace iret {
const int order = 5; //Half the order of the Btree that we build.
const int height_limit =12; //Limit on the height of the Btree.
const int ord2 = order*2; //The order of the Btree.
int stc_my(int &,int &,const char *,const char *); //Function used to compare
//two strings. The first two arguments hold information about how much the
//string can be ignored in the comparison.
class Page; //forward declaration
class Btree; //forward declaration
class Partial_match; //forward declaration
class Node {
friend int stc_my(int &,int &,const char *,const char *);
friend class Page;
friend class Btree;
friend class List;
friend class Count;
friend class FCount;
friend class DCount;
template<class Z> friend class BCount;
friend class Partial_match;
friend class Thes;
public:
Node(void); //Sets all points to NULL.
Node(const char * ); //Argument is the string for this node.
Node(const char * ,void *); //Arguments are first the string and then the
//data pointer.
~Node();
void debug(); //Prints out the node in simple format.
private:
char *str; //String pointer.
void *rel; //Data pointer.
Page *pdn; //Points down to the page below or to NULL.
};
class Page {
friend int stc_my(int &,int &,const char *,const char *);
friend class Btree;
friend class Partial_match;
friend class FCount;
friend class DCount;
public:
Page(); //Constructs a new empty page. Only happens at the root.
Page(Page * const pz,Page * const pn,const int n); //Constructs a page that
//holds the right half of a full page. The full page is pointed at by the
//pz. The new pages downward pointer is set to pn.
//n tells how much of the full page is to remain or where to begin removal.
~Page();
void clean(void); //Used to delete without touching search keys in the nodes
//which were created with addp functions and do not belong to the tree.
void insert(const int n,Node * const nd,const int j); //inserts in partially empty
//page. n is insertion point, j is number of nodes on page that are viable.
int search(int &a,int &b,const char *,int &p); //searches for string on
//the page. Returns 1 if found, 0 otherwise. If found p is the index, otherwise
//if p is 0 then the page downward pointer is to next page to search, but if
//p is positive then p-1 is number of node that has the downward pointer to
//next page to search.
int search(int &a,int &b,char *,int &p,Partial_match *btr); //Looks for longest
//partial match.
void debug(); //Prints out the page for debugging purposes.
private:
char ndnm; //Indicates the number of Nodes on the page.
Page *pdn; //Pointer that points to the page below and also lexically below.
//May be NULL.
Node *pnd[ord2]; //Pointers to the nodes on the page. Some may be NULL.
};
class Btree {
friend class Page;
public:
Btree(void);
Btree(ifstream &); //Reads in a Btree in form of list written out by
//list_write() from disc. String arguments mark the path in proj file.
Btree( const Btree & btree ) {copy = true; root = btree.root;} // Actually
// creates another reference to the same tree. Take great care to
// avoid simultaneously modifying both copies.
~Btree(void);
int search(const char *); //Searches for a string and sets the path to that
//string or its insertion point.
int insert(Node *);//Only to be called after a search has failed to find the
//string.
void node_first();//Finds the first node in the tree and sets the path to it.
int node_next(); //Given the path is already set to a node, this function
//finds the next node in lexicographic order.
char *show_str();//Used to show the string after a call to next is successful.
void *give_ptr();//Used to give the data pointer in the current node.
void set_ptr(void *); //Used to set the data pointer after a call to search
//has found string.
int add(Node *); //Only to be used to construct a tree from a lexical list
//as written out by list_write();
void next_empty(); //Only used to reset the pointer arrays when the root is
//split. Used in add().
long list_write(ofstream &); //Writes out a lexical list of the strings in
//the tree.
int iclean; //Default 0, but set to 1 if want to have destructor run without
//touching key strings (if addp used in making tree).
protected:
int depth; //Tells the depth in the tree that marks the current location.
Page *root; //Points at the root page of the tree.
Page *pg[height_limit]; //Descending list of pointers that mark the pages.
int cnd[height_limit]; //Mark the positions of the nodes just above the
//downard page pointer at each level. Thus 0 marks the page's downward
//pointer, but a nonzero value must have 1 subtracted and then it gives
//the node whose downward pointer is the correct downward pointer.
bool copy; //flags copies of a tree with true.
};
class List : public Btree {
public:
List();
List(const List & list) : Btree(list) {}
~List();
void add_key(const char *str); //Adds the string *str to the tree if not already in list
void add_key_count(const char *str); //Adds the string *str to the tree if
//not already in list and counts it.
void addp_key_count(char *str); //Adds the string *str to the tree if
//not already in list and counts it. Uses the actual string pointer instead
//of making a copy
long cnt_key; //Used to count the number of keys.
};
class Count : public List {
public:
Count();
Count(const Count & Ct) : List(Ct){}
~Count();
void add_count(const char *str,long n); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is incremented by n.
void add_countz(const char *str,long n); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
//Does not add count to the total variable, unlike add_count2.
void add_count2(const char *str,long n); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
void addp_count2(char *str,long n); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
void correct(const char *str,long n); //If str is in the tree the count is
//changed to n. Otherwise nothing is done.
//Functions for maximum calculation
void max_count(const char *str,long n); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is max of n and prior value.
void max_count2(const char *str,long n); //Adds the string *str with its count
//just as max_count, but also counts number of unique keys in count.
void maxp_count2(char *str,long n); //Adds the string *str with its count
//just as max_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
//Functions for minium calculation
void min_count(const char *str,long n); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is min of n and prior value.
void min_count2(const char *str,long n); //Adds the string *str with its count
//just as min_count, but also counts number of unique keys in count.
void minp_count2(char *str,long n); //Adds the string *str with its count
//just as min_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
long count(const char *str); //Returns the count if a key (in list) otherwise
//returns 0.
long count(void); //Returns the count of the current string. Assumes the
//pointers have already been set by a search or node_next call.
long total; //Holds the total of all counts added for all keys.
};
class FCount : public List {
public:
FCount();
FCount(const FCount & Ct) : List(Ct){}
~FCount();
void Copy(FCount &Dc); //Makes a copy of the tree Dc in the current tree.
void add_count(const char *str,float z); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is incremented by z.
void add_count2(const char *str,float z); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
void addp_count2(char *str,float z); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
float count(const char *str); //Returns the count if a key (in list) otherwise
//returns 0.
float count(void); //Returns the count of the current string. Assumes the
//pointers have already been set by a search or node_next call.
float total; //Holds the total of all counts added for all keys.
};
class DCount : public List {
public:
DCount();
DCount(const DCount & Ct) : List(Ct){}
~DCount();
void Copy(DCount &Dc); //Makes a copy of the tree Dc in the current tree.
void add_count(const char *str,double z); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is incremented by z.
void add_count2(const char *str,double z); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
void addp_count2(char *str,double z); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
double count(const char *str); //Returns the count if a key (in list) otherwise
//returns 0.
double count(void); //Returns the count of the current string. Assumes the
//pointers have already been set by a search or node_next call.
//Functions for maximum calculation
void max_count(const char *str,double z); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is max of z and prior value.
void max_count2(const char *str,double z); //Adds the string *str with its count
//just as max_count, but also counts number of unique keys in count.
void maxp_count2(char *str,double z); //Adds the string *str with its count
//just as max_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
//Functions for minium calculation
void min_count(const char *str,double z); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is min of z and prior value.
void min_count2(const char *str,double z); //Adds the string *str with its count
//just as min_count, but also counts number of unique keys in count.
void minp_count2(char *str,double z); //Adds the string *str with its count
//just as min_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
void debug(void); //Prints to stdout a list "i str[i]"
double total; //Holds the total of all counts added for all keys.
};
class Partial_match : public Count {
friend class Page;
public:
Partial_match();
Partial_match(const Partial_match & Par_mat) : Count(Par_mat){}
~Partial_match();
void long_match(char *,List &); //Finds the longest matches for all word
//starts in the string and adds them to the list.
void local_match(char *,List &); //Finds all matches that start at
//beginning of the string and adds them to the list.
void all_match(char *,List &); //Finds all matches within the string and
//adds them to the list.
void long_match(char *,Count &,long n); //Finds the longest matches for all word
//starts in the string and adds them to the list in Count.
void local_match(char *,Count &,long n); //Finds all matches that start at
//beginning of string and adds them to the list in Count.
void all_match(char *,Count &,long n); //Finds all matches within the string and
//adds them to the list in Count.
int search_long(char *); //Searches for longest partial match to an initial
//segment of a string that ends at a word boundary and
//sets the path to that string or its insertion point.
private:
int stc_my_long(int &,int &,char *,const char *,int); //Function used to compare
//two strings. The first two arguments hold information about how much the
//string can be ignored in the comparison. The last argument holds the index
//or number of the string's node on the page.
int step_one(int &,int &,char *); //Looks for partial or complete match and
//returns 1 if complete found. Partial is reflected in parameters.
//Special parameters used in partial matching.
int depth_o; //Depth of longest partial match thus far.
int index_o; //index of longest partial match thus far.
int cln_o; //String length of longest partial match thus far.
int len; //Length of query string.
int cln; //Current null position in string.
};
class Str_str : public Btree {
public:
Str_str();
Str_str(const Str_str & Stst) : Btree(Stst){}
~Str_str();
void add_pair(const char *one,const char *two); //Adds the string *one to the tree and stores
//the string *two at that node.
char *match(const char *one); //Returns pointer to the string stored under string *one.
};
class Num_num : public Btree {
public:
Num_num();
Num_num(const Num_num & Nmnm) : Btree(Nmnm){}
~Num_num();
void add_pair(long i, long j); //Adds the string for i to the tree and
//stores the number j at that node.
long match(long i); //Returns the number stored under the string for i.
};
template<class Z>
class BCount : public List {
public:
BCount();
BCount(const BCount<Z> & Ct) : List(Ct){}
~BCount();
void add_count(const char *str,Z n); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is incremented by n.
void add_count2(const char *str,Z n); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
void addp_count2(char *str,Z n); //Adds the string *str with its count
//just as add_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
void correct(const char *str,Z n); //If str is in the tree the count is
//changed to n. Otherwise nothing is done.
//Functions for maximum calculation
void max_count(const char *str,Z n); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is max of n and prior value.
void max_count2(const char *str,Z n); //Adds the string *str with its count
//just as max_count, but also counts number of unique keys in count.
void maxp_count2(char *str,Z n); //Adds the string *str with its count
//just as max_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
//Functions for minium calculation
void min_count(const char *str,Z n); //Adds the string *str with its count
//to the tree if not already in list. String is key and count is data.
//If string is already a key the count is min of n and prior value.
void min_count2(const char *str,Z n); //Adds the string *str with its count
//just as min_count, but also counts number of unique keys in count.
void minp_count2(char *str,Z n); //Adds the string *str with its count
//just as min_count, but also counts number of unique keys in count.
//Does not make copy of string, but uses the pointer str as key pointer.
Z count(const char *str); //Returns the count if a key (in list) otherwise
//returns 0.
Z count(void); //Returns the count of the current string. Assumes the
//pointers have already been set by a search or node_next call.
Z total; //Holds the total of all counts added for all keys.
};
template<class Z>
BCount<Z>::BCount() : List() {
total=0;
}
template<class Z>
BCount<Z>::~BCount(){
if(copy)return;
Z *pk;
this->node_first();
while(this->node_next()){
pk=(Z *)(this->give_ptr());
if(pk)delete pk;
}
}
template<class Z>
void BCount<Z>::add_count(const char *pch,Z n){
Z *ppt;
Node *np;
total+=n;
if(this->search(pch)==0){
ppt = new Z;
(*ppt) =n;
np=new Node(pch,(void*)ppt);
this->insert(np);
}
else {
(*(Z *) this->give_ptr())+=n;
}
}
template<class Z>
void BCount<Z>::add_count2(const char *pch,Z n){
Z *ppt;
Node *np;
total+=n;
if(this->search(pch)==0){
ppt = new Z;
(*ppt) =n;
np=new Node(pch,(void*)ppt);
this->insert(np);
cnt_key++;
}
else {
(*(Z *) this->give_ptr())+=n;
}
}
template<class Z>
void BCount<Z>::addp_count2(char *pch,Z n){
Z *ppt;
Node *np;
total+=n;
if(this->search(pch)==0){
ppt = new Z;
(*ppt) =n;
np=new Node;
np->str=pch;
np->rel=ppt;
this->insert(np);
cnt_key++;
}
else {
(*(Z *) this->give_ptr())+=n;
}
}
template<class Z>
void BCount<Z>::correct(const char *pch,Z n){
if(this->search(pch)){
(*(Z *) this->give_ptr())=n;
}
}
template<class Z>
Z BCount<Z>::count(const char *pch){
if(this->search(pch)==0){
return(0);
}
else {
return(*((Z *) this->give_ptr()));
}
}
template<class Z>
Z BCount<Z>::count(void){
return(*((Z *) this->give_ptr()));
}
template<class Z>
void BCount<Z>::max_count(const char *pch,Z n){
Z *ppt,i;
Node *np;
total+=n;
if(!search(pch)){
ppt = new Z;
(*ppt) =n;
np=new Node(pch,(void*)ppt);
this->insert(np);
}
else {
ppt=(Z *)give_ptr();
if(*ppt<n)*ppt=n;
}
}
template<class Z>
void BCount<Z>::max_count2(const char *pch,Z n){
Z *ppt,i;
Node *np;
total+=n;
if(!search(pch)){
ppt = new Z;
(*ppt) =n;
np=new Node(pch,(void*)ppt);
this->insert(np);
cnt_key++;
}
else {
ppt=(Z *)give_ptr();
if(*ppt<n)*ppt=n;
}
}
template<class Z>
void BCount<Z>::maxp_count2(char *pch,Z n){
Z *ppt,i;
Node *np;
total+=n;
if(!search(pch)){
ppt = new Z;
(*ppt) =n;
np=new Node;
np->str=pch;
np->rel=ppt;
this->insert(np);
cnt_key++;
}
else {
ppt=(Z *)give_ptr();
if(*ppt<n)*ppt=n;
}
}
template<class Z>
void BCount<Z>::min_count(const char *pch,Z n){
Z *ppt,i;
Node *np;
total+=n;
if(!search(pch)){
ppt = new Z;
(*ppt) =n;
np=new Node(pch,(void*)ppt);
this->insert(np);
}
else {
ppt=(Z *)give_ptr();
if(*ppt>n)*ppt=n;
}
}
template<class Z>
void BCount<Z>::min_count2(const char *pch,Z n){
Z *ppt,i;
Node *np;
total+=n;
if(!search(pch)){
ppt = new Z;
(*ppt) =n;
np=new Node(pch,(void*)ppt);
this->insert(np);
cnt_key++;
}
else {
ppt=(Z *)give_ptr();
if(*ppt>n)*ppt=n;
}
}
template<class Z>
void BCount<Z>::minp_count2(char *pch,Z n){
Z *ppt,i;
Node *np;
total+=n;
if(!search(pch)){
ppt = new Z;
(*ppt) =n;
np=new Node;
np->str=pch;
np->rel=ppt;
this->insert(np);
cnt_key++;
}
else {
ppt=(Z *)give_ptr();
if(*ppt>n)*ppt=n;
}
}
}
#endif
|