Created
September 28, 2024 04:02
-
-
Save tigercoding56/84bf50b9eb51a7833b7f51162da91322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.stream.Stream; | |
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or | |
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. | |
public class Main { | |
public static class ll<E extends Comparable<E>> implements Iterator<E>, Iterable<E> { | |
//who needs nodes anyway | |
/* | |
░░░░░▄▄▄▄▀▀▀▀▀▀▀▀▄▄▄▄▄▄░░░░░░░ | |
░░░░░█░░░░▒▒▒▒▒▒▒▒▒▒▒▒░░▀▀▄░░░░ | |
░░░░█░░░▒▒▒▒▒▒░░░░░░░░▒▒▒░░█░░░ | |
░░░█░░░░░░▄██▀▄▄░░░░░▄▄▄░░░░█░░ | |
░▄▀▒▄▄▄▒░█▀▀▀▀▄▄█░░░██▄▄█░░░░█░ | |
█░▒█▒▄░▀▄▄▄▀░░░░░░░░█░░░▒▒▒▒▒░█ | |
█░▒█░█▀▄▄░░░░░█▀░░░░▀▄░░▄▀▀▀▄▒█ | |
░█░▀▄░█▄░█▀▄▄░▀░▀▀░▄▄▀░░░░█░░█░ | |
░░█░░░▀▄▀█▄▄░█▀▀▀▄▄▄▄▀▀█▀██░█░░ | |
░░░█░░░░██░░▀█▄▄▄█▄▄█▄████░█░░░ | |
░░░░█░░░░▀▀▄░█░░░█░█▀██████░█░░ | |
░░░░░▀▄░░░░░▀▀▄▄▄█▄█▄█▄█▄▀░░█░░ | |
░░░░░░░▀▄▄░▒▒▒▒░░░░░░░░░░▒░░░█░ | |
░░░░░░░░░░▀▀▄▄░▒▒▒▒▒▒▒▒▒▒░░░░█░ | |
░░░░░░░░░░░░░░▀▄▄▄▄▄▄▄▄▄▄██▀▀░░ | |
*/ | |
Object[] first = null;//hehehehehehehhehehhehehehehhehehe | |
Object[] current; | |
int l = -1; | |
private int currentind=-1; | |
private int ll; | |
public void rotateToFront() { | |
if (gi(l) != null) { | |
Object[] t = gi(l).clone(); | |
Object tmp = gi(l);//cant be single line bc java | |
tmp = null; | |
t[1] = first.clone(); | |
first = t; | |
} | |
} | |
public E remove(int i) { | |
if (i > l) { | |
return null; | |
} | |
if (i == 0) { | |
E t = (E) first[0]; | |
first = (Object[]) first[1]; | |
l -= 1; | |
return t; | |
} else { | |
E t = (E) first[0]; | |
Object[] tmp = gi(i - 1); | |
tmp[1] = (Object[]) ((Object[]) tmp[1])[1]; | |
l-=1; | |
return t; | |
} | |
} | |
public E addfirst(E i) { | |
l++; | |
if (first == null) { | |
first = new Object[2]; | |
} | |
Object[] tmp = first.clone(); | |
first[1] = tmp; | |
first[0] = i; | |
return i; | |
} | |
public E addlast(E i) { | |
if (first == null) { | |
first = new Object[2]; | |
first[0]=i; | |
l=1; | |
return i; | |
} | |
Object[] tmp=gi(l+2); | |
if (tmp==null){ | |
tmp=new Object[]{i,null}; | |
}else { | |
tmp[1] = new Object[]{i, null}; | |
} | |
l++; | |
return i; | |
} | |
private Object[] gi(int i){ | |
if (first==null){ | |
return null; | |
}if (i==0){ | |
return first; | |
} | |
Object[] temp; | |
temp=first; | |
int n=0; | |
while(temp[1]!=null && n<i){ | |
temp= (Object[]) temp[1];//unsafe but this is not rust | |
n++; | |
} | |
return temp; | |
} | |
public ArrayList<E> toArrayList() { | |
Object[] temp; | |
temp = first; | |
ArrayList<E> trt = new ArrayList<>(); | |
while (temp != null) { | |
trt.add((E) temp[0]); | |
temp = (Object[]) temp[1];//unsafe but this is not rust | |
} | |
return trt; | |
} | |
public void sort() { | |
ArrayList<E> list = toArrayList(); | |
list.sort((c, c1) -> ( c1==null ? 1 : (c==null ? -1 :((E) (c)).compareTo((E) (c1)))) ); | |
ll<E> temp = new ll<E>(); | |
for (int i = 0; i < list.size(); i++) { | |
if (list.get(i)!=null) | |
temp.addfirst(list.get(i)); | |
} | |
this.first=temp.first; | |
} | |
public E get(int i ) { | |
Object[] temp = gi(i); | |
if (i==0){ | |
temp=first; | |
} | |
return temp!=null? (E) temp[0]:null; | |
} | |
public void set(int x,E i ) { | |
Object[] temp = gi(x); | |
if (temp==null){ | |
temp = new Object[]{i, null}; | |
}else{ | |
if (x!=0) { | |
temp[0] = i; | |
}else{ | |
first[0]=i; | |
} | |
} | |
} | |
public void rev() { | |
ll<E> temp = new ll<E>(); | |
for(E i:this){ | |
temp.addfirst(i); | |
} | |
this.first=temp.first.clone(); | |
} | |
public ll<E> union(ll<E> added){ | |
ll<E> temp = new ll<E>(); | |
for (E i:this){ | |
temp.addlast(i); | |
} | |
for (E i:added){ | |
temp.addlast(i); | |
} | |
temp.rmsd(); | |
return temp; | |
} | |
public ll<E> intersection(ll<E> added){ | |
ll<E> temp = new ll<E>(); | |
ArrayList<E> temp2 = added.toArrayList(); | |
for (E i:this){ | |
if (temp2.contains(i)) { | |
temp.addlast(i); | |
} | |
} | |
temp.rmsd(); | |
return temp; | |
} | |
public void rmsd(){ | |
Hashtable<E,Integer> temp2=new Hashtable<>(l); | |
ArrayList<E> list = toArrayList(); | |
list.sort((c, c1) -> ( c1==null ? 1 : (c==null ? -1 :((E) (c)).compareTo((E) (c1)))) ); | |
ll<E> temp = new ll<E>(); | |
for (int i = 0; i < list.size(); i++) { | |
if (list.get(i)!=null) { | |
temp2.put(list.get(i), temp2.getOrDefault(list.get(i), i)); | |
} | |
} | |
for (int i = 0; i < temp2.keySet().size(); i++) { | |
temp.addfirst((E) temp2.keySet().toArray()[i]); | |
} | |
this.first=temp.first; | |
this.l=temp.l; | |
} | |
public Iterator<E> iterator() { | |
this.currentind=-1; | |
return this; | |
} | |
public boolean hasloop(){ | |
int c=-1; | |
for(E i:this){ | |
c+=1; | |
if (c>l+10){ | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public boolean hasNext() { | |
if (currentind==-1){ | |
return first!=null; | |
}else{ | |
return current!=null; | |
} | |
} | |
public void addloop(){ | |
System.out.print(" \u001B[5;31moh no the evil kumquats attacked and introduced a loop in your singly linked array list"); | |
Object[] temp = gi(l-1); | |
temp[1]=first; | |
} | |
private Object[] getrandomnode(){ | |
Random t = new Random(); | |
return gi(t.nextInt(0,l)); | |
} | |
public Stream<E> tostream(){ | |
Stream<E> t = java.util.stream.StreamSupport.stream(this.spliterator(),false); | |
return t; | |
} | |
public Stream<E> uniquestream(){ | |
return this.tostream().distinct(); | |
} | |
public void removeDupes(){ | |
ll<E> temp = new ll<>(); | |
for (Iterator<E> it = uniquestream().distinct().iterator(); it.hasNext(); ) { | |
E i = it.next(); | |
temp.addlast(i); | |
} | |
this.first=temp.first.clone(); | |
this.l=temp.l; | |
} | |
@Override | |
public E next() { | |
if (currentind==-1){ | |
currentind=0; | |
current = (Object[]) first[1]; | |
return (E) first[0]; | |
}else{ | |
if (current==null){ | |
currentind=-1; | |
return null; | |
} | |
E tmp = (E) current[0]; | |
current = (Object[]) current[1]; | |
return tmp; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
ll<String> x = new ll<>(); | |
x.addlast("hello"); | |
x.addlast("hello"); | |
x.addlast("hello"); | |
x.addlast("world"); | |
x.addlast("worldx"); | |
x.rev(); | |
//System.out.print(Arrays.toString(x.gi(1))); | |
for(String i:x) { | |
System.out.print(i); | |
} | |
//x.rotateToFront(); | |
x.removeDupes();; | |
System.out.print(x.hasloop()); | |
System.out.print(Arrays.toString(x.union(x).toArrayList().toArray())); | |
/* | |
ExampleClass ex = new ExampleClass(); | |
System.out.println("Ex "+ex +"\n starting ex.a "+ ex.a); | |
System.out.println("Before call-by-value: " + ex.a); | |
ex.call(520); | |
System.out.println("After call-by-value: " + ex.a); | |
System.out.println("Before call-by-reference: " + ex.a); | |
// passing the object as a value using pass-by-reference | |
ex.call(ex); | |
System.out.println("After call-by-reference: " + ex.a); | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment