New Recite Function
[FreeRecite.git] / src / core / WordList.cpp
blob06841972b293ba3e794bd0cd1327d63f861fb301
1 #include "WordList.h"
3 namespace freeRecite {
5 WordList::WordList(unsigned init__Size)
6 :__size(init__Size),first(0),last(0)
8 for(unsigned i = 0; i < __size; ++i) {
9 if(i == 0) {
10 first = new NodeWord(0);
11 last = first;
12 for(int i = 1; i < 4; ++i)
13 pos[i] = first;
14 }else {
15 last->next = new NodeWord(i);
16 last = last->next;
18 if( i == 1 )
19 pos[1] = pos[1]->next;
20 if( i > 0 && i <= 3 )
21 pos[2] = pos[2]->next;
22 if( i > 0 && i <= 10)
23 pos[3] = pos[3]->next;
28 WordList::~WordList()
30 pos[0] = first;
31 while(first != 0) {
32 first = first->next;
33 delete pos[0];
34 pos[0] = 0;
38 int WordList::scan() {
39 if(first->status == 0) {
40 lose();
41 return 1;
42 }else
43 return pass();
46 int WordList::pass()
48 int status = first->status;
49 if( status > 0 && status < 4) //You should review it! status = 1,2,3
50 moveToPos(status);
51 else if(status == 4) //Put the word to the last to test whether you remember it.
52 moveToLast();
53 else //You have remembered it! status = 0,5
54 pop();
55 return status;
56 } //end of function WordList::pass().
58 void WordList::lose()
60 first->status = 1;
63 void WordList::moveToPos(unsigned i) {
64 if(pos[i] == last){
65 moveToLast();
66 }else {
67 ++(first->status);
68 //pos[0] is a tempory variable.
69 pos[0] = pos[i]->next;
70 pos[i]->next = first;
71 first = first->next;
72 pos[i]->next->next = pos[0];
73 advancePos(i);
77 void WordList::moveToLast() {
78 ++(first->status);
79 last->next = first;
80 last = last->next;
81 first = first->next;
82 last->next = 0;
83 advancePos();;
86 void WordList::pop() {
87 pos[0] = first;
88 if(first == last) {
89 first = 0;
90 last = 0;
91 __size = 0;
92 delete pos[0];
93 for(int i = 0; i < 4; ++i)
94 pos[i] = 0;
95 }else {
96 first = first->next;
97 delete pos[0];
98 --__size;
99 advancePos();;
103 void WordList::add(unsigned size) {
104 NodeWord *tmp = new NodeWord(size - 1);
105 last->next = tmp;
106 last = last->next;
107 ++__size;
110 void WordList::remove(unsigned size) {
111 NodeWord *tmp = first;
112 NodeWord *max = first;
113 for(int i = 0; i < __size; ++i) {
114 tmp = tmp->next;
115 if(max->index < tmp->index)
116 max = tmp;
118 //Because the user should always call it after remove
119 //the word from the contain, it subtract 2 here.
120 if( max->index == (size - 2) ){
121 max->index = first->index;
122 pop();
126 void WordList::advancePos(unsigned status) {
127 switch(status) {
128 case 0: case 3:
129 if(pos[3] != last)
130 pos[3] = pos[3]->next;
131 case 2:
132 if(pos[2] != last)
133 pos[2] = pos[2]->next;
134 case 1:
135 if(pos[1] != last)
136 pos[1] = pos[1]->next;
140 } //Namespace freeRecite end.