Results 1 to 2 of 2
  1. #1
    zTwix0R's Avatar
    Join Date
    May 2012
    Gender
    male
    Posts
    96
    Reputation
    10
    Thanks
    6
    My Mood
    Aggressive

    Basic vector class for Arduino

    I needed a vector class for Arduino to do a couple of things. Since Arduino doesn't have one and since I had issues importing the C++ STL, I decided to make my own. It doesn't use iterators, since arduino doesn't have that class either.
    Anyway, comment below ways to improve this class! Thanks

    Code:
    /*Basic Vector Class for Arduino IDE*/
    
    template<typename Data>
    typedef class vector{
    	size_t d_size; // Stores no. of actually stored objects
    	size_t d_capacity; // Stores allocated capacity
    	Data *d_data; // Stores data
    public:
    	/*------------CONSTRUCTORS & DESTRUCTORS ----------*/
    
    	vector() : d_size(0), d_capacity(0), d_data(0) {}; // Default constructor
    
    	vector(vector const &other) : d_size(other.d_size), d_capacity(other.d_capacity), d_data(0) {
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));	
    		memcpy(d_data, other.d_data, d_size*sizeof(Data));
    	}; // Copy constuctor
    
    	vector(Data* arr, size_t len) : d_size(len), d_capacity(len), d_data(0) {
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, arr, len*sizeof(Data));
    	}; // Copy constuctor
    
    	vector(size_t size, Data val = Data()) : d_size(size), d_capacity(size), d_data(0) {
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));		
    		for (size_t s = 0; s < size; s++)
    			d_data[s] = val;
    	}; // Other constructor
    	~vector() { free(d_data); }; // Destructor
    
    
    	vector const &operator=(vector const &other) {
    		free(d_data);
    		d_size = other.d_size;
    		d_capacity = other.d_capacity;
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, other.d_data,
    			d_size*sizeof(Data));
    		return *this;
    	}; // Needed for memory management
    	vector &operator=(vector &other) {
    		free(d_data);
    		d_size = other.d_size;
    		d_capacity = other.d_capacity;
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, other.d_data,
    			d_size*sizeof(Data));
    		return *this;
    	}; // Needed for memory management
    	vector &operator=(Data arr[]) {
    		free(d_data);
    		size_t len = sizeof(arr) / sizeof(Data);
    		d_size = len;
    		d_capacity = len;
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, arr,
    			d_size*sizeof(Data));
    		return *this;
    	}; // Needed for memory management
    
    
    	/*------------CAPACITY----------*/
    	size_t size() const { return d_size; }; // Size getter		
    	void resize(size_t newsize, Data val = Data()) {
    		while (newsize > d_capacity)
    			resize_capacity();
    
    		Data *newdata = (Data *)malloc(d_capacity*sizeof(Data));
    		if (d_size > newsize) //resizes to a lower size.
    			memcpy(newdata, d_data, newsize * sizeof(Data));
    		else{ //d_data is bigger.
    			if (d_size != 0) { memcpy(newdata, d_data, d_size * sizeof(Data)); }
    			for (size_t s = d_size; s < newsize; s++)
    				newdata[s] = val;
    		}
    
    		d_size = newsize;
    		free(d_data);
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, newdata, d_size * sizeof(Data));
    	};
    	size_t capacity() const { return d_capacity; };
    	boolean empty() { return d_size == 0; };
    	void reserve(size_t n) { if (n > d_capacity) resize_capacity(n); };
    	void shrink_to_fit() { d_size ? resize_capacity(d_size) : 0 ; };
    
    
    	/*------------ELEMENT ACCESS----------*/
    	Data const &operator[](size_t n) const { return d_data[n]; }; // Const getter
    	Data &operator[](size_t n) { return d_data[n]; }; // Changeable getter
    	Data at(size_t n){ return d_data[n]; };
    	Data const at(size_t n) const{ return d_data[n]; };
    	Data front(){ return d_data[0]; };
    	Data const front() const{ return d_data[0]; };
    	Data back(){ return d_data[d_size - 1]; };
    	Data const back() const{ return d_data[d_size - 1]; };
    	Data* data() { return d_data; };
    
    
    	/*------------MODIFIERS----------*/
    	void assign(size_t n, const Data& val){d_size = 0; resize(n, val); };
    	void push_back(Data const &x) { resize(d_size+1, x);}; // Adds new value. If needed, allocates more space.
    	void pop_back() { resize(d_size - 1); };
    	void insert(size_t position, const Data &val){
    		while (d_size +1 > d_capacity) resize_capacity();
    		Data *newdata = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(newdata, d_data, position * sizeof(Data));
    		newdata[position] = val;
    		memcpy(newdata + position + 1, d_data + position, (d_size - position)* sizeof(Data));
    		d_size++;
    		free(d_data);
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, newdata, d_size*sizeof(Data));
    	};
    	void erase(size_t position){
    		Data *newdata = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(newdata, d_data, position * sizeof(Data));
    		memcpy(newdata + position, d_data + position + 1, (d_size - position - 1)* sizeof(Data)); d_size--;
    		free(d_data);
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, newdata, d_size*sizeof(Data));
    	};
    	void erase(size_t first, size_t last){
    		Data *newdata = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(newdata, d_data, first * sizeof(Data));
    		memcpy(newdata + first, d_data + last + 1, (d_size - last - 1)* sizeof(Data));
    		free(d_data);
    		d_size -= last - first + 1;
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, newdata, d_size*sizeof(Data));
    	};
    	
    	void swap(vector& x){
    		vector temp = x;
    		x = *this;
    		d_capacity = temp.capacity();
    		d_size = temp.size();
    		free(d_data);
    		d_data = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(d_data, temp.data(), d_size*sizeof(Data));
    	}; //Swaps the data from vector X	
    	void clear() { resize(0); };
    private:
    	void resize_capacity(size_t newsize) {
    		d_capacity = newsize ? newsize : 1;
    		d_size = d_size > d_capacity ? d_capacity : d_size;
    		Data *newdata = (Data *)malloc(d_capacity*sizeof(Data));
    		memcpy(newdata, d_data, d_size * sizeof(Data));
    		free(d_data);
    		d_data = newdata;
    	}; // Changes d_data's allocated memory size.
    	void resize_capacity() { resize_capacity(d_capacity * 2); };// Allocates double the old space
    
    };

  2. #2
    zTwix0R's Avatar
    Join Date
    May 2012
    Gender
    male
    Posts
    96
    Reputation
    10
    Thanks
    6
    My Mood
    Aggressive
    Forgot to say, but I'm having problems making a constructor for a static array.
    For some reason, everytime I try to obtain the size of the array, it always returns 1.

Similar Threads

  1. Second LT 4th class for sale.
    By thebazarr21 in forum Trade Accounts/Keys/Items
    Replies: 11
    Last Post: 07-04-2010, 08:10 PM
  2. Master Sgt 3rd class for sale
    By octogon in forum Trade Accounts/Keys/Items
    Replies: 10
    Last Post: 09-16-2009, 10:56 PM
  3. Tutorial - How to use Visual Basics 6 (vb6) for WarRock hacks
    By Oneirish in forum Visual Basic Programming
    Replies: 17
    Last Post: 05-26-2008, 07:24 AM
  4. Tutorial - How to use Visual Basics 6 (vb6) for WarRock "easy"
    By Oneirish in forum Programming Tutorials
    Replies: 2
    Last Post: 04-23-2008, 08:23 AM
  5. [Source]Visual Basic Phishing Files for warrock (client + server)
    By HeXel in forum Trade Accounts/Keys/Items
    Replies: 9
    Last Post: 03-10-2008, 05:41 AM