Fun Stuff > CLIKC

A programming thread!

<< < (15/37) > >>

snalin:
Heartbleed wouldn't have happened had it been written in a higher level language!

No, each to their own. You get shit done in C as well, you just have the whole additional layer of complexity between raw values and data types that you kinda have to handle yourself. And segfaults exist. Oh lawd, the segfaults. "Yr code did something wrong somewhere, good fucking luck!"

These days I write more C# than Java, and I find the languages pretty comparable in how easy they're to use. There's no free C# IDE that's at the quality level of the free Java ones, but Visual Studio is supposed to be the shit, so I guess there's a trade off to consider there. Javadoc is vastly superior to the stupid xml scheme in C#, which is  why I still prefer Java, but that's only slightly.

I also have an irrational hatred for anything with the Lisp-syntax and anything purely functional just doesn't seem worth the hassle.

ankhtahr:
I don't know why, but to me C is a lot more intuitive than Java. I tell the computer what to do, it does what I tell it to do, and if it does something wrong, then I can be relatively sure that I made a mistake somewhere.

I think I prefer C over Java simply because I have my most experience with Assembler (AVR Assembler, but still), so I like to be able to project my code into stuff the CPU could do, or the memory. That's not at all possible with Java, it's too abstract.

But I somehow like both extremes, as I'm also a great fan of the functional Erlang.

Anyway, I'm finished with my C exercise sheets, and will go hand them in now, and then I'll go to sleep. It's 8 am. And then I should do the exercise sheet for software engineering! Yay! More Java!

ev4n:

--- Quote from: snalin on 11 May 2014, 17:21 ---Heartbleed wouldn't have happened had it been written in a higher level language!

--- End quote ---

I'm always leery of statements like this.  I know what you're saying, but compilers and OSs have bugs too...


--- Quote from: ankhtahr on 11 May 2014, 23:07 ---I don't know why, but to me C is a lot more intuitive than Java. I tell the computer what to do, it does what I tell it to do, and if it does something wrong, then I can be relatively sure that I made a mistake somewhere.

--- End quote ---

Which, as snalin rightly points out, is fine when you have 12 lines of code you wrote running independently.  A system of 100,000 lines of C code written by 3 organizations and couple of dozen developers?  Have fun....

ankhtahr:
uh. Now this i a slightly more challenging task. It's for the algorithms lecture. I need to implement a LRU Cache in Java, by combining a doubly linked list with a hash table. For maximum points I need to implement the list and the hash table myself, without relying on java.util.HashMap or any list class. I've got a few files where I have to fit it in. I have to put it into the LRUCache class.

snalin:

--- Quote ---Ihr code hier
machen Sie das schneller

--- End quote ---

I love how they use the formal pronouns when addressing you. We barbaric Norwegians stopped doing that sometime around the 1960s. They insist on writing the Javadoc in German (they do the same thing here), which makes it a tad bit harder for me to get you some hints, but I'll give it a god*.

Doubly linked lists are pretty easy - you need a class DoublyLinkedList, and a (possibly inner) class DoublyLinkedListNode. The head should always have a previous value of null, and the tail should always have a next value of null. That's the java way of doing it.


--- Code: ---public class DoublyLinkedList<DataType> {

MyLinkedListNode<DataType> head;
MyLinkedListNode<DataType> tail;
}

public class DoublyLinkedListNode<DataType> {
DataType data;
MyLinkedListNode<DataType> previous;
MyLinkedListNode<DataType> next;
}

--- End code ---

Then the insertFirst method in the DoublyLinkedList would be:


--- Code: ---public void insertFirst(DataType data) {
DoublyLinkedListNode<DataType> node = new DoublyLinkedListNode<DataType>(data);
head.setPrevious(node);
head = node;
}

--- End code ---

And so on.

The hash table is also really straight forward - it's a map from an int to a resizable list of some kind, and you use the hashCode of the element you insert as the key of the map. The map can be implemented as an array, where you use the index of the array as the key.

This means that you have an array of lists (for example your doubly linked ones):

map = DoublyLinkedList[]

And inserting an element o means inserting into the array at index o.hashCode:


--- Code: ---public void insert(DataType data) {
    map[data.hashCode].addFirst(data);
}

--- End code ---

Check that the map has that index. If it doesn't (IE. it's too small), you can make an array of double the size of the old one, and transfer all of the elements. Insertion is still an O(1) operation, if you look at the average insertion time (I'll save the proof for when you get it on an exercise sheet in some Data Structures or Algorithms course :p). You could also just use ArrayLists:

map = ArrayList<DoublyLinkedList>();

The way the built in ArrayList inserts elements is exactly the algorithm I just described. To do the LRU (if I've understood what the LRU is), simply removeLast on the LinkedList at the array space you're inserting in if it's got more than the max number of elements. Also, you shouldn't fill the array space with a list before you need that list, so the insert method will also need to check if the index of the array is currently null.

Hope that helps.

* When I found that typo, I realized that it needed to stay, because giving you a God is a pretty cool thing to do, I recon.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version