服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 自定义的Troop 泛型类( c++, java和c#)的实现代码

自定义的Troop 泛型类( c++, java和c#)的实现代码

2020-10-29 15:10白首太玄經 Java教程

这篇文章主要介绍了自定义的Troop泛型类( c++, java和c#)的实现代码的相关资料,需要的朋友可以参考下

Troop<T>是一个泛型列表操作类,适用于非高性能和非大数据量的要求。包括了:取值get,赋值set,追加append,插入insert,清除remove,进队enqueue,出队dequeue,交换swap,滚动roll,进栈push,出栈pop等日常操作。

 
?
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//for more information, please access http://www.one-lab.net
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace onelab
{
  public interface ISortable
  {
    void sort(bool decending);
  }
 
  public interface IStringable
  {
    int fromString(String input);
    String toString();
  }
 
  public class Troop<T> : ICloneable
  {
    public override int GetHashCode()
    {
      return base.GetHashCode();
    }
    public override bool Equals(object obj) { return this == (Troop<T>)obj; }
    public object Clone()
    {
      Troop<T> oValue = new Troop<T>();
      oValue.cloneFrom(this);
      return oValue;
    }
    protected bool equalsTo(Troop<T> aValue)
    {
      int len = aValue.length();
      if (len == this.length())
      {
        for (int n = 0; n < len; n++)
        {
          if (!aValue.data[n].Equals(data[n]))
            return false;
        }
        return true;
      }
      return false;
    }
    protected void cloneFrom(Troop<T> aValue)
    {
      data.Clear();
      data.AddRange(aValue.data);
    }
    public static bool operator ==(Troop<T> a0, Troop<T> a1) { object o1 = (object)a0; object o2 = (object)a1; if (o1 == null && o2 == null) return true; return (o1 == null || o2 == null) ? false : a0.equalsTo(a1); }
    public static bool operator !=(Troop<T> a0, Troop<T> a1) { object o1 = (object)a0; object o2 = (object)a1; if (o1 == null && o2 == null) return false; return (o1 == null || o2 == null) ? true : !a0.equalsTo(a1); }
    private bool mHasError = false;
    public bool hasError() { return mHasError; }
    public List<T> data = new List<T>();
    public T get(int index)
    {
      mHasError = false;
      if (index >= 0 && index < data.Count)
        return data[index];
      mHasError = true;
      return default(T);
    }
    public bool set(int index, T value)
    {
      if (index >= 0 && index < data.Count)
      {
        data[index] = value;
        return true;
      }
      return false;
    }
    public void append(T value)
    {
      data.Add(value);
    }
    public bool insert(int index, T value)
    {
      if (index >= 0 && index < data.Count)
      {
        data.Insert(index, value);
        return true;
      }
      return false;
    }
    public void appendRange(ref List<T> range)
    {
      data.AddRange(range);
    }
    public bool insertRange(int index, ref List<T> range)
    {
      if (index >= 0 && index < data.Count)
      {
        data.InsertRange(index, range);
        return true;
      }
      return false;
    }
    public void clear()
    {
      data.Clear();
    }
    public bool remove(int index)
    {
      if (index >= 0 && index < data.Count)
      {
        data.RemoveAt(index);
        return true;
      }
      return false;
    }
    public bool removeRange(int index, int count)
    {
      if (count > 0 && index >= 0 && ((index + count) <= data.Count))
      {
        data.RemoveRange(index, count);
        return true;
      }
      return false;
    }
    public int length()
    {
      return data.Count;
    }
    public void enqueue(T value)
    {
      data.Insert(0, value);
    }
    public T dequeue()
    {
      mHasError = false;
      int length = data.Count;
      if (length > 0)
      {
        T b = data[length - 1];
        data.RemoveAt(length - 1);
        return b;
      }
      mHasError = true;
      return default(T);
    }
    public void push(T value)
    {
      data.Add(value);
    }
    public T pop()
    {
      return dequeue();
    }
    public Troop<T> getRange(int index, int count)
    {
      Troop<T> output = new Troop<T>();
      mHasError = true;
      if (count > 0 && index >= 0 && ((index + count) <= data.Count))
      {
        output.data = data.GetRange(index, count);
        mHasError = false;
      }
      return output;
    }
    public void reverse()
    {
      data.Reverse();
    }
    public bool swap(int index0, int index1)
    {
      int length = data.Count;
      if (index0 >= 0 && index0 < length && index1 >= 0 && index1 < length)
      {
        T v = data[index0];
        data[index0] = data[index1];
        data[index1] = v;
        return true;
      }
      return false;
 
    }
    public T take(int index)
    {
      mHasError = false;
      if (index >= 0 && index < data.Count)
      {
        T v = data[index];
        data.RemoveAt(index);
        return v;
      }
      mHasError = true;
      return default(T);
    }
    public void rollForward(int offset)
    {
      if (offset >= data.Count) return;
      List<T> left = data.GetRange(offset - 1, data.Count - offset + 1);
      List<T> right = data.GetRange(0, offset - 1);
      data.Clear();
      data.AddRange(left);
      data.AddRange(right);
    }
    public void rollBackward(int offset)
    {
      if (offset >= data.Count) return;
      List<T> left = data.GetRange(offset, data.Count - offset);
      List<T> right = data.GetRange(0, offset);
      data.Clear();
      data.AddRange(left);
      data.AddRange(right);
    }
  }
}
 
?
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
//for more information, please access http://www.one-lab.net
 
#ifndef VALUES_H
#define VALUES_H
 
#include <QList>
#include <QDebug>
 
namespace onelab
{
  class ISortable
  {
  public:
    virtual void sort(bool decending) = 0;
  };
 
  template <typename T>
  class Troop
  {   
  private:
    bool mHasError;
    static bool equals(const Troop<T> &from, const Troop<T> &to);
    
  public:
    QList<T> data;
    Troop();
    T get(int index);
    bool set(int index, T value);
    void append(T value);
    bool insert(int index, T value);
    void appendRange(const QList<T>& range);
    bool insertRange(int index, const QList<T>& range);
    void clear();
    bool remove(int index);
    bool removeRange(int index, int count);
    int length() const;
    void enqueue(T value);
    T dequeue();
    void push(T value);
    T pop();
    bool getRange(int index, int count, Troop<T>& output);
    void reverse();
    bool swap(int index0, int index1);
    T take(int index);
    void rollForward(int offset);
    void rollBackward(int offset);
    bool operator==(const Troop& input) const;
    bool operator!=(const Troop& input) const;
    bool hasError() const;
    virtual QString toString();
    virtual int fromString(const QString& input);
  };
}
 
#endif // VALUES_H
java" id="highlighter_641682">
 
?
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//for more information, please access http://www.one-lab.net
 
#include "values.h"
#include <QStringList>
#include <QDateTime>
 
namespace onelab
{
template <typename T>
bool Troop<T>::equals(const Troop<T> &from, const Troop<T> &to)
{
  int len = from.length();
  if (len == to.length())
  {
    for (int n = 0; n < len; n++)
    {
      if (from.data[n] != to.data[n])
        return false;
    }       
    return true;
  }
  return false
}
 
template <typename T>
Troop<T>::Troop()
{
  mHasError = false;
}
 
template <typename T>
T Troop<T>::get(int index)
{
  mHasError = false;
  if (index >= 0 && index < data.length())
    return data[index];
  mHasError = true;
  return (T)0;   
}
 
template <typename T>
bool Troop<T>::set(int index, T value)
{
  if (index >= 0 && index < data.length())
  {
    data[index] = value;
    return true;
  }
  return false;   
}
 
template <typename T>
void Troop<T>::append(T value)
{
  data.append(value);
}
 
template <typename T>
bool Troop<T>::insert(int index, T value)
{
  if (index >= 0 && index < data.length())
  {
    data.insert(index, value);
    return true;
  }
  return false;   
}
 
template <typename T>
void Troop<T>::appendRange(const QList<T> &range)
{
  data.append(range);
}
 
template <typename T>
bool Troop<T>::insertRange(int index, const QList<T> &range)
{
  int length = data.length();
  if (index >= 0 && index < length)
  {
    QList<T> left = data.mid(0, index);
    QList<T> right = data.mid(index, -1);
    left.append(range);
    data.clear();
    data.append(left);
    data.append(right);
    return true;
  }
  return false;   
}
 
template <typename T>
void Troop<T>::clear()
{
  data.clear();
}
 
template <typename T>
bool Troop<T>::remove(int index)
{
  if (index >= 0 && index < data.length())
  {
    data.removeAt(index);
    return true;
  }
  return false;   
}
 
template <typename T>
bool Troop<T>::removeRange(int index, int count)
{
  if (count > 0 && index >= 0 && ((index + count) <= data.length()))
  {
    for (int n = 0; n < count; n++)
      data.removeAt(index);
    return true;
  }
  return false;   
}
 
template <typename T>
int Troop<T>::length() const
{
  return data.length();
}
 
template <typename T>
void Troop<T>::enqueue(T value)
{
  data.insert(0, value);
}
 
template <typename T>
T Troop<T>::dequeue()
{
  mHasError = false;
  int length = data.length();
  if (length > 0)
  {
    T b = data[length - 1];
    data.removeAt(length - 1);
    return b;
  }
  mHasError = true;
  return (T)0;   
}
 
template <typename T>
void Troop<T>::push(T value)
{
  data.append(value);
}
 
template <typename T>
T Troop<T>::pop()
{
  return dequeue();
}
 
template <typename T>
bool Troop<T>::getRange(int index, int count, Troop<T>& output)
{
  mHasError = true;
  if (count > 0 && index >= 0 && ((index + count) <= data.length()))
  {
    output.data = data.mid(index, count);
    mHasError = false;
  }
  return mHasError;
}
 
template <typename T>
void Troop<T>::reverse()
{
  int length = data.length();
  QList<T> newData;
  for (int n = length - 1; n >= 0; n--)
    newData.append(data[n]);
  
  data.clear();
  data = newData;   
}
 
template <typename T>
bool Troop<T>::swap(int index0, int index1)
{
  int length = data.length();
  if (index0 >= 0 && index0 < length && index1 >= 0 && index1 < length)
  {
    T v = data[index0];
    data[index0] = data[index1];
    data[index1] = v;
    return true;
  }
  return false;
}
 
template <typename T>
T Troop<T>::take(int index)
{
  mHasError = false;
  if (index >= 0 && index < data.length())   
  {
    T v = data[index];
    data.removeAt(index);
    return v;
  }
  mHasError = true;
  return (T)0;
}
 
template <typename T>
void Troop<T>::rollForward(int offset)
{
  if (offset >= data.length()) return;
  QList<T> left = data.mid(offset - 1, -1);
  QList<T> right = data.mid(0, offset - 1);
  data.clear();
  data.append(left);
  data.append(right);
}
 
template <typename T>
void Troop<T>::rollBackward(int offset)
{
  if (offset >= data.length()) return;
  QList<T> left = data.mid(offset, -1);
  QList<T> right = data.mid(0, offset);   
  data.clear();
  data.append(left);
  data.append(right);
}
 
template <typename T>
bool Troop<T>::operator!=(const Troop& input) const
{
  return !equals(input, *this);
}
 
template<typename T>
bool Troop<T>::hasError() const
{
  return mHasError; 
}
 
template<typename T>
QString Troop<T>::toString()
{
  return QString(); 
}
 
template<typename T>
int Troop<T>::fromString(const QString&)
{
  return 0
}
 
template <typename T>
bool Troop<T>::operator==(const Troop& input) const
{
  return equals(input, *this);
}
 
}
//for more information, please access http://www.one-lab.net
 
package net.onelab;
 
import java.util.ArrayList;
 
public class Troop<T> implements Cloneable {
 public Object clone() {
 Troop<T> obj = new Troop<T>();
 obj.data.addAll(data);
 return obj;
 }
 
 private boolean mHasError = false;
 public boolean hasError() { return mHasError; }
 public ArrayList<T> data = new ArrayList<T>();
 public T get(int index) {
 mHasError = false;
 if (index >= 0 && index < data.size())
  return data.get(index);
 mHasError = true;
 return null;
 
 }
 public boolean set(int index, T value) {
 if (index >= 0 && index < data.size())
 {
  data.set(index, value);
  return true;
 }
 return false;
 }
  public void append(T value) {
    data.add(value);
  }
  public boolean insert(int index, T value) {
    if (index >= 0 && index < data.size())
    {
      data.add(index, value);
      return true;
    }
    return false;
  }
  public void appendRange(ArrayList<T> range) {
    data.addAll(range);
  }
  public boolean insertRange(int index, ArrayList<T> range) {
    if (index >= 0 && index < data.size())
    {
      data.addAll(index, range);
      return true;
    }
    return false;
  }
  public void clear() {
    data.clear();
  }
  public boolean remove(int index) {
    if (index >= 0 && index < data.size())
    {
      data.remove(index);
      return true;
    }
    return false;
  }
  public boolean removeRange(int index, int count) {
    if (count > 0 && index >= 0 && ((index + count) <= data.size()))
    {
      for (int n = 0; n < count; n++)
       data.remove(index);
      
      return true;
    }
    return false;
  }
  public int length() {
    return data.size();
  }
  public void enqueue(T value) {
    data.add(0, value);
  }
  public T dequeue() {
    mHasError = false;
    int length = data.size();
    if (length > 0)
    {
      T b = data.get(length - 1);
      data.remove(length - 1);
      return b;
    }
    mHasError = true;
    return null;
  }
  public void push(T value) {
    data.add(value);
  }
  public T pop() {
    return dequeue();
  }
  public Troop<T> getRange(int index, int count) {
    mHasError = true;
    Troop<T> output = new Troop<T>();
    if (count > 0 && index >= 0 && ((index + count) <= data.size()))
    {
     output.data.addAll(data.subList(index, index + count));
      mHasError = false;
    }
    return output;
  }
  public void reverse() {
   int length = data.size();
   ArrayList<T> newData = new ArrayList<T>();
   for (int n = length - 1; n >= 0; n--)
   newData.add(data.get(n));
   
   data.clear();
   data = newData;
  }
 
  boolean swap(int index0, int index1) {
    int length = data.size();
    if (index0 >= 0 && index0 < length && index1 >= 0 && index1 < length)
    {
      T v = data.get(index0);
      data.set(index0, data.get(index1));
      data.set(index1, v);
      return true;
    }
    return false;  
  }
  T take(int index)
  {
    mHasError = false;
    if (index >= 0 && index < data.size())
    {
      T v = data.get(index);
      data.remove(index);
      return v;
    }
    mHasError = true;
    return null;
  }
  void rollForward(int offset)
  {
    if (offset >= data.size()) return;
    ArrayList<T> left = new ArrayList<T>();
    left.addAll(data.subList(offset - 1, data.size()));
    ArrayList<T> right = new ArrayList<T>();
    right.addAll(data.subList(0, offset - 1));
    data.clear();
    data.addAll(left);
    data.addAll(right);
  }
  void rollBackward(int offset)
  {
    if (offset >= data.size()) return;
    ArrayList<T> left = new ArrayList<T>();
    left.addAll(data.subList(offset, data.size()));
    ArrayList<T> right = new ArrayList<T>();
    right.addAll(data.subList(0, offset));
    data.clear();
    data.addAll(left);
    data.addAll(right);
  }  
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:https://my.oschina.net/u/1377242/blog/908322

延伸 · 阅读

精彩推荐