Class: Puzzle

Inherits:
Object
  • Object
show all
Defined in:
lib/puzzle/version.rb,
ext/libpuzzle.c

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'1.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#vectorObject (readonly)

Class Method Details

.new(file_path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'ext/libpuzzle.c', line 17

VALUE new(VALUE klass, VALUE file_path){
  Puzzle *puzzle;
  VALUE ret = Data_Make_Struct(klass, Puzzle, 0, free_puzzle, puzzle);
  puzzle_init_context(&puzzle->context);
  puzzle_init_cvec(&puzzle->context, &puzzle->vector);

  if(puzzle_fill_cvec_from_file(&puzzle->context, &puzzle->vector, StringValueCStr(file_path)) < 0)
    rb_raise(rb_eError, "failed to open file");

  VALUE rb_vector = rb_str_new((const char*)puzzle->vector.vec, puzzle->vector.sizeof_vec);

  rb_iv_set(ret, "@vector", rb_vector);
  return ret;
}

.uncompress(compressed_string) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'ext/libpuzzle.c', line 61

VALUE uncompress(VALUE klass, VALUE compressed_string){
  Puzzle *puzzle;
  PuzzleCompressedCvec c_cvec;

  VALUE ret = Data_Make_Struct(klass, Puzzle, 0, free_puzzle, puzzle);
  puzzle_init_context(&puzzle->context);
  puzzle_init_cvec(&puzzle->context, &puzzle->vector);
  c_cvec.vec = (unsigned char*)RSTRING_PTR(compressed_string);
  c_cvec.sizeof_compressed_vec = RSTRING_LEN(compressed_string);

  puzzle_uncompress_cvec(&puzzle->context, &c_cvec, &puzzle->vector);

  VALUE rb_vector = rb_str_new((const char*)puzzle->vector.vec, puzzle->vector.sizeof_vec);
  rb_iv_set(ret, "@vector", rb_vector);
  return ret;
}

Instance Method Details

#==(p2) ⇒ Object



45
46
47
# File 'ext/libpuzzle.c', line 45

VALUE equal(VALUE p1, VALUE p2){
  return _distance(p1, p2) < 0.6 ? Qtrue : Qfalse;
}

#compressObject



49
50
51
52
53
54
55
56
57
58
59
# File 'ext/libpuzzle.c', line 49

VALUE compress(VALUE self){
  Puzzle *puzzle;
  PuzzleCompressedCvec c_cvec;
  Data_Get_Struct(self, Puzzle, puzzle);
  puzzle_init_compressed_cvec(&puzzle->context, &c_cvec);

  puzzle_compress_cvec(&puzzle->context, &c_cvec, &puzzle->vector);
  VALUE ret = rb_str_new((const char*)c_cvec.vec, c_cvec.sizeof_compressed_vec);
  puzzle_free_compressed_cvec(&puzzle->context, &c_cvec);
  return ret;
}

#distance(p2) ⇒ Object



41
42
43
# File 'ext/libpuzzle.c', line 41

VALUE distance(VALUE p1, VALUE p2){
  return rb_float_new(_distance(p1, p2));
}