Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bflibcpp/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ defer
ifeq ($(CONFIG),release) # release
BIN_NAME = $(LIB_FILE_NAME_RELEASE)
FLAGS = $(CPPFLAGS) -Isrc/ $(CPPSTD) -I../bflibc/bin/release

### Debug settings
else ifeq ($(CONFIG),debug) # debug
BIN_NAME = $(LIB_FILE_NAME_DEBUG)
#ADDR_SANITIZER = -fsanitize=address
FLAGS = $(CPPFLAGS) -DDEBUG -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -I../bflibc/bin/debug

### Test settings
else ifeq ($(CONFIG),test) # test
BIN_NAME = libbfcpp-test
Expand Down
17 changes: 10 additions & 7 deletions bflibcpp/src/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "vector.hpp"
#include <string.h>
#include "exception.hpp"
#include "swap.hpp"

namespace BF {

Expand All @@ -26,7 +27,7 @@ namespace BF {
* Objects stored in array are assumed to be owned by owner of array
* object
*/
template <typename T, typename S = size_t>
template <typename T, typename S = long>
class Array : public Vector<T,S> {
public:
virtual const char * className() const {
Expand Down Expand Up @@ -382,12 +383,6 @@ class Array : public Vector<T,S> {

public:

/*
T operator[](S index) const {
return this->objectAtIndex(index);
}
*/

void operator=(const std::initializer_list<T> & list) {
this->saveArray(list);
}
Expand All @@ -400,6 +395,14 @@ class Array : public Vector<T,S> {
return *this;
}

virtual T operator[](S index) const {
return this->objectAtIndex(index);
}

virtual T & operator[](S index) {
return this->refObjectAtIndex(index);
}

// Comparators
public:
/**
Expand Down
1 change: 1 addition & 0 deletions bflibcpp/src/bflibcpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "deque.hpp"
#include "hash.hpp"
#include "defer.hpp"
#include "sort.hpp"

#endif // BFLIBCPP_HPP

63 changes: 33 additions & 30 deletions bflibcpp/src/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
#include "access.hpp"
#include "vector.hpp"
#include "exception.hpp"
#include "swap.hpp"
#include <iostream>
#include <initializer_list>

namespace BF {

template<template <typename...> class T> struct Sort;

/**
* Linked List implementation
*
Expand All @@ -29,15 +32,18 @@ namespace BF {
* Unless a callback is specified, by default the node
* object memory will not be deallocated.
*/
template <typename L, typename S = size_t>
template <typename L, typename S = long>
class List : public Vector<L,S> {
public:
friend struct Sort<List>;

/**
* Bi-directional node
*/
class Node : public Object {
friend List<L, S>;
friend List<L,S>;

friend struct Sort<List>;
public:
Node * next() const {
return this->right;
Expand Down Expand Up @@ -207,13 +213,13 @@ class List : public Vector<L,S> {
// returns object at index
// returns 0 if an error ocurred
virtual L objectAtIndex(S index) const {
Node * n = this->nodeAtIndex(index, this->_head, 0);
Node * n = this->nodeAtIndex(index, this->_head);
if (n) return n->object();
else return 0;
}

virtual L & refObjectAtIndex(S index) {
Node * n = this->nodeAtIndex(index, this->_head, 0);
Node * n = this->nodeAtIndex(index, this->_head);
if (n) return n->refobject();
throw Exception("no object found at %d", (int) index);
}
Expand Down Expand Up @@ -276,9 +282,8 @@ class List : public Vector<L,S> {
for (; n; n = n->prev()) {
// Get random node
int r = rand() % (i + 1);
Node * tmp = this->nodeAtIndex(r, this->_head, 0);
int err = this->swap(n, tmp); // swap nodes
if (err) return err; // leave if there was an error in swapping
Node * tmp = this->nodeAtIndex(r, this->_head);
BF::swap<L>(n->obj, tmp->obj); // swap nodes
i--;
}

Expand Down Expand Up @@ -316,20 +321,6 @@ class List : public Vector<L,S> {

private:

/**
* Swaps a and b objects
*/
static int swap(Node * a, Node * b) {
// Don't continue with this if a or b are null
if (!a || !b) return -1;
else {
L obj = a->obj;
a->obj = b->obj;
b->obj = obj;
return 0;
}
}

/**
* Allows us to set list with {...} notation
*
Expand All @@ -355,16 +346,20 @@ class List : public Vector<L,S> {
}

/**
* Recursively traverses through linked list until we read the reqIndex'th node
* iteratively traverses through linked list until we read the reqIndex'th node
*
* we start with the search from the `node` param
*
* if reqIndex < 0, returns NULL
*/
Node * nodeAtIndex(S reqIndex, Node * node, S currIndex) const {
if (node) {
if (currIndex == reqIndex) {
return node;
} else {
return this->nodeAtIndex(reqIndex, node->right, ++currIndex);
}
} else return 0;
Node * nodeAtIndex(S reqIndex, Node * node) const {
if (reqIndex < 0) return NULL;

while (--reqIndex >= 0 && node) {
node = node->right;
}

return node;
}

/**
Expand Down Expand Up @@ -484,6 +479,14 @@ class List : public Vector<L,S> {
this->set(list);
}

virtual L operator[](S index) const {
return this->objectAtIndex(index);
}

virtual L & operator[](S index) {
return this->refObjectAtIndex(index);
}

public:

/**
Expand Down
Loading
Loading