Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

quote optimizer

Name: Anonymous 2012-10-15 19:30


/*
 * File: prog-utils/quote-optimizer.cxx
 *
 * Purpose: Optimizes quotes for you
 *
 * Usage: At this point, you may only pass unoptimized quotes
 * (i.e., >>24>>28\n>>2 ...)
 * Text file input (-i) is supported
 *
 * Build instructions:
 *  g++ quote-optimizer.cxx -o quote-optimizer -std=c++0x -O2
 *
 * Contributors:
 *  Anonymous-san
 *
*/

/*
prog-utils/quote-optimizer
Copyright (C) 2012  Anonymous-san

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/


#include <vector>
#include <list>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <utility>
#include <algorithm>
#include <assert.h>

using namespace std;

vector<uint16_t> parse_quotes(const char *);
vector<string> optimize(vector<uint16_t> *);
int custom_itoa(int val, char * buf);
void get_from_stdin(char *, uint32_t, char **, FILE *);
void get_from_args(char *, uint32_t, char **, FILE *);
void get_from_file(char *, uint32_t, char **, FILE *);
static char * input = NULL;

int main(int argc, char ** argv)
{
  void (*get_from)(char *, uint32_t, char **, FILE *) = get_from_stdin;
  FILE * file = NULL;
  while(*argv != NULL)
  {
    if(!strcmp("--help", *argv)
      || !strcmp("-h", *argv))
    {
      puts("Usage: optimize-quotes [options] [string]"
      "\nOptions: --help [-h], --stdin [-s], --args [-a], --input [-i]");
      exit(1);
    }
    else if(!strcmp("--stdin", *argv)
      || !strcmp("-s", *argv))
    {
      get_from = get_from_stdin;
    }
    else if(!strcmp("--args", *argv)
      || !strcmp("-a", *argv))
    {
      get_from = get_from_args;
      assert(*++argv);
      input = *argv;
    }
    else if(!strcmp("--input", *argv)
      || !strcmp("-i", *argv))
    {
      get_from = get_from_file;
      assert(*++argv);
      file = fopen(*argv, "r");
    }
    ++argv;
  }
  //const char test[] = ">>1\n>>2\n>>7\n>>3\n>>20\n>>4\n>>45>>1>>7>>99";
  char * buf = new char [1024];
  get_from(buf, 1024, argv, file);
  if(file != NULL)
    fclose(file);
  input = input == NULL ? buf : input;
  vector<uint16_t> parsed = parse_quotes(input);
  delete [] buf;
  auto output = optimize(&parsed);

  auto i = begin(output);
  char c = 0;
  for( ; i != end(output); ++i)
  {
    c = ((i + 1) == end(output)) ? '\n' : ',';
    printf("%s%c", i->c_str(), c);
  }

  return 0;
}

void get_from_stdin
(
  char * dest,
  uint32_t n,
  char **,
  FILE *
)
{
  puts("--stdin (default) selected; enter unoptimized quotes");
  fgets(dest, n, stdin);
}

void get_from_args
(
  char * dest,
  uint32_t,
  char ** argv,
  FILE *
)
/*
 * TODO concatenate all arguments instead
 *
 * dummy function for the moment
*/
{
}

void get_from_file
(
  char * dest,
  uint32_t n,
  char **,
  FILE * file
)
{
  uint32_t actually_read = 0;
  static const uint32_t should_read = 128;
  //static const uint32_t max_file_size = 4096;

  do
  {
    actually_read = fread(dest, sizeof(char), should_read, file);
    dest += actually_read;
  } while(actually_read == should_read);

}

vector<uint16_t> parse_quotes
(
  const char * src
)
{
  vector<uint16_t> ret;
  char * buf = new char [20];
  char * pbuf = buf;

  uint8_t read = 0;
  static const uint8_t first_arrow = 0x01;
  static const uint8_t second_arrow = 0x02;
  static const uint8_t number = 0x04;

  while(*src != 0)
  {
    switch(*src)
    {
      case '>':
      {
        if(read ^ first_arrow
          && read ^ second_arrow
          && pbuf != buf)
        {
          *pbuf = 0;
          ret.push_back(atoi(buf));
          pbuf = buf;
        }
        if(read ^ first_arrow)
          read |= first_arrow;
        else if(read ^ second_arrow)
          read |= second_arrow;
        else if(read & first_arrow
          && read & second_arrow)
        {
          puts("Syntax error");
        }
        break;
      }
      case '0': case '1': case '2':
      case '3': case '4': case '5':
      case '6': case '7': case '8':
      case '9':
      {
        if(read ^ number)
          read |= number;
        if(read & second_arrow)
          *(pbuf++) = *src;
        break;
      }
      default:
      {
        /*
        if(read & first_arrow)
          read -= first_arrow;
        if(read & second_arrow)
          read -= second_arrow;
        */
        break;
      }
    }

    ++src;
  }

  *pbuf = 0;
  ret.push_back(atoi(buf));
  pbuf = buf;

  delete [] buf;

  return ret;
}

vector<string> optimize
(
  vector<uint16_t> * src
)
{
  vector<string> ret;
  char * const buf = new char[20];

  //sort
  sort(src->begin(), src->end());

  //set up for reading
  uint8_t read = 0;
  static const uint8_t range = 0x01;
  static const uint8_t item = 0x02;
 
  uint16_t first_in_range = 0;
  uint16_t last_in_range = 0;

  uint16_t last_number_read = 1;

  //reading
  for(auto& i : *src)
  {
    if(i == last_number_read)
      continue;
    if(i == last_number_read + 1)
    {
      if(read ^ range)
      {
        first_in_range = last_number_read;
        read |= range;
      }
    }
    else
    {
      if(read & range)
      {
        last_in_range = last_number_read;
        uint16_t l = custom_itoa(first_in_range, buf);
        buf[l] = '-';
        uint16_t m = custom_itoa(last_in_range, buf + l + 1);
        buf[l + m + 1] = 0;
        ret.push_back(buf);

        read -= range;
      }

      if(read ^ item)
        read |= item;

      uint16_t l = custom_itoa(i, buf);
      ret.push_back(buf);
     
    }
   
    last_number_read = i;
  }

  delete [] buf;

  return ret;
}


int custom_itoa(int val, char * buf)
//stolen code
{
    const unsigned int radix = 10;

    char* p;
    unsigned int a;        //every digit
    int len;
    char* b;            //start of the digit char
    char temp;
    unsigned int u;

    p = buf;

    if (val < 0)
    {
        *p++ = '-';
        val = 0 - val;
    }
    u = (unsigned int)val;

    b = p;

    do
    {
        a = u % radix;
        u /= radix;

        *p++ = a + '0';

    } while (u > 0);

    len = (int)(p - buf);

    *p-- = 0;

    //swap
    do
    {
        temp = *p;
        *p = *b;
        *b = temp;
        --p;
        ++b;

    } while (b < p);

    return len;
}

Name: Anonymous 2012-10-15 19:33

-1, not JavaScript

Name: Anonymous 2012-10-15 19:35

$ cat okay
>>3>>2>>1>>54>>2>>4>>89>>87>>88>>79>>128>>389
$ g++ quote-optimizer.cxx -o quote-optimizer -O3 -std=c++0x
$ ./quote-optimizer -i okay
1-4,54,79,87,88-89,128,389
$ ./quote-optimizer --stdin
>>3>>2>>1>>54>>2>>4>>89>>87>>88>>79>>128>>389
1-4,54,79,87,88-89,128,389

Name: Anonymous 2012-10-15 19:49

>>1
Ahmed won't use it if it's GPL.

Name: Anonymous 2012-10-15 20:04

Ahmed-kun, they made it for you!

Name: Anonymous 2012-10-15 20:39

Holy enterprise bloat. What do I expect from something written in sepples.

Name: Anonymous 2012-10-15 21:29

Oh Jesus this is terrible. I don't know what you're doing, but if I wrote this I would probably manage it in about 10 lines of C.

Name: Anonymous 2012-10-15 21:33

that code is fucking ugly

Name: Anonymous 2012-10-15 21:45

I like it ~tima.

Name: Anonymous 2012-10-15 21:57

Typical GNU software.

Name: Anonymous 2012-10-15 22:00

IF only there were sweet and simple algorithms for looping through a series of numbers and sorting them.

Name: Anonymous 2012-10-15 22:07

>>11
Too bad such a thing will never exist.

Name: Anonymous 2012-10-15 22:48

If only you actually knew how to program instead of puking on your keyboard and calling it a program.

Name: Anonymous 2012-10-15 23:20

That feel when I can write this in one line of APL.

Name: Anonymous 2012-10-15 23:52

'>>',1↓⊃,/',',¨⍕¨(¯1↓1,A≠1⌽A)/A←(⍋A)⌷¨⊂A←⍎¨(~B∨((⍴A)⍴0,B←'>>'⍷A)∨','⍷A)⊂A
It handles commas and duplicates but doesn't do ranges yet.

Name: Anonymous 2012-10-16 0:01

250 lines of code
That's GNU QUALITY !

And it could probably be done in one line of Python. No wonder Uriel offed himself.

Name: Anonymous 2012-10-16 0:07

>>16
and good riddance!

Name: Anonymous 2012-10-16 0:08

>>16
It could easily be done in one line of C.

Name: Anonymous 2012-10-16 0:10

>>18
#include <stdio.h>

There's the first line. Your move.

Name: Anonymous 2012-10-16 0:31

>>19
Why the fuck would I use stdio.h?

Name: Anonymous 2012-10-16 0:47

>>20
To print out the result, genius.

Oh, don't tell me you write non-ANSI C (faggot).

Name: Anonymous 2012-10-16 0:57

g++
No thank you!

And GPL ahahaha

Name: Anonymous 2012-10-16 1:01

>>15
what? thought bb and lisp were le /prog/ langs...

#lang racket
(require racket/set)
(let ((quotes (list->set
               (map string->number
                    (regexp-match* #rx"[0-9]+"
                                   (port->string))))))
  (set-map quotes (λ (s)
                    (display ">>")
                    (display s))))

Name: Anonymous 2012-10-16 1:20

>>21
Why would I write the code to print out the results?
Even a retarded /g/-class code monkey could write that, so let them do it. All the real work can be done in one line.

Name: Anonymous 2012-10-16 2:09

Sepples is disgusting and you should hate yourself.

Name: Anonymous 2012-10-16 2:27

>>24
Using the #line preprocessor directive you can write everything in 0 lines!

Name: Anonymous 2012-10-16 3:47

>>6-7,16,18,25
If C++ is so bad, why is the most important free software program being rewritten in it?

http://www.developers.slashdot.org/story/12/08/15/1338212/gcc-switches-from-c-to-c

Name: Anonymous 2012-10-16 4:02

>>27
I said it was a disgusting language, if you consider GNU code beautiful then you have a warped sense of beauty.

Name: Anonymous 2012-10-16 5:07

sage for ``using namespace std;'', pulling in a million headers but still writing your own itoa(), using both <string> and <string.h>, no support for partially optimised quotes, and using c-style <X.h> headers instead of <cX>

check my oxford comma

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2012-10-16 6:58

but still writing your own itoa
And one that's far less efficient than it should be. Determining how many digits will be emitted is trivial. Then emit them in descending order. NOT emit in reverse and swap them afterwards, that's just a stupid waste of memory accesses.

Name: Anonymous 2012-10-16 8:04

>>30
Anytime you want to stop pretending you know anything about real-world performance, go right ahead.

Name: Anonymous 2012-10-16 8:34

You need a code optimiser for your quote optimiser.

Name: Anonymous 2012-10-16 8:37

>>29
nice comma bro but check my binary pentuples

Name: Anonymous 2012-10-16 17:19

And one that's far less efficient than it should be. Determining how many digits will be emitted is trivial. Then emit them in descending order. NOT emit in reverse and swap them afterwards, that's just a stupid waste of memory accesses.
int custom_itoa(int val, char * buf) {
  return sprintf(buf, '%d', val);

}

Name: Anonymous 2012-10-16 17:38

>>30
String.h and string are completely fucking different files. Please be a troll

Name: Anonymous 2012-10-16 17:43

If you don't use a static char buffer[11] for your int32_t itoa, you're doing it wrong. Let the user copy it into another array afterwards if they need to use itoa again.

Name: Anonymous 2012-10-16 18:10

>>34
'%d'
Hold on a second there, champ.

Name: Anonymous 2012-10-16 18:23

>>37
I was going to see how long it took for Cudder to notice.
Thanks for pointing it out to him.

Name: Anonymous 2012-10-16 18:30

>>36
Twits like you are the reason POSIX is full of _r functions.

Name: Anonymous 2012-10-16 18:33

>>39
JavaScript does just fine with one thread. Why should a much less powerful language need more than that?

Name: Anonymous 2012-10-16 21:58

>>38
Cudder is a she

Name: Anonymous 2012-10-16 23:58

>>36
If you use a static char buffer[11] for your int32_t itoa, you're doing it wrong. Let the user trash it from another thread afterwards if they need to use itoa concurrently.

Name: Anonymous 2012-10-17 3:06

>>39,42
If you really need a concurrent itoa, create another worker thread which only does that.
Or simply use mutexes.

Name: Anonymous 2012-10-17 3:28

>>41
Cudder is a he, dumbass.

Check le doobs.

Name: Anonymous 2012-10-17 4:00

>>41
Cudder is a tranny.
Unless you can provide proof of his biologically female born identity.

Name: Anonymous 2012-10-17 7:33

More importantly Cudder is a kike, defending Jewish Rights.

Name: Anonymous 2012-10-17 8:37

>>44
Cudder is a he, dumbass.
Actually, Cudder is a "they".

Name: Anonymous 2012-10-17 8:42

*Cudder are

Name: Anonymous 2012-10-17 8:47

Yes, Cudder are those threee little kawai coding girls. Not even ashamed.

Name: Anonymous 2012-10-17 9:33

uint32_t for da portable assembly wangwadge

Name: Anonymous 2012-10-17 10:02

>>44
Cudder is a heeb.
Why else would he like Intel?

Name: Anonymous 2012-10-17 12:41

OK, so was it Hahaharues (or whatever) who was the ``she'' then?

Name: Anonymous 2012-10-17 12:43

>>52
YOU MENA HARUHI

Name: Anonymous 2012-10-17 19:25

>>53
Sorry, I don't watch anime.

Name: Anonymous 2012-10-17 20:01

dubs optimizer
checked automatically

Name: Anonymous 2012-10-17 20:10

>>51
It works.

Don't change these.
Name: Email:
Entire Thread Thread List