Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Treat warnings as error. Fixed all warnings.
Signed-off-by: saq10002 <saad0105050@gmail.com>
  • Loading branch information
saq10002 committed Oct 25, 2014
1 parent dde3b27 commit 1fcb3f6
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 47 deletions.
1 change: 1 addition & 0 deletions YASI_12/YASI_12.vcxproj
Expand Up @@ -46,6 +46,7 @@
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>"$(SolutionDir)\gtest-1.7.0\include"</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down
10 changes: 10 additions & 0 deletions YASI_12/common.h
Expand Up @@ -52,4 +52,14 @@ namespace yasi{

#define IS_POW_OF_TWO(x) ( ((x) >= 1) && ( ((x) & ((x)-1)) == 0) )

// various types
typedef unsigned int uint;
typedef unsigned char byte;

// Windows specific
#if _WIN32
// disable "inherits via dominance" warning for virtual inheritance diamond cases
#pragma warning( disable : 4250)
#endif

} // namespace yasi
14 changes: 7 additions & 7 deletions YASI_12/ds.HopScotchHashTable.h
Expand Up @@ -146,7 +146,7 @@ protected:

}

inline void setBitmap(const int bucket, const unsigned int position, const unsigned int bit) const{
inline void setBitmap(const unsigned int bucket, const unsigned int position, const unsigned int bit) const{
if (bucket >= 0 && bucket < _size &&
position >= 0 && position < H &&
bit <= 1){
Expand Down Expand Up @@ -236,7 +236,7 @@ protected:

hashCurIsAtLeftOfEmptySlot = diffHashCurToEmptySlot > 0;
hashCurIsAtRightOfFirstBucket = hashCur - firstBucket > 0;
emptySlotInNeighborhoodOfHashCur = diffHashCurToEmptySlot < H;
emptySlotInNeighborhoodOfHashCur = diffHashCurToEmptySlot < (int) H;
}
else{
// firstBucket >= emptySlot
Expand All @@ -248,7 +248,7 @@ protected:
if (hashCur > firstBucket){
hashCurIsAtRightOfFirstBucket = true;
hashCurIsAtLeftOfEmptySlot = true;
emptySlotInNeighborhoodOfHashCur = diffHashCurToEmptySlot < H;
emptySlotInNeighborhoodOfHashCur = diffHashCurToEmptySlot < (int) H;
}
else{
if (hashCur == firstBucket) return NULL;
Expand All @@ -257,7 +257,7 @@ protected:
if (hashCur < emptySlot){
hashCurIsAtRightOfFirstBucket = true;
hashCurIsAtLeftOfEmptySlot = true;
emptySlotInNeighborhoodOfHashCur = diffHashCurToEmptySlot < H;
emptySlotInNeighborhoodOfHashCur = diffHashCurToEmptySlot < (int) H;
}
else{
// hashCur >= emptySlot
Expand All @@ -278,7 +278,7 @@ protected:
// returns NULL if the pull is unsuccessful
BucketType* pullFromLeft(const int firstBucket, const int emptySlot){
//when to stop
int remainingSlots = circularDiff(firstBucket, emptySlot);
uint remainingSlots = circularDiff(firstBucket, emptySlot);
if (remainingSlots < H){
// the empty slot is within the neighborhood of firstBucket
return &table[emptySlot];
Expand Down Expand Up @@ -344,7 +344,7 @@ protected:
}

// found an empty slot
int positionInBucket = circularDiff(firstBucket, cur);
uint positionInBucket = circularDiff(firstBucket, cur);
if ( positionInBucket < H){
// empty slot within the neighborhood of firstBucket
table[cur].key = k;
Expand Down Expand Up @@ -395,7 +395,7 @@ protected:
#endif
// keep probing to the right
// the item, if exists, must be in the neighborhood H
for (int i = 0; i < H; i = nextPosInBucket(bitmap, i)){
for (uint i = 0; i < H; i = nextPosInBucket(bitmap, i)){
if (table[firstBucket + i].key == k)
return &table[firstBucket + i];
#if _DEBUG
Expand Down
2 changes: 1 addition & 1 deletion YASI_12/ds.HopScotchHashTable.test.h
Expand Up @@ -547,7 +547,7 @@ namespace yasi{

///////// test resizing when a bucket gets more than H items
IntHashTable32 h2(4, 4); // keep the hash func mod 32 so that we can grow and rehash
for (int i = 0; i < h2.H; i++){
for (uint i = 0; i < h2.H; i++){
h2.insert(16 * i + 2, i); // all hashed to 2
}
ASSERT_EQ(16, h2.size());
Expand Down
4 changes: 2 additions & 2 deletions YASI_12/ds.LinearProbingHashTable.h
Expand Up @@ -65,7 +65,7 @@ namespace ds{
else
return _size - low + high;
}
inline unsigned bool circularBetweenInclusive(const int bucket, const int left, const int right) const{
inline bool circularBetweenInclusive(const int bucket, const int left, const int right) const{
if (left == right && bucket != left) return false;
if (left < right){
return left <= bucket && bucket <= right;
Expand Down Expand Up @@ -208,7 +208,7 @@ namespace ds{
// non-zero key
Pred keyEquals;
unsigned int curFirstBucket = index(k);
int cur = curFirstBucket;
unsigned int cur = curFirstBucket;
const int searchStart = curFirstBucket; // remember our first posti
do{
//Pair* pEntry = table[cur];
Expand Down
2 changes: 1 addition & 1 deletion YASI_12/ds.LinearProbingHashTable.test.h
Expand Up @@ -157,7 +157,7 @@ namespace yasi{

// check that all items exist
// items are rehashed mod 32
for (int i = 0; i < h1._size; i++){
for (uint i = 0; i < h1._size; i++){
if (!h1.isNull(i)){
int key = h1.key(i);
int value = h1.value(i);
Expand Down
2 changes: 1 addition & 1 deletion YASI_12/ds.SeparateChainingHashTable.h
Expand Up @@ -93,7 +93,7 @@ namespace ds{

virtual ~SeparateChainingHashTable(){
if (table){
for (int i = 0; i < _size; i++){
for (uint i = 0; i < _size; i++){
// each entry is either NULL or a List*
if (table[i]){
table[i]->clear();
Expand Down
8 changes: 4 additions & 4 deletions YASI_12/ds.SeparateChainingHashTable.test.h
Expand Up @@ -22,9 +22,9 @@ namespace yasi{
srand((unsigned int)time(NULL));
for (int i = 0; i < 1000; i++){
int n = rand();
int index = h.index(n);
uint index = h.index(n);
ASSERT_LT(index, h.size()) << "index(" << n << ") out of range";
ASSERT_GE(index, 0) << "index(" << n << ") out of range";
ASSERT_GE(index, 0u) << "index(" << n << ") out of range";
}
}

Expand All @@ -34,7 +34,7 @@ namespace yasi{
int size = h._size;
ASSERT_EQ(IntHashTable::INIT_LOGSIZE, logSize) << "initial logSize not " << IntHashTable::INIT_LOGSIZE;
ASSERT_EQ(1 << logSize, h.size()) << "table size not 2^" << logSize;
for (int i = 0; i < h.size(); i++){
for (uint i = 0; i < h.size(); i++){
ASSERT_EQ(0, (int)h.table[i]) << "table[i] not NULL for i=" << i;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ namespace yasi{
{
SCOPED_TRACE("remove");
// now remove entries but do not trigger shrink()
int i = 0;
uint i = 0;
BucketType pCriticalBucket = NULL;
DoublyLinkedList<Pair> survivedKeys;
int initPop = h.population();
Expand Down
22 changes: 11 additions & 11 deletions YASI_12/ds.arraybinarytree.h
Expand Up @@ -90,7 +90,7 @@ namespace yasi{
virtual void fromArray(const E*, const int) = 0;
virtual int getRootIndex() const = 0;
virtual Node* nodeAt(int) const = 0;
virtual Node* insertAt(const E& e, int) = 0;
virtual Node* insertAt(const E& e, uint) = 0;
};


Expand All @@ -109,18 +109,18 @@ namespace yasi{
const int ROOT_INDEX = 1; // for quick parent-child indexing, 1 instead of 0

// the underlying container
const int INIT_CAPACITY = 2; // initial size of the array
const uint INIT_CAPACITY = 2; // initial size of the array
const float LOAD_FACTOR = 2; // how much the array size increases at each resize

// do not initialize _capacity and _size here; let constructors do that
int _capacity; // current length of the array, not necessarily the same as the capacity of a vector (due to automatic resizing)
int _size; // the actual size of the tree, always <= _capacity.
uint _capacity; // current length of the array, not necessarily the same as the capacity of a vector (due to automatic resizing)
uint _size; // the actual size of the tree, always <= _capacity.

// the vector
vector< node_t* > _arr; // the array of pointers to nodes

// returns true if 0 <= index < capacity
bool isArrIndex(int k) const{
bool isArrIndex(uint k) const{
return k >= 0 && k < _capacity;
}
// returns true if the node-ptr at index is not null
Expand Down Expand Up @@ -192,7 +192,7 @@ namespace yasi{
// increases the size of the array
// initializes new slots with NULL pointer
void resize(int newSize, node_t* initValue = 0){
int i = _capacity; // old capacity
uint i = _capacity; // old capacity
_arr.resize(newSize, initValue);
_capacity = _arr.capacity() - 1; // new capacity; one less than vector-capacity to possibly stop auto-resize
while (i < _capacity){
Expand Down Expand Up @@ -304,8 +304,8 @@ namespace yasi{
*ppDestArr = new E[_size];
E* pDestArr = *ppDestArr;

int countElems = 0; // how many elements touched
for (int i = 1; i < _capacity && countElems < _size; i++){ // discard the index 0
uint countElems = 0; // how many elements touched
for (uint i = 1; i < _capacity && countElems < _size; i++){ // discard the index 0
node_t* pNode = nodeAt(i);
if (pNode != NULL){
pDestArr[countElems] = pNode->element;
Expand All @@ -325,7 +325,7 @@ namespace yasi{
}
void clear(){
// delete each pointer contained in the vector
for (int i = 0; i < _capacity && i < _arr.size(); i++){
for (uint i = 0; i < _capacity && i < _arr.size(); i++){
DELETE_SAFE(_arr[i]);
}

Expand Down Expand Up @@ -357,7 +357,7 @@ namespace yasi{
// (3) does not check parent/child relationship; the node can be possibly without a parent
// dynamically allocates a new node; increases the tree-size by one
// returns the node-ptr on success, NULL on failure
node_t* insertAt(const E& e, int index) override{
node_t* insertAt(const E& e, uint index) override{
node_t* pNode = NULL;
if (index > 0){
while (index >= _capacity){
Expand Down Expand Up @@ -471,7 +471,7 @@ namespace yasi{
string toStringNodeArray(const char* strDelim, const char* strEmptySlot) const{
// cout << endl << "ArrayBinaryTree contents:" << endl;
std::stringstream buffer;
for (int i = 0; i < _capacity; i++){
for (uint i = 0; i < _capacity; i++){
if (_arr[i] == NULL){
// print empty slots
buffer << strEmptySlot;
Expand Down
10 changes: 5 additions & 5 deletions YASI_12/ds.arraybinarytree.test.h
Expand Up @@ -22,7 +22,7 @@ namespace yasi{
ASSERT_EQ(7, n1.element) << "element should be 5";

n1.makeNull();
ASSERT_EQ(1, n1.isNull()) << "makeNull() and isNull() disagree";
ASSERT_EQ(true, n1.isNull()) << "makeNull() and isNull() disagree";
}
void indexing(){
const int N = 7;
Expand Down Expand Up @@ -77,9 +77,9 @@ namespace yasi{
ArrayBinaryTree<int> bt;
ASSERT_EQ(1, bt.ROOT_INDEX) << "Index of root is not 1";
ASSERT_LE(bt.INIT_CAPACITY, bt._arr.capacity()) << "Initial _capacity must be <= containers capacity";
ASSERT_LE(2, bt._arr.capacity()) << "Initial capacity must be >= 2";
ASSERT_LE((uint)2, bt._arr.capacity()) << "Initial capacity must be >= 2";
ASSERT_EQ(0, bt.size()) << "Initial size is not zero";
for (int i = 0; i < bt._capacity; i++){
for (uint i = 0; i < bt._capacity; i++){
ASSERT_EQ(0, bt._arr[i]) << "Pointer at index " << i << " is not initialized to NULL";
}

Expand All @@ -103,12 +103,12 @@ namespace yasi{

void fromArr(){
int srcArr[] = { 10, 20, 30, 40, 50, 60 };
int n = ARR_LENGTH(srcArr);
uint n = ARR_LENGTH(srcArr);
ArrayBinaryTree<int> bt(srcArr, n);
ASSERT_EQ(n, bt.size()) << "size of tree is not " << n;
// check node-ptrs stored in internal container
int i = 0;
for (int i = 0; i < bt._capacity; i++){
for (uint i = 0; i < bt._capacity; i++){
if (i >= 1 && i <= n){
// valid node-ptrs
ASSERT_NE(0, (int)bt._arr[i]) << "Pointer NULL at index " << i;
Expand Down
1 change: 0 additions & 1 deletion YASI_12/ds.binaryheap.h
Expand Up @@ -245,7 +245,6 @@ public:

// returns the top element
const E& top()const override {
E e;
if (size() > 0){
return bt.root()->element;
}
Expand Down
3 changes: 3 additions & 0 deletions YASI_12/ds.doublylinkedlist.h
Expand Up @@ -79,6 +79,9 @@ namespace ds{
public:
typedef DLLIterator<E, Node> iterator;
typedef node_t NodeType;

// parent methods/variables

/////// constructor/destructor ///////////
DoublyLinkedList(){
// make a unit circle: horizon--horizon--horizon
Expand Down
1 change: 0 additions & 1 deletion YASI_12/ds.doublylinkedlist.test.h
Expand Up @@ -19,7 +19,6 @@ namespace yasi{
template<class E>
void checkListElements(DoublyLinkedList<E, DListNode<E> >* pList, const E* srcArr, const int n){
E* pElems = new E[n];
int numElems;
DoublyLinkedList<E, DListNode<E> >& list = *pList;
list.elements(&pElems);
ASSERT_EQ(true, arrcmp(srcArr, pElems, n)) << "arrays are not the same" << endl
Expand Down
11 changes: 5 additions & 6 deletions YASI_12/ds.hashtablebase.h
@@ -1,8 +1,8 @@
#pragma once
#include "common.h"
#include "ds.kvpair.h"
#include "ds.doublylinkedlist.h"
#include "ds.iterator.h"
//#include "ds.doublylinkedlist.h"
//#include "ds.iterator.h"
using namespace std;

namespace yasi{
Expand All @@ -16,7 +16,7 @@ protected:
RESIZE_GROW = 0,
RESIZE_SHRINK
};
virtual void resize(const int growOrShrink, const unsigned int logFactor) = 0;
virtual void resize(const int growOrShrink, const unsigned int logFactor = 1) = 0;
void grow() { resize(RESIZE_GROW, 1); } // doubles the capacity
void shrink() { resize(RESIZE_SHRINK, 1); } // halves the capacity
public:
Expand Down Expand Up @@ -154,7 +154,7 @@ protected:
// grows/shrinks by specified logFactor
// that is, newSize is either oldSize << logFactor (grow)
// or oldSize >> logFactor (shrink)
virtual void resize(int growOrShrink, unsigned int logFactor = 1) {
virtual void resize(const int growOrShrink, const unsigned int logFactor = 1) {
unsigned int oldLogSize = _logSize;
unsigned int oldSize = _size;
unsigned int oldPopulation = _population;
Expand Down Expand Up @@ -202,7 +202,6 @@ protected:
}
public:
// the type of the entries in the hash table
HashTableBase():HashTableBase(INIT_LOGSIZE){}
HashTableBase(unsigned int logSize = INIT_LOGSIZE) : _logSize(logSize), _size(1 << logSize), _population(0), DENSITY_MAX(0.75), DENSITY_MIN(0.25){
assert(_logSize > 0);
// table = new BucketType[_size];
Expand All @@ -220,7 +219,7 @@ public:

string toString() const{
stringstream buf;
for (int i = 0; i < _size; i++){
for (uint i = 0; i < _size; i++){
buf << "[" << i << "] ";
if (!isBucketEmpty(i)) buf << table[i];
buf << endl;
Expand Down
18 changes: 17 additions & 1 deletion YASI_12/ds.iterator.h
Expand Up @@ -130,7 +130,23 @@ namespace yasi{
NodeIteratorBase(Node* pNode) : IteratorBase(pNode){}
NodeIteratorBase(const self& other) : IteratorBase(other.pNode){}
// operators
virtual E& operator* () const override { E e; if (pNode) return pNode->element; else return e; }
// if the pNode is NULL, behavior is undefined
virtual E& operator* () const override {
E e;
if (pNode)
return pNode->element;
else
// undefined behavior
// in MSVC, disable warning "returning address of local variable or temporary"
#ifdef _MSC_VER
# pragma warning( push )
# pragma warning( disable:4172)
#endif
return e;
#ifdef _MSC_VER
# pragma warning( pop )
#endif
}
self& operator=(const self& other){ pNode = other.pNode; return *this; }

};
Expand Down
2 changes: 1 addition & 1 deletion YASI_12/ds.singlylinkedlist.h
Expand Up @@ -216,7 +216,7 @@ public:
return _size;
}
inline bool empty() const override{
return _size == 0 (pHorizon == pHorizon->next() );
return _size == 0 && (pHorizon == pHorizon->next() );
}
virtual void push(const E& e) override{
this->addFront(e);
Expand Down

0 comments on commit 1fcb3f6

Please sign in to comment.