LiFan
9 天以前 63eebabbfee1bc84850b36967ecac5116a28b73f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
 
#include "system/base.hpp"
#include "wobject/xobject.hpp"
#include "wobject/xstring.hpp"
 
template<class T>
class xarray : public xobject
{
public:
    xarray():length_(0), capacity_(0), items(nullptr) {}
 
public:
    xarray* push_back(T t)
    {
        sure_space();
 
        items[length_] = t;
        ++length_;
        return this;
    }
 
 
    xarray* pop_back()
    {
        if (length() > 0)
            erase(length() - 1);
        return this;
    }
 
    void clear()
    {
        for (int i = 0; i <= length(); i++)
            items[i].~T();
        length_ = 0;
    }
 
    xarray* erase(int index)
    {
        if (index < 0 || index > length_ - 1)
        {
            //throw("...");
            return this;
        }
        //move data
        for (int i = index; i < length_ - 1; i++)items[i] = items[i + 1];
        --length_;
        return this;
    }
 
    xarray* insert_before(int index, T t)
    {
        sure_space();
 
        if (index < 0)
        {
            for (int i = 0; i < length_ - 1; i++)items[i] = items[i + 1];
            items[0] = t;
            return this;
        }
        if (index >= length_)
        {
            items[length_] = t;
            ++length_;
        }
        return this;
    }
 
    xarray* insert_after(int index, T t)
    {
        return insert_before(index + 1, t);
    }
 
    T back()
    {
        if (length_)
            return items[length_ - 1];
        return 0;
    }
 
    T front()
    {
        if (length_)
            return items[0];
        return 0;
    }
 
    int length()
    {
        return length_;
    }
 
    T item(int index)
    {
        if (index >= 0 && index < length_) return items[index];
        return 0;
    }
 
    int find(T t)
    {
        for (int i = 0; i < length_; i++)
        {
            if (items[i] == t) return i;
        }
        return -1;
    }
private:
    bool sure_space()
    {
        if (length_ == capacity_)
        {
            if (!items)
            {
                items = new T[2];
                capacity_ = 2;
            }
            else
            {
                capacity_ = capacity_ * 2;
                T* temp = new T[capacity_];
                //¿ÉÒÔÓÅ»¯Îª¿½±´²Ù×÷
                for (int i = 0; i < length_; ++i)
                {
                    temp[i] = items[i];
                }
                delete[] items;
                items = temp;
            }
        }
        return true;
    }
private:
    T* items;
    int length_;
    int capacity_;
};