Skip to content

Commit

Permalink
further libagg namespace cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
albertjiang committed Feb 15, 2012
1 parent 97e3509 commit 35b15d1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 54 deletions.
1 change: 0 additions & 1 deletion src/libagg/agg.h
Expand Up @@ -4,7 +4,6 @@
#ifndef __AGG_H
#define __AGG_H

//using namespace std;


#include <iostream>
Expand Down
25 changes: 12 additions & 13 deletions src/libagg/proj_func.h
Expand Up @@ -8,8 +8,7 @@
#include <vector>
#include <cstdlib>

//using namespace std;
using std::multiset;

using std::ostream;
using std::endl;
using std::istream;
Expand Down Expand Up @@ -69,7 +68,7 @@ struct proj_func{
return Type!=v.Type || Default!=v.Default || weights !=v.weights;
}
virtual int operator()(int x,int y) =0;
virtual int operator()(multiset<int>& s) =0;
virtual int operator()(std::multiset<int>& s) =0;
virtual void print (std::ostream& out){
out<< Type;
out<<" "<<Default<<" ";
Expand All @@ -88,9 +87,9 @@ struct proj_func_SUM: public proj_func {
struct proj_func_SUM2: public proj_func{
proj_func_SUM2(istream& in, int S):proj_func(P_SUM2,in,S){ }
inline int operator()(int x, int y){return x+y;}
inline int operator()(multiset<int>& s){
inline int operator()(std::multiset<int>& s){
int res=Default;
for(multiset<int>::iterator it=s.begin();it!=s.end();it++){
for(std::multiset<int>::iterator it=s.begin();it!=s.end();it++){
res+=weights.at(*it);
}
return res;
Expand All @@ -116,9 +115,9 @@ struct proj_func_EXIST2: public proj_func{
}
}
inline int operator()(int x, int y){return (x+y>0);}
inline int operator()(multiset<int>& s){
inline int operator()(std::multiset<int>& s){
int res=Default;
for(multiset<int>::iterator it=s.begin();it!=s.end();it++){
for(std::multiset<int>::iterator it=s.begin();it!=s.end();it++){
res+=weights.at(*it);
}
return (res>0);
Expand All @@ -131,7 +130,7 @@ struct proj_func_HIGH:public proj_func{
if (y==Default)return x;
return ( (x>y)?x:y );
}
inline int operator()(multiset<int>& s){
inline int operator()(std::multiset<int>& s){
if (s.empty()) return Default;
return *(s.rbegin());
}
Expand All @@ -144,9 +143,9 @@ struct proj_func_HIGH2: public proj_func{
if (y==Default)return x;
return ( (x>y)?x:y );
}
inline int operator()(multiset<int>& s){
inline int operator()(std::multiset<int>& s){
if (s.empty()) return Default;
multiset<int>::iterator it=s.begin();
std::multiset<int>::iterator it=s.begin();
int res=weights.at(*it);
for(;it!=s.end();it++){
res=(res>weights.at(*it))?res:weights[*it];
Expand All @@ -161,7 +160,7 @@ struct proj_func_LOW: public proj_func{
if (y==Default )return x;
return ( (x<y)?x:y);
}
inline int operator()(multiset<int>& s){
inline int operator()(std::multiset<int>& s){
if (s.empty() ) return Default;
return *(s.begin());
}
Expand All @@ -174,9 +173,9 @@ struct proj_func_LOW2: public proj_func{
if (y==Default)return x;
return ( (x<y)?x:y );
}
inline int operator()(multiset<int>& s){
inline int operator()(std::multiset<int>& s){
if (s.empty()) return Default;
multiset<int>::iterator it=s.begin();
std::multiset<int>::iterator it=s.begin();
int res=weights.at(*it);
for(;it!=s.end();it++){
res=(res<weights.at(*it))?res:weights[*it];
Expand Down
58 changes: 28 additions & 30 deletions src/libagg/trie_map.h
Expand Up @@ -9,9 +9,7 @@
#include <math.h>
#include <ext/slist>
#include <iterator>
//using namespace std;
using std::vector;
using std::pair;

using std::ostream;
using std::endl;
using std::cout;
Expand All @@ -32,14 +30,14 @@ template <class V>
struct TrieNode
{
//members:
vector<TrieNode*> children;
typename slist<pair<vector<int>,V> >::iterator val;
std::vector<TrieNode*> children;
typename slist<std::pair<std::vector<int>,V> >::iterator val;


//methods:

//constructor
TrieNode(size_t branches, typename slist<pair<vector<int>,V> >::iterator v):children(branches,(TrieNode*)NULL),val(v) {}
TrieNode(size_t branches, typename slist<std::pair<std::vector<int>,V> >::iterator v):children(branches,(TrieNode*)NULL),val(v) {}


inline TrieNode*& operator[](size_t i){
Expand All @@ -53,8 +51,8 @@ class trie_map {

public:
//typedefs
typedef vector<int> key_type;
typedef pair<vector<int>, V> value_type;
typedef std::vector<int> key_type;
typedef std::pair<std::vector<int>, V> value_type;

typedef V* pointer;
typedef V& reference;
Expand Down Expand Up @@ -94,7 +92,7 @@ class trie_map {
inline const_iterator end() const {return data.end();}

//insert: same interface as in STL map
inline pair<iterator,bool> insert (const value_type& x);
inline std::pair<iterator,bool> insert (const value_type& x);

template <class InputIterator>
inline void insert(InputIterator f, InputIterator l){
Expand All @@ -105,7 +103,7 @@ class trie_map {

//insert or add
inline trie_map<V>& operator+=(const value_type& x){
pair<typename trie_map<V>::iterator ,bool> r=insert(x);
std::pair<typename trie_map<V>::iterator ,bool> r=insert(x);
if (!r.second) (*r.first).second += x.second;
return (*this);
}
Expand Down Expand Up @@ -170,10 +168,10 @@ class trie_map {

//polynomial multiplication of t1 and t2, store the result in self
void multiply (const trie_map<V>& t1,const trie_map<V>& t2,size_t keylen,
vector<proj_func*>& f)
std::vector<proj_func*>& f)
{
static size_t i;
static pair<vector<int>, V> v;
static std::pair<std::vector<int>, V> v;
const_iterator p1,p2;
assert(this!=&t1 && this != &t2);
v.first.resize(keylen);
Expand All @@ -195,9 +193,9 @@ class trie_map {
//Do simplification when V is a class of symbolic expressions and there is strict independence
//However, wouldn't it be sufficient to check if projectedStrat is a singleton?
void multiply_smart (const trie_map<V>& P_k_minus_1,const trie_map<V>& projectedStrat,size_t keylen,
vector<proj_func*>& f)
std::vector<proj_func*>& f)
{
static pair<vector<int>, V> v;
static std::pair<std::vector<int>, V> v;
v.first.resize(keylen);
reset();

Expand Down Expand Up @@ -248,11 +246,11 @@ class trie_map {
}

//multiply in-place. other should not be the same object as self.
void multiply (const trie_map<V>& other,size_t keylen, vector<proj_func*>& f);
void multiply (const trie_map<V>& other,size_t keylen, std::vector<proj_func*>& f);

//squaring
void square(trie_map<V>& dest, size_t keylen, vector<proj_func*>& f) const{
static pair<vector<int>, V> v;
void square(trie_map<V>& dest, size_t keylen, std::vector<proj_func*>& f) const{
static std::pair<std::vector<int>, V> v;
v.first.resize(keylen);
assert(this!=&dest);
dest.reset();
Expand All @@ -270,9 +268,9 @@ class trie_map {
}

//squaring in-place
void square(size_t keylen, vector<proj_func*>& f){
void square(size_t keylen, std::vector<proj_func*>& f){
static typename slist<typename trie_map<V>::value_type>::iterator p1,p2;
static pair<vector<int>, V> v;
static std::pair<std::vector<int>, V> v;
v.first.resize(keylen);
slist<typename trie_map<V>::value_type> data2;
data.swap(data2);
Expand All @@ -293,7 +291,7 @@ class trie_map {
//take power of self using repeated squaring. result stored in dest.
//this is actually slower than power by straight multiplication, if the # of configurations grow polynomially
//in the # of players.
void power_repsq (size_t p, trie_map<V>& dest, size_t keylen, vector<proj_func*>& f) const{
void power_repsq (size_t p, trie_map<V>& dest, size_t keylen, std::vector<proj_func*>& f) const{
assert(p>0 && this!=&dest );
if(p==1){
dest=*this;
Expand All @@ -315,7 +313,7 @@ class trie_map {
}
}

void power(size_t p, trie_map<V> &dest,trie_map<V> &scratch, size_t keylen, vector<proj_func*> &f){
void power(size_t p, trie_map<V> &dest,trie_map<V> &scratch, size_t keylen, std::vector<proj_func*> &f){
assert(p>0 && this!=&dest );
if (p==1) {
dest = *this;
Expand Down Expand Up @@ -357,7 +355,7 @@ class trie_map {
}

//first apply the action x, then inner prod
V inner_prod(const vector<int>& x, size_t keylen, vector<proj_func*>& f,
V inner_prod(const std::vector<int>& x, size_t keylen, std::vector<proj_func*>& f,
trie_map<V>& other, V init=(V)(0) ) const
{
V result(init);
Expand Down Expand Up @@ -394,7 +392,7 @@ class trie_map {


//polynomial division
inline trie_map<V>& operator/= (const vector<V>& denom);
inline trie_map<V>& operator/= (const std::vector<V>& denom);


private:
Expand All @@ -403,7 +401,7 @@ class trie_map {
size_type initBranches; //default branching factor
TrieNode<V> *root;

vector<TrieNode<V>*> leaves;
std::vector<TrieNode<V>*> leaves;

static const double THRESH = 1e-12;

Expand All @@ -423,7 +421,7 @@ class trie_map {

//private methods:
//div: helper for operator/=()
void div(const vector<V>& denom, TrieNode<V>* n, int current, int pivot);
void div(const std::vector<V>& denom, TrieNode<V>* n, int current, int pivot);



Expand Down Expand Up @@ -480,23 +478,23 @@ class trie_map {
//private helper functor classes:

struct div_helper :public std::unary_function<void, iterator>{
div_helper(const vector<V>& den, int piv,iterator en)
div_helper(const std::vector<V>& den, int piv,iterator en)
:pivot(piv), denom(den),endp(en){}
inline void operator()(iterator p){
if (p==endp) return;
p->first[pivot]--;
p->second /= denom[pivot];
}
int pivot;
const vector<V>& denom;
const std::vector<V>& denom;
iterator endp;
};

struct div_helper_mul: public std::unary_function<void,iterator>{
div_helper_mul(const vector<V>& den, int piv, TrieNode<V>* des,iterator en)
div_helper_mul(const std::vector<V>& den, int piv, TrieNode<V>* des,iterator en)
:pivot(piv),denom(den), dest(des),endp(en) {}

void add(const vector<int>& conf, V y){
void add(const std::vector<int>& conf, V y){
size_t i,keylen=conf.size();
double th(THRESH/(double)denom[pivot]);
//if (y<=th&&y>=-th) return;
Expand Down Expand Up @@ -543,7 +541,7 @@ class trie_map {


int pivot;
const vector<V>& denom;
const std::vector<V>& denom;
TrieNode<V>* dest;
iterator endp;
}; //end struct div_helper_mul
Expand Down
20 changes: 10 additions & 10 deletions src/libagg/trie_map.template
@@ -1,12 +1,12 @@

using namespace std;
//using namespace std;

template <class V>
inline pair<typename trie_map<V>::iterator, bool>
inline std::pair<typename trie_map<V>::iterator, bool>
trie_map<V>::insert(const trie_map<V>::value_type& x) {

static size_t ind;
static vector<int>::const_iterator p;//,s;
static std::vector<int>::const_iterator p;//,s;
//s=x.first.end();
TrieNode<V>* ptr = root;

Expand All @@ -19,11 +19,11 @@ trie_map<V>::insert(const trie_map<V>::value_type& x) {
ptr->children[ind] = new TrieNode<V>(initBranches,data.end());
ptr=ptr->children[ind];
}
if (ptr->val!=data.end()) return pair<typename trie_map<V>::iterator,bool>(ptr->val, false);
if (ptr->val!=data.end()) return std::pair<typename trie_map<V>::iterator,bool>(ptr->val, false);

//now insert the item
leaves.push_back(ptr);
return pair<typename trie_map<V>::iterator,bool>((ptr->val=data.insert_after(data.before_begin(),x)),true);
return std::pair<typename trie_map<V>::iterator,bool>((ptr->val=data.insert_after(data.before_begin(),x)),true);
}


Expand Down Expand Up @@ -88,7 +88,7 @@ inline void trie_map<V>::swap( trie_map<V>& other )


template <class V>
void trie_map<V>::multiply (const trie_map<V>& other,size_t keylen, vector<proj_func*>& f)
void trie_map<V>::multiply (const trie_map<V>& other,size_t keylen, std::vector<proj_func*>& f)
{
//#ifdef AGGDEBUG
// cout<< "multiplying "<<endl<<*this<<endl <<"(in order): "<<endl;
Expand All @@ -100,14 +100,14 @@ void trie_map<V>::multiply (const trie_map<V>& other,size_t keylen, vector<proj_
static size_t i;

if(&other == this){
cerr<<"Error: (in-place) multiply: other should not be the same object as self"<<endl;
std::cerr<<"Error: (in-place) multiply: other should not be the same object as self"<<endl;
exit(1);
}
slist<typename trie_map<V>::value_type> data2;
data.swap(data2);
reset();

static pair<vector<int>, V> v;
static std::pair<std::vector<int>, V> v;
v.first.resize(keylen);
TrieNode<V>* ptr;

Expand Down Expand Up @@ -151,7 +151,7 @@ void trie_map<V>::multiply (const trie_map<V>& other,size_t keylen, vector<proj_

template <class V>
inline trie_map<V>&
trie_map<V>::operator/= (const vector<V>& denom){
trie_map<V>::operator/= (const std::vector<V>& denom){
//first, find the pivot: the first nonzero element of denom
V th(sqrt(THRESH));
int piv=-1;
Expand All @@ -175,7 +175,7 @@ trie_map<V>::operator/= (const vector<V>& denom){
}

template <class V>
void trie_map<V>:: div(const vector<V>& denom,TrieNode<V>* n, int current, int pivot){
void trie_map<V>:: div(const std::vector<V>& denom,TrieNode<V>* n, int current, int pivot){
int i;
if ( pivot != current) {
int s=n->children.size();
Expand Down

0 comments on commit 35b15d1

Please sign in to comment.