Skip to content

Commit

Permalink
use move
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuhui ZHI authored and Yuhui ZHI committed Nov 18, 2016
1 parent 729b2f9 commit f328551
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/obj
/.obj
matrix.out
27 changes: 27 additions & 0 deletions Column.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Column.h"


Column::Column() : column{}, pieces{}
{
// nothing to do
}

Column::Column(int size, Char c) : column(size, std::move(c)), pieces{String(size, true, size - 1)}
{
// nothing to do
}

void Column::init(Generator & g)
{
pieces.emplace_back(g.generateEmpty());
}

bool Column::needNewString() const
{
const auto & last = pieces.back();
if (last.getLen() == last.getPos())
{
return true;
}
return false;
}
24 changes: 12 additions & 12 deletions Data.h → Column.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#ifndef DATA_H_
#define DATA_H_
#ifndef COLUMN_H_
#define COLUMN_H_

#include "String.h"
#include "Generator.h"
#include "Char.h"
#include <deque>


class Data
class Column
{
public:
Data();
Data(int, const Char &);
Column();
Column(int, Char);
void init(Generator &);

const Char * getElement(int pos) const
{
return &data[pos];
return &column[pos];
}

char getChar(int pos, const string & s = "string by default")
Expand All @@ -40,11 +40,11 @@ class Data
}
}
const auto & it = pieces.front();
if (it.getLen() + data.size() == it.getPos() + 1)
if (it.getLen() + column.size() == it.getPos() + 1)
{
pieces.pop_front();
}
int size = data.size();
int size = column.size();
int pos, len, head, num, color, delta;
constexpr int SIZE = sizeof...(st);
for (auto it = pieces.begin(); it != pieces.end(); ++it)
Expand All @@ -56,7 +56,7 @@ class Data
delta = pos - head;
if (it->isEmpty())
{
data[size - 1 - head] = Char(generator.EMPTY, color_empty);
column[size - 1 - head] = Char(generator.EMPTY, color_empty);
}
else
{
Expand All @@ -76,11 +76,11 @@ class Data
}
if (SIZE == 0)
{
data[size - 1 - head--] = Char(generator.generateChar(), color);
column[size - 1 - head--] = Char(generator.generateChar(), color);
}
else
{
data[size - 1 - head--] = Char(getChar((pos + 1 > size) ? (pos - size + 1 + i) : i, std::forward<StringType>(st)...), color);
column[size - 1 - head--] = Char(getChar((pos + 1 > size) ? (pos - size + 1 + i) : i, std::forward<StringType>(st)...), color);
}
++delta;
}
Expand All @@ -90,7 +90,7 @@ class Data
}
private:
std::deque<String> pieces;
std::vector<Char> data;
std::vector<Char> column;

bool needNewString() const;
};
Expand Down
27 changes: 0 additions & 27 deletions Data.cpp

This file was deleted.

8 changes: 4 additions & 4 deletions Scene.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "Scene.h"


Scene::Scene(int cols, int lines, int time) : COLS{cols}, generator{lines}, millisec(time), scene(lines, nullptr), data(cols, Data(lines, Char(Generator::EMPTY, SysTool::COLOR_EMPTY)))
Scene::Scene(int cols, int lines, int time) : COLS{cols}, generator{lines}, millisec(time), scene(lines, nullptr), column(cols, Column(lines, Char(Generator::EMPTY, SysTool::COLOR_EMPTY)))
{
std::for_each(scene.begin(), scene.end(), [=](Char **& p){ p = new Char *[cols]; });
for (int i = 0; i < data.size(); ++i)
for (int i = 0; i < column.size(); ++i)
{
data[i].init(generator);
column[i].init(generator);
for (int j = 0; j < scene.size(); ++j)
{
scene[j][i] = const_cast<Char *>(data[i].getElement(lines - j - 1));
scene[j][i] = const_cast<Char *>(column[i].getElement(lines - j - 1));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Scene.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SCENE_H_
#define SCENE_H_

#include "Data.h"
#include "Column.h"
#include "Generator.h"
#include "SysTool.h"
#include <iostream>
Expand Down Expand Up @@ -38,15 +38,15 @@ class Scene
millisec = (millisec == SysTool::TIME_HIGH) ? millisec : millisec + SysTool::TIME_DELTA;
}
showScene();
std::for_each(data.begin(), data.end(), [&](Data & d){ d.refresh(generator, SysTool::COLOR_HEAD, SysTool::COLOR_BODY, SysTool::COLOR_TAIL, SysTool::COLOR_EMPTY, std::forward<StringType>(st)...); });
std::for_each(column.begin(), column.end(), [&](Column & d){ d.refresh(generator, SysTool::COLOR_HEAD, SysTool::COLOR_BODY, SysTool::COLOR_TAIL, SysTool::COLOR_EMPTY, std::forward<StringType>(st)...); });
std::this_thread::sleep_for(std::chrono::milliseconds(millisec));
}
}
private:
const int COLS;
Generator generator;
vector<Char **> scene;
vector<Data> data;
vector<Column> column;
int millisec;

void showScene();
Expand Down
4 changes: 1 addition & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ int main(int argc, char ** argv)
std::cout<<"| |"<<std::endl;
std::cout<<"| |"<<std::endl;
std::cout<<"------------------------------------------"<<std::endl;
std::cin.get();



std::cin.get();

SysTool::init();
Scene scene(SysTool::getCols(), SysTool::getLines());
Expand Down
6 changes: 3 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ cflags:=-lncurses

TARGET:=matrix.out
CPPFILES:=$(wildcard *.cpp)
OBJDIRECTORY:=.obj
#OBJFILES:=$(CPPFILES:.cpp=.o)
OBJFILES:=$(addprefix obj/,$(notdir $(CPPFILES:.cpp=.o)))
OBJDIRECTORY:=obj
OBJFILES:=$(addprefix $(OBJDIRECTORY)/,$(notdir $(CPPFILES:.cpp=.o)))


.PHONY: all
Expand All @@ -21,7 +21,7 @@ $(OBJDIRECTORY)/%.o: %.cpp
#%.o: %.cpp
$(g++11) -c -o $@ $< -g

-include $(addprefix obj/,$(notdir $(CPPFILES:.cpp=.d)))
-include $(addprefix $(OBJDIRECTORY)/,$(notdir $(CPPFILES:.cpp=.d)))


.PHONY: clean
Expand Down

0 comments on commit f328551

Please sign in to comment.