Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module list;
- interface IList(T) {
- @property T[] array_show();
- @property bool contains(T x);
- @property bool empty();
- @property size_t length();
- @property T[] reverse();
- @property T[] uniq();
- void append(T x);
- // void create(T[] ...);
- void extend(T[] x...);
- void insert(T x,size_t n);
- void remove(T x);
- void remove_nth(size_t n);
- }
- class List(T) : IList!T {
- private:
- T[] list;
- public:
- T[] array_show() @property {
- return list;
- }
- bool contains(T x) @property {
- foreach (elem; list) {
- if (elem == x) return true;
- }
- return false;
- }
- bool empty() @property {
- if (list.length == 0) return true;
- else return false;
- }
- size_t length() @property {
- return list.length;
- }
- T[] reverse() @property {
- T[] tmp;
- foreach_reverse(elem; list) {
- tmp ~= elem;
- }
- return tmp;
- }
- T[] uniq() @property {
- auto tmp = new List!T;
- foreach (elem;list) {
- if (!tmp.contains(elem)) tmp.append(elem);
- }
- return tmp.array_show;
- }
- void append(T x) {
- list ~= x;
- }
- void extend(T[] x...) {
- list ~= x;
- }
- void insert(T x,size_t n) {
- T[] tmp;
- if (n == list.length) list ~= x;
- else {
- tmp = list[0..n] ~ x ~ list[n..$];
- list = tmp;
- }
- }
- void remove(T x) {
- T[] tmp;
- foreach (elem; list) {
- if (elem != x) tmp ~= elem;
- }
- list = tmp;
- }
- void remove_nth(size_t n) {
- T[] tmp;
- tmp = list[0..n] ~ list[n+1..$];
- list = tmp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement